Skip to content

Commit

Permalink
feat(region): Add migration (#6320)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl authored Feb 6, 2024
1 parent 12054f5 commit e2cb72e
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ describe("Region Module Service", () => {
expect.objectContaining({
id: createdRegion.id,
name: "Europe",
currency_code: "EUR",
currency: expect.objectContaining({
code: "eur",
name: "Euro",
}),
currency_code: "eur",
countries: [],
})
)
Expand All @@ -80,7 +76,7 @@ describe("Region Module Service", () => {
expect.objectContaining({
id: region.id,
name: "Europe",
currency_code: "EUR",
currency_code: "eur",
currency: expect.objectContaining({
code: "eur",
name: "Euro",
Expand Down
287 changes: 287 additions & 0 deletions packages/region/src/migrations/.snapshot-medusa-region.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
{
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
"columns": {
"code": {
"name": "code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol": {
"name": "symbol",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol_native": {
"name": "symbol_native",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "region_currency",
"schema": "public",
"indexes": [
{
"keyName": "region_currency_pkey",
"columnNames": [
"code"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"currency_code": {
"name": "currency_code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "region",
"schema": "public",
"indexes": [
{
"columnNames": [
"currency_id"
],
"composite": false,
"keyName": "IDX_region_currency_code",
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"composite": false,
"keyName": "IDX_region_deleted_at",
"primary": false,
"unique": false
},
{
"keyName": "region_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"region_currency_id_foreign": {
"constraintName": "region_currency_code_foreign",
"columnNames": [
"currency_code"
],
"localTableName": "public.region",
"referencedColumnNames": [
"code"
],
"referencedTableName": "public.region_currency",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"iso_2": {
"name": "iso_2",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"iso_3": {
"name": "iso_3",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"num_code": {
"name": "num_code",
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"display_name": {
"name": "display_name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"region_id": {
"name": "region_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
}
},
"name": "region_country",
"schema": "public",
"indexes": [
{
"columnNames": [
"region_id"
],
"composite": false,
"keyName": "IDX_country_region_id",
"primary": false,
"unique": false
},
{
"keyName": "region_country_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"region_country_region_id_foreign": {
"constraintName": "region_country_region_id_foreign",
"columnNames": [
"region_id"
],
"localTableName": "public.region_country",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.region",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
}
]
}
53 changes: 53 additions & 0 deletions packages/region/src/migrations/RegionModuleSetup20240205173216.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Migration } from "@mikro-orm/migrations"

export class RegionModuleSetup20240205173216 extends Migration {
async up(): Promise<void> {
this.addSql(`
-- Create or update "region" table
CREATE TABLE IF NOT EXISTS "region" (
"id" text NOT NULL,
"name" text NOT NULL,
"currency_code" text NOT NULL,
"metadata" jsonb NULL,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
"deleted_at" timestamptz NULL,
CONSTRAINT "region_pkey" PRIMARY KEY ("id")
);
-- Create or update "region_currency" table
CREATE TABLE IF NOT EXISTS "region_currency" (
"code" text NOT NULL,
"symbol" text NOT NULL,
"symbol_native" text NOT NULL,
"name" text NOT NULL,
CONSTRAINT "region_currency_pkey" PRIMARY KEY ("code")
);
-- Adjust "region" table
ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_3bdd5896ec93be2f1c62a3309a5";
ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
ALTER TABLE "region" ADD CONSTRAINT "region_currency_code_foreign" FOREIGN KEY ("currency_code") REFERENCES "region_currency" ("code") ON UPDATE CASCADE;
CREATE INDEX IF NOT EXISTS "IDX_region_currency_code" ON "region" ("currency_code");
CREATE INDEX IF NOT EXISTS "IDX_region_deleted_at" ON "region" ("deleted_at") WHERE "deleted_at" IS NOT NULL;
-- Create or update "region_country" table
CREATE TABLE IF NOT EXISTS "region_country" (
"id" text NOT NULL,
"iso_2" text NOT NULL,
"iso_3" text NOT NULL,
"num_code" int NOT NULL,
"name" text NOT NULL,
"display_name" text NOT NULL,
"region_id" text NULL,
CONSTRAINT "region_country_pkey" PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "IDX_country_region_id" ON "region_country" ("region_id");
-- Adjust foreign keys for "region_country"
ALTER TABLE "region_country" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE;
`)
}
}
10 changes: 3 additions & 7 deletions packages/region/src/models/country.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
BeforeCreate,
Cascade,
Entity,
ManyToOne,
OnInit,
Expand Down Expand Up @@ -31,16 +30,13 @@ export default class Country {
@Property({ columnType: "text" })
display_name: string

@Property({ columnType: "text", nullable: true })
region_id: string | null = null

@ManyToOne({
entity: () => Region,
onDelete: "cascade",
fieldName: "region_id",
index: "IDX_country_region_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
nullable: true,
})
region: Region
region: Region | null = null

@BeforeCreate()
onCreate() {
Expand Down
Loading

0 comments on commit e2cb72e

Please sign in to comment.