penisularhr / src / database / migrations / 1710470593660-create-audit-log.ts
1710470593660-create-audit-log.ts
Raw
/* eslint-disable max-len */
import { type MigrationInterface, type QueryRunner } from 'typeorm';

export class CreateAuditLog1710470593660 implements MigrationInterface {
  name = 'CreateAuditLog1710470593660';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TYPE "public"."audit_logs_operate_type_enum" AS ENUM('CREATE', 'UPDATE', 'DELETE')`,
    );
    await queryRunner.query(
      `CREATE TABLE "audit_logs" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "operate_type" "public"."audit_logs_operate_type_enum" NOT NULL, "table_name" character varying NOT NULL, "item_id" character varying, "summary_changes" text NOT NULL, "old_value" text, "new_value" text, "user_id" uuid, CONSTRAINT "PK_1bb179d048bbc581caa3b013439" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `ALTER TABLE "audit_logs" ADD CONSTRAINT "FK_bd2726fd31b35443f2245b93ba0" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "audit_logs" DROP CONSTRAINT "FK_bd2726fd31b35443f2245b93ba0"`,
    );
    await queryRunner.query(`DROP TABLE "audit_logs"`);
    await queryRunner.query(
      `DROP TYPE "public"."audit_logs_operate_type_enum"`,
    );
  }
}