|
| 1 | +import { MigrationInterface, QueryRunner } from 'typeorm'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates the `admin_correction_ledger` table. |
| 5 | + * |
| 6 | + * The table is intentionally designed to be append-only: |
| 7 | + * - No UPDATE or DELETE permissions should be granted to the application role. |
| 8 | + * - A database-level trigger can optionally enforce this; the application |
| 9 | + * layer already guards it via AdminLedgerService. |
| 10 | + * |
| 11 | + * Down migration is a simple DROP — this is safe because the table only ever |
| 12 | + * holds audit history and is never a FK dependency for operational tables. |
| 13 | + */ |
| 14 | +export class CreateAdminCorrectionLedger1803000000000 |
| 15 | + implements MigrationInterface |
| 16 | +{ |
| 17 | + name = 'CreateAdminCorrectionLedger1803000000000'; |
| 18 | + |
| 19 | + public async up(queryRunner: QueryRunner): Promise<void> { |
| 20 | + // Enum type for correction category |
| 21 | + await queryRunner.query(` |
| 22 | + CREATE TYPE "admin_correction_type_enum" AS ENUM ( |
| 23 | + 'BALANCE_CREDIT', |
| 24 | + 'BALANCE_DEBIT', |
| 25 | + 'FEE_WAIVER', |
| 26 | + 'FEE_ADJUSTMENT', |
| 27 | + 'INTEREST_CORRECTION', |
| 28 | + 'OTHER' |
| 29 | + ) |
| 30 | + `); |
| 31 | + |
| 32 | + await queryRunner.query(` |
| 33 | + CREATE TABLE "admin_correction_ledger" ( |
| 34 | + "id" UUID NOT NULL DEFAULT uuid_generate_v4(), |
| 35 | + "targetId" VARCHAR(255) NOT NULL, |
| 36 | + "targetType" VARCHAR(64) NOT NULL, |
| 37 | + "adminId" UUID NOT NULL, |
| 38 | + "correctionType" "admin_correction_type_enum" NOT NULL, |
| 39 | + "delta" VARCHAR(64) NOT NULL, |
| 40 | + "previousValue" TEXT, |
| 41 | + "newValue" TEXT, |
| 42 | + "reason" TEXT NOT NULL, |
| 43 | + "requestId" VARCHAR(255), |
| 44 | + "workflowId" VARCHAR(255), |
| 45 | + "metadata" JSONB, |
| 46 | + "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), |
| 47 | + CONSTRAINT "pk_admin_correction_ledger" PRIMARY KEY ("id") |
| 48 | + ) |
| 49 | + `); |
| 50 | + |
| 51 | + // Indexes for the most common query patterns |
| 52 | + await queryRunner.query(` |
| 53 | + CREATE INDEX "idx_acl_target_id" |
| 54 | + ON "admin_correction_ledger" ("targetId") |
| 55 | + `); |
| 56 | + |
| 57 | + await queryRunner.query(` |
| 58 | + CREATE INDEX "idx_acl_admin_id" |
| 59 | + ON "admin_correction_ledger" ("adminId") |
| 60 | + `); |
| 61 | + |
| 62 | + await queryRunner.query(` |
| 63 | + CREATE INDEX "idx_acl_request_id" |
| 64 | + ON "admin_correction_ledger" ("requestId") |
| 65 | + WHERE "requestId" IS NOT NULL |
| 66 | + `); |
| 67 | + |
| 68 | + await queryRunner.query(` |
| 69 | + CREATE INDEX "idx_acl_workflow_id" |
| 70 | + ON "admin_correction_ledger" ("workflowId") |
| 71 | + WHERE "workflowId" IS NOT NULL |
| 72 | + `); |
| 73 | + |
| 74 | + await queryRunner.query(` |
| 75 | + CREATE INDEX "idx_acl_created_at" |
| 76 | + ON "admin_correction_ledger" ("createdAt") |
| 77 | + `); |
| 78 | + |
| 79 | + // Composite index useful for reconciliation queries |
| 80 | + await queryRunner.query(` |
| 81 | + CREATE INDEX "idx_acl_target_type_correction_type" |
| 82 | + ON "admin_correction_ledger" ("targetId", "correctionType") |
| 83 | + `); |
| 84 | + } |
| 85 | + |
| 86 | + public async down(queryRunner: QueryRunner): Promise<void> { |
| 87 | + await queryRunner.query(`DROP TABLE IF EXISTS "admin_correction_ledger"`); |
| 88 | + await queryRunner.query( |
| 89 | + `DROP TYPE IF EXISTS "admin_correction_type_enum"`, |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments