1 import { generatePostgresAlterColummnIfExistStatement } from "@medusajs/framework/utils"
2 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
3
4 export class Migration20240214033943 extends Migration {
5 async up(): Promise<void> {
6 this.addSql(
7 'create table if not exists "invite" ("id" text not null, "email" text not null, "accepted" boolean not null default false, "token" text not null, "expires_at" timestamptz not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "invite_pkey" primary key ("id"));'
8 )
9 this.addSql(
10 'alter table "invite" add column if not exists "email" text not null;'
11 )
12
13 this.addSql(
14 generatePostgresAlterColummnIfExistStatement(
15 "invite",
16 ["user_email"],
17 "DROP NOT NULL"
18 )
19 )
20
21 this.addSql(
22 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_invite_email" ON "invite" (email) WHERE deleted_at IS NULL;'
23 )
24
25 this.addSql(
26 'CREATE INDEX IF NOT EXISTS "IDX_invite_token" ON "invite" (token) WHERE deleted_at IS NULL;'
27 )
28 this.addSql(
29 'CREATE INDEX IF NOT EXISTS "IDX_invite_deleted_at" ON "invite" (deleted_at) WHERE deleted_at IS NOT NULL;'
30 )
31
32 this.addSql(
33 'create table if not exists "user" ("id" text not null, "first_name" text null, "last_name" text null, "email" text not null, "avatar_url" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "user_pkey" primary key ("id"));'
34 )
35
36 this.addSql(
37 'alter table "user" add column if not exists "avatar_url" text null;'
38 )
39
40 this.addSql(
41 'CREATE INDEX IF NOT EXISTS "IDX_user_email" ON "user" (email) WHERE deleted_at IS NULL;'
42 )
43 this.addSql(
44 'CREATE INDEX IF NOT EXISTS "IDX_user_deleted_at" ON "user" (deleted_at) WHERE deleted_at IS NOT NULL;'
45 )
46 }
47
48 async down(): Promise<void> {
49 this.addSql('drop table if exists "invite" cascade;')
50
51 this.addSql('drop table if exists "user" cascade;')
52 }
53 }