1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2 import { ulid } from "ulid"
3
4 export class Migration20250825132614 extends Migration {
5 override async up(): Promise<void> {
6 // 1. Find all type-code shipping option types
7 const typeCodeTypeIds = await this.execute(`
8 SELECT id FROM "shipping_option_type"
9 WHERE code = 'type-code' AND deleted_at IS NULL
10 `)
11
12 if (typeCodeTypeIds.length > 0) {
13 const defaultTypeId = `sotype_${ulid()}`
14 // 2. Create default shipping option type
15 await this.execute(`
16 INSERT INTO "shipping_option_type" (id, label, description, code)
17 VALUES ('${defaultTypeId}', 'Default', 'Default shipping option type', 'default');
18 `)
19
20 const typeIdsString = typeCodeTypeIds
21 .map((row) => `'${row.id}'`)
22 .join(",")
23
24 // 3. Reassign shipping options to the default type
25 await this.execute(`
26 UPDATE "shipping_option"
27 SET shipping_option_type_id = '${defaultTypeId}'
28 WHERE shipping_option_type_id IN (${typeIdsString}) AND deleted_at IS NULL;
29 `)
30
31 // 4. Soft delete the old type-code types
32 await this.execute(`
33 UPDATE "shipping_option_type"
34 SET deleted_at = now()
35 WHERE id IN (${typeIdsString});
36 `)
37 }
38 }
39
40 override async down(): Promise<void> {
41 // Not reversible: would require restoring old type-code types + reassignment
42 }
43 }