104 lines
4.0 KiB
TypeScript
104 lines
4.0 KiB
TypeScript
import { Migration } from '@mikro-orm/migrations';
|
|
|
|
export class Migration20260627120000_add_payment_method_cards_table extends Migration {
|
|
|
|
override async up(): Promise<void> {
|
|
this.addSql(`
|
|
create table "payment_method_cards" (
|
|
"id" char(26) not null,
|
|
"created_at" timestamptz not null default now(),
|
|
"updated_at" timestamptz not null default now(),
|
|
"deleted_at" timestamptz null,
|
|
"payment_method_id" char(26) not null,
|
|
"card_number" varchar(255) not null,
|
|
"owner" varchar(255) not null,
|
|
constraint "payment_method_cards_pkey" primary key ("id")
|
|
);
|
|
`);
|
|
this.addSql(`create index "payment_method_cards_payment_method_id_index" on "payment_method_cards" ("payment_method_id");`);
|
|
this.addSql(`create index "payment_method_cards_deleted_at_index" on "payment_method_cards" ("deleted_at");`);
|
|
this.addSql(`create index "payment_method_cards_created_at_index" on "payment_method_cards" ("created_at");`);
|
|
this.addSql(`
|
|
alter table "payment_method_cards"
|
|
add constraint "payment_method_cards_payment_method_id_foreign"
|
|
foreign key ("payment_method_id") references "payment_methods" ("id")
|
|
on update cascade on delete cascade;
|
|
`);
|
|
|
|
this.addSql(`
|
|
insert into "payment_method_cards" ("id", "created_at", "updated_at", "payment_method_id", "card_number", "owner")
|
|
select substring(replace(gen_random_uuid()::text, '-', ''), 1, 26), now(), now(), "id", "card_number", "card_owner"
|
|
from "payment_methods"
|
|
where "card_number" is not null and "card_owner" is not null;
|
|
`);
|
|
this.addSql(`
|
|
insert into "payment_method_cards" ("id", "created_at", "updated_at", "payment_method_id", "card_number", "owner")
|
|
select substring(replace(gen_random_uuid()::text, '-', ''), 1, 26), now(), now(), "id", "card_number2", "card_owner2"
|
|
from "payment_methods"
|
|
where "card_number2" is not null and "card_owner2" is not null;
|
|
`);
|
|
this.addSql(`
|
|
insert into "payment_method_cards" ("id", "created_at", "updated_at", "payment_method_id", "card_number", "owner")
|
|
select substring(replace(gen_random_uuid()::text, '-', ''), 1, 26), now(), now(), "id", "card_number3", "card_owner3"
|
|
from "payment_methods"
|
|
where "card_number3" is not null and "card_owner3" is not null;
|
|
`);
|
|
|
|
this.addSql(`
|
|
alter table "payment_methods"
|
|
drop column "card_number",
|
|
drop column "card_number2",
|
|
drop column "card_number3",
|
|
drop column "card_owner",
|
|
drop column "card_owner2",
|
|
drop column "card_owner3";
|
|
`);
|
|
}
|
|
|
|
override async down(): Promise<void> {
|
|
this.addSql(`
|
|
alter table "payment_methods"
|
|
add column "card_number" varchar(255) null,
|
|
add column "card_number2" varchar(255) null,
|
|
add column "card_number3" varchar(255) null,
|
|
add column "card_owner" varchar(255) null,
|
|
add column "card_owner2" varchar(255) null,
|
|
add column "card_owner3" varchar(255) null;
|
|
`);
|
|
|
|
this.addSql(`
|
|
update "payment_methods" pm
|
|
set
|
|
"card_number" = c1."card_number",
|
|
"card_owner" = c1."owner",
|
|
"card_number2" = c2."card_number",
|
|
"card_owner2" = c2."owner",
|
|
"card_number3" = c3."card_number",
|
|
"card_owner3" = c3."owner"
|
|
from (
|
|
select distinct on ("payment_method_id") *
|
|
from "payment_method_cards"
|
|
order by "payment_method_id", "created_at"
|
|
) c1
|
|
left join lateral (
|
|
select *
|
|
from "payment_method_cards"
|
|
where "payment_method_id" = c1."payment_method_id"
|
|
order by "created_at"
|
|
offset 1 limit 1
|
|
) c2 on true
|
|
left join lateral (
|
|
select *
|
|
from "payment_method_cards"
|
|
where "payment_method_id" = c1."payment_method_id"
|
|
order by "created_at"
|
|
offset 2 limit 1
|
|
) c3 on true
|
|
where pm."id" = c1."payment_method_id";
|
|
`);
|
|
|
|
this.addSql(`drop table if exists "payment_method_cards" cascade;`);
|
|
}
|
|
|
|
}
|