Merge pull request #931 from Tria-plc/Truckmaintenance

fix(maintenance): schema-qualify maintenance_intervals migration
This commit is contained in:
Hagernesh Tadesse
2026-07-23 11:21:32 +03:00
committed by GitHub

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 {
name = 'AddMaintenanceIntervals2800000000000';
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.dropTable('maintenance_intervals', true, true, true);
await queryRunner.query(`DROP TABLE IF EXISTS freight.maintenance_intervals;`);
}
}