From fb1e4ffcb31b0f6b6cce9d0aa2018a65836f67bc Mon Sep 17 00:00:00 2001 From: Hagernesh Date: Thu, 23 Jul 2026 08:18:33 +0000 Subject: [PATCH] fix(maintenance): schema-qualify maintenance_intervals migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit createUniqueConstraint/createForeignKey used bare table names, which resolve against the default schema (public) while the table was created in freight — the migration failed on boot and rolled back every start. Rewritten as idempotent schema-qualified SQL (unique index replaces the constraint, FK inline on the column). Co-Authored-By: Claude Fable 5 --- .../2800000000000-AddMaintenanceIntervals.ts | 127 +++++------------- 1 file changed, 32 insertions(+), 95 deletions(-) diff --git a/apps/edr-freight-api/src/migrations/2800000000000-AddMaintenanceIntervals.ts b/apps/edr-freight-api/src/migrations/2800000000000-AddMaintenanceIntervals.ts index 3013f0320..f4f125eaf 100644 --- a/apps/edr-freight-api/src/migrations/2800000000000-AddMaintenanceIntervals.ts +++ b/apps/edr-freight-api/src/migrations/2800000000000-AddMaintenanceIntervals.ts @@ -1,103 +1,40 @@ -import { MigrationInterface, QueryRunner, Table, TableIndex, TableForeignKey, TableUnique } from 'typeorm'; +import { MigrationInterface, QueryRunner } from 'typeorm'; +/** + * KM-based maintenance scheduling: per-vehicle service intervals (by km + * and/or days) driving the maintenance due engine. Raw schema-qualified SQL — + * the builder API resolved bare table names against the default schema and + * failed on boot ("Table maintenance_intervals does not exist"). + */ export class AddMaintenanceIntervals2800000000000 implements MigrationInterface { + name = 'AddMaintenanceIntervals2800000000000'; + public async up(queryRunner: QueryRunner): Promise { - await queryRunner.createTable( - new Table({ - name: 'maintenance_intervals', - schema: 'freight', - columns: [ - { - name: 'id', - type: 'uuid', - isPrimary: true, - generationStrategy: 'uuid', - default: 'gen_random_uuid()', - }, - { - name: 'vehicle_id', - type: 'uuid', - isNullable: false, - }, - { - name: 'maintenance_type', - type: 'varchar', - isNullable: false, - }, - { - name: 'interval_km', - type: 'numeric', - precision: 14, - scale: 2, - isNullable: true, - }, - { - name: 'interval_days', - type: 'integer', - isNullable: true, - }, - { - name: 'description', - type: 'text', - isNullable: true, - }, - { - name: 'is_active', - type: 'boolean', - default: true, - }, - { - name: 'created_at', - type: 'timestamptz', - default: 'now()', - }, - { - name: 'updated_at', - type: 'timestamptz', - default: 'now()', - }, - { - name: 'deleted_at', - type: 'timestamptz', - isNullable: true, - }, - ], - }), - ); - - // Add indexes - await queryRunner.createIndex( - new Table({ name: 'maintenance_intervals', schema: 'freight' }), - new TableIndex({ - name: 'IDX_maintenance_intervals_vehicle_type', - columnNames: ['vehicle_id', 'maintenance_type'], - }), - ); - - // Add unique constraint - await queryRunner.createUniqueConstraint( - 'maintenance_intervals', - new TableUnique({ - name: 'UQ_maintenance_intervals_vehicle_type', - columnNames: ['vehicle_id', 'maintenance_type'], - }), - ); - - // Add foreign key - await queryRunner.createForeignKey( - 'maintenance_intervals', - new TableForeignKey({ - name: 'FK_maintenance_intervals_vehicle', - columnNames: ['vehicle_id'], - referencedSchema: 'freight', - referencedTableName: 'vehicles', - referencedColumnNames: ['id'], - onDelete: 'CASCADE', - }), - ); + await queryRunner.query(` + CREATE TABLE IF NOT EXISTS freight.maintenance_intervals ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + vehicle_id uuid NOT NULL REFERENCES freight.vehicles(id) ON DELETE CASCADE, + maintenance_type varchar NOT NULL, + interval_km numeric(14,2), + interval_days integer, + description text, + is_active boolean NOT NULL DEFAULT true, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + deleted_at timestamptz + ); + `); + await queryRunner.query(` + CREATE INDEX IF NOT EXISTS "IDX_maintenance_intervals_vehicle_type" + ON freight.maintenance_intervals (vehicle_id, maintenance_type); + `); + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS "UQ_maintenance_intervals_vehicle_type" + ON freight.maintenance_intervals (vehicle_id, maintenance_type); + `); } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.dropTable('maintenance_intervals', true, true, true); + await queryRunner.query(`DROP TABLE IF EXISTS freight.maintenance_intervals;`); } }