diff --git a/apps/edr-freight-api/src/migrations/2260000000000-SeedEdrWagonFleetErNumbering.ts b/apps/edr-freight-api/src/migrations/2260000000000-SeedEdrWagonFleetErNumbering.ts index fdfcf8be5..717a48217 100644 --- a/apps/edr-freight-api/src/migrations/2260000000000-SeedEdrWagonFleetErNumbering.ts +++ b/apps/edr-freight-api/src/migrations/2260000000000-SeedEdrWagonFleetErNumbering.ts @@ -44,6 +44,14 @@ export class SeedEdrWagonFleetErNumbering2260000000000 implements MigrationInter // train_set_wagons null their link, wagon_movements cascade. await queryRunner.query(`DELETE FROM freight.wagons;`); + // Wagon.wagonNumber declares `unique: true`, but some environments never got + // the constraint. Repair it here — the table is empty at this point, so the + // index build cannot fail on pre-existing duplicates. + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS wagons_wagon_number_key + ON freight.wagons (wagon_number); + `); + for (const row of FLEET) { if (row.end - row.start + 1 !== row.count) { throw new Error(`wagon_range_mismatch:${row.code}`); @@ -59,7 +67,9 @@ export class SeedEdrWagonFleetErNumbering2260000000000 implements MigrationInter } // generate_series builds the range server-side — one round trip per type - // instead of 1100 individual INSERTs. + // instead of 1100 individual INSERTs. No ON CONFLICT clause: every wagon + // was deleted above, so a plain INSERT cannot collide, and the clause would + // otherwise hard-require a unique index this table lacks on some envs. await queryRunner.query( ` INSERT INTO freight.wagons ( @@ -83,12 +93,7 @@ export class SeedEdrWagonFleetErNumbering2260000000000 implements MigrationInter NULL, NULL, NULL - FROM generate_series($2::int, $3::int) AS seq - ON CONFLICT (wagon_number) DO UPDATE SET - wagon_type_id = EXCLUDED.wagon_type_id, - status = EXCLUDED.status, - current_yard_id = EXCLUDED.current_yard_id, - updated_at = now(); + FROM generate_series($2::int, $3::int) AS seq; `, [typeRecord.id, row.start, row.end], );