Skip to content

Commit 059bfc1

Browse files
chore(fulfillment): cleanup old auto-generatred shipping type (medusajs#13298)
* chore(fulfillment): cleanup old auto-generatred shipping type * changeset * integrationt ests * random id * wrong code * switch steps around --------- Co-authored-by: Oli Juhl <[email protected]>
1 parent a0fca16 commit 059bfc1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

.changeset/mighty-clocks-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/fulfillment": patch
3+
---
4+
5+
chore(fulfillment): cleanup old auto-generatred shipping type
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Migration } from "@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+
}

0 commit comments

Comments
 (0)