1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class InitialSetup20240226130829 extends Migration {
4 async up(): Promise<void> {
5 // TODO: The migration needs to take care of moving data before dropping columns, among other things
6 const storeTables = await this.execute(
7 "select * from information_schema.tables where table_name = 'store' and table_schema = 'public'"
8 )
9
10 if (storeTables.length > 0) {
11 this.addSql(`alter table "store" alter column "id" TYPE text;`)
12 this.addSql(`alter table "store" alter column "name" TYPE text;`)
13 this.addSql(
14 `alter table "store" alter column "name" SET DEFAULT 'Medusa Store';`
15 )
16 this.addSql(
17 `alter table "store" alter column "default_currency_code" TYPE text;`
18 )
19 this.addSql(
20 `alter table "store" alter column "default_currency_code" drop not null;`
21 )
22 this.addSql(
23 `alter table "store" alter column "default_sales_channel_id" TYPE text;`
24 )
25 this.addSql(
26 `alter table "store" alter column "default_location_id" TYPE text;`
27 )
28
29 this.addSql(
30 `alter table "store" add column "default_region_id" text null;`
31 )
32 this.addSql(
33 `alter table "store" add column "deleted_at" timestamptz null;`
34 )
35 this.addSql(
36 `alter table "store" add column "supported_currency_codes" text[] not null default \'{}\';`
37 )
38
39 this.addSql(
40 'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
41 )
42
43 this.addSql(
44 `alter table "store" drop constraint if exists "FK_61b0f48cccbb5f41c750bac7286";`
45 )
46 this.addSql(
47 `alter table "store" drop constraint if exists "FK_55beebaa09e947cccca554af222";`
48 )
49
50 // this.addSql(`alter table "store" drop column "default_currency_code";`)
51 // this.addSql(`alter table "store" drop column "swap_link_template";`)
52 // this.addSql(`alter table "store" drop column "payment_link_template";`)
53 // this.addSql(`alter table "store" drop column "invite_link_template";`)
54 } else {
55 this.addSql(`create table if not exists "store"
56 ("id" text not null, "name" text not null default \'Medusa Store\', "supported_currency_codes" text[] not null default \'{}\',
57 "default_currency_code" text null, "default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null,
58 "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null,
59 constraint "store_pkey" primary key ("id"));`)
60
61 this.addSql(
62 'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
63 )
64 }
65 }
66 }