import { MigrationInterface, QueryRunner } from 'typeorm'; export class PhaseF4SealedActs1790056800000 implements MigrationInterface { name = 'PhaseF4SealedActs1790056800000'; public async up(queryRunner: QueryRunner): Promise { await queryRunner.query(` UPDATE inspection_acts SET sealed_at = COALESCE(closed_at, updated_at, CURRENT_TIMESTAMP), sealed_by = closed_by WHERE status = 'CLOSED' AND sealed_at IS NULL `); await queryRunner.query(` CREATE OR REPLACE FUNCTION dhv2_f4_stamp_act_seal() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.status = 'CLOSED' AND OLD.status IS DISTINCT FROM 'CLOSED' THEN NEW.sealed_at := COALESCE(NEW.closed_at, CURRENT_TIMESTAMP); NEW.sealed_by := NEW.closed_by; END IF; RETURN NEW; END; $$ `); await queryRunner.query(` DROP TRIGGER IF EXISTS trg_dhv2_f4_stamp_act_seal ON inspection_acts `); await queryRunner.query(` CREATE TRIGGER trg_dhv2_f4_stamp_act_seal BEFORE UPDATE OF status ON inspection_acts FOR EACH ROW EXECUTE FUNCTION dhv2_f4_stamp_act_seal() `); } public async down(queryRunner: QueryRunner): Promise { await queryRunner.query(`DROP TRIGGER IF EXISTS trg_dhv2_f4_stamp_act_seal ON inspection_acts`); await queryRunner.query(`DROP FUNCTION IF EXISTS dhv2_f4_stamp_act_seal()`); } }