1 import { generatePostgresAlterColummnIfExistStatement } from "@medusajs/framework/utils"
2 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
3
4 export class RegionModuleSetup20240205173216 extends Migration {
5 async up(): Promise<void> {
6 this.addSql(`
7 -- Create or update "region" table
8 CREATE TABLE IF NOT EXISTS "region" (
9 "id" text NOT NULL,
10 "name" text NOT NULL,
11 "currency_code" text NOT NULL,
12 "metadata" jsonb NULL,
13 "created_at" timestamptz NOT NULL DEFAULT now(),
14 "updated_at" timestamptz NOT NULL DEFAULT now(),
15 "deleted_at" timestamptz NULL,
16 CONSTRAINT "region_pkey" PRIMARY KEY ("id")
17 );
18 -- Adjust "region" table
19 ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_3bdd5896ec93be2f1c62a3309a5";
20 ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
21 ${generatePostgresAlterColummnIfExistStatement(
22 "region",
23 ["tax_rate", "gift_cards_taxable", "includes_tax"],
24 "DROP NOT NULL"
25 )}
26 ALTER TABLE "region" ADD COLUMN IF NOT EXISTS "automatic_taxes" BOOLEAN NOT NULL DEFAULT TRUE;
27 CREATE INDEX IF NOT EXISTS "IDX_region_deleted_at" ON "region" ("deleted_at") WHERE "deleted_at" IS NOT NULL;
28 -- Create or update "region_country" table
29 CREATE TABLE IF NOT EXISTS "region_country" (
30 "iso_2" text NOT NULL,
31 "iso_3" text NOT NULL,
32 "num_code" text NOT NULL,
33 "name" text NOT NULL,
34 "display_name" text NOT NULL,
35 "region_id" text NULL,
36 CONSTRAINT "region_country_pkey" PRIMARY KEY ("iso_2")
37 );
38 CREATE UNIQUE INDEX IF NOT EXISTS "IDX_region_country_region_id_iso_2_unique" ON "region_country" (region_id, iso_2);
39 -- Adjust foreign keys for "region_country"
40 ALTER TABLE "region_country" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
41 ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE ON DELETE SET NULL;
42 `)
43 }
44 }