From ec82f8eb9fcd8186222a1f1c0f8e0e6860ddb320 Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 30 Jun 2026 02:03:20 +0000 Subject: [PATCH] fix(migrations): use CREATE TYPE IF NOT EXISTS for invoices enum Allows migration to run when enum already exists in prod DB. Prevents 'type already exists' error on redeployment. Co-Authored-By: Claude Haiku 4.5 --- .../1821000000002-CreateInvoices.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/apps/edr-freight-api/src/migrations/1821000000002-CreateInvoices.ts b/apps/edr-freight-api/src/migrations/1821000000002-CreateInvoices.ts index 5c42cad65..610fe6b30 100644 --- a/apps/edr-freight-api/src/migrations/1821000000002-CreateInvoices.ts +++ b/apps/edr-freight-api/src/migrations/1821000000002-CreateInvoices.ts @@ -15,16 +15,22 @@ export class CreateInvoices1821000000002 implements MigrationInterface { name = "CreateInvoices1821000000002"; public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - CREATE TYPE freight.invoices_status_enum AS ENUM ( - 'DRAFT', - 'PENDING', - 'PAID', - 'OVERDUE', - 'CANCELLED', - 'REFUNDED' - ); - `); + const typeExists = await queryRunner.query( + `SELECT 1 FROM pg_type WHERE typname = 'invoices_status_enum' AND typnamespace = 'freight'::regnamespace;`, + ); + + if (!typeExists.length) { + await queryRunner.query(` + CREATE TYPE freight.invoices_status_enum AS ENUM ( + 'DRAFT', + 'PENDING', + 'PAID', + 'OVERDUE', + 'CANCELLED', + 'REFUNDED' + ); + `); + } await queryRunner.query(` CREATE TABLE freight.invoices (