39 lines
1.9 KiB
TypeScript
39 lines
1.9 KiB
TypeScript
import { Migration } from '@mikro-orm/migrations';
|
|
|
|
export class Migration20260701180000_addRestaurantCredit extends Migration {
|
|
|
|
override async up(): Promise<void> {
|
|
this.addSql(`alter table "restaurants" add column "credit" numeric(10,0) not null default 0;`);
|
|
|
|
this.addSql(`
|
|
create table "restaurant_credit_transactions" (
|
|
"id" char(26) not null,
|
|
"created_at" timestamptz not null default now(),
|
|
"updated_at" timestamptz not null default now(),
|
|
"deleted_at" timestamptz null,
|
|
"restaurant_id" char(26) not null,
|
|
"amount" numeric(10,0) not null,
|
|
"balance" numeric(10,0) not null,
|
|
"type" text check ("type" in ('credit', 'debit')) not null,
|
|
"reason" text check ("reason" in ('sms_send', 'deposit', 'adjustment')) not null,
|
|
constraint "restaurant_credit_transactions_pkey" primary key ("id")
|
|
);
|
|
`);
|
|
this.addSql(`create index "restaurant_credit_transactions_created_at_index" on "restaurant_credit_transactions" ("created_at");`);
|
|
this.addSql(`create index "restaurant_credit_transactions_deleted_at_index" on "restaurant_credit_transactions" ("deleted_at");`);
|
|
this.addSql(`create index "restaurant_credit_transactions_restaurant_id_index" on "restaurant_credit_transactions" ("restaurant_id");`);
|
|
this.addSql(`
|
|
alter table "restaurant_credit_transactions"
|
|
add constraint "restaurant_credit_transactions_restaurant_id_foreign"
|
|
foreign key ("restaurant_id") references "restaurants" ("id") on update cascade;
|
|
`);
|
|
}
|
|
|
|
override async down(): Promise<void> {
|
|
this.addSql(`alter table "restaurant_credit_transactions" drop constraint if exists "restaurant_credit_transactions_restaurant_id_foreign";`);
|
|
this.addSql(`drop table if exists "restaurant_credit_transactions" cascade;`);
|
|
this.addSql(`alter table "restaurants" drop column if exists "credit";`);
|
|
}
|
|
|
|
}
|