fix(maintenance): schema-qualify maintenance_intervals migration

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 <noreply@anthropic.com>
This commit is contained in:
Hagernesh
2026-07-23 08:18:33 +00:00
parent f2f6ac616f
commit fb1e4ffcb3

View File

@@ -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 { export class AddMaintenanceIntervals2800000000000 implements MigrationInterface {
name = 'AddMaintenanceIntervals2800000000000';
public async up(queryRunner: QueryRunner): Promise<void> { public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable( await queryRunner.query(`
new Table({ CREATE TABLE IF NOT EXISTS freight.maintenance_intervals (
name: 'maintenance_intervals', id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
schema: 'freight', vehicle_id uuid NOT NULL REFERENCES freight.vehicles(id) ON DELETE CASCADE,
columns: [ maintenance_type varchar NOT NULL,
{ interval_km numeric(14,2),
name: 'id', interval_days integer,
type: 'uuid', description text,
isPrimary: true, is_active boolean NOT NULL DEFAULT true,
generationStrategy: 'uuid', created_at timestamptz NOT NULL DEFAULT now(),
default: 'gen_random_uuid()', updated_at timestamptz NOT NULL DEFAULT now(),
}, deleted_at timestamptz
{ );
name: 'vehicle_id', `);
type: 'uuid', await queryRunner.query(`
isNullable: false, CREATE INDEX IF NOT EXISTS "IDX_maintenance_intervals_vehicle_type"
}, ON freight.maintenance_intervals (vehicle_id, maintenance_type);
{ `);
name: 'maintenance_type', await queryRunner.query(`
type: 'varchar', CREATE UNIQUE INDEX IF NOT EXISTS "UQ_maintenance_intervals_vehicle_type"
isNullable: false, ON freight.maintenance_intervals (vehicle_id, maintenance_type);
}, `);
{
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',
}),
);
} }
public async down(queryRunner: QueryRunner): Promise<void> { public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('maintenance_intervals', true, true, true); await queryRunner.query(`DROP TABLE IF EXISTS freight.maintenance_intervals;`);
} }
} }