Files
dmenu-api/database/migrations/Migration20260714120000_addCashShifts.ts
2026-07-14 12:57:30 +03:30

62 lines
2.8 KiB
TypeScript

import { Migration } from '@mikro-orm/migrations';
export class Migration20260714120000_addCashShifts extends Migration {
override async up(): Promise<void> {
this.addSql(`
create table "cash_shifts" (
"id" char(26) not null,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
"deleted_at" timestamptz null,
"admin_id" char(26) not null,
"restaurant_id" char(26) not null,
"opened_at" timestamptz not null,
"closed_at" timestamptz null,
"opening_amount" numeric(10,0) not null default 0,
"expected_amount" numeric(10,0) null,
"counted_amount" numeric(10,0) null,
"difference_amount" numeric(10,0) null,
"orders" int not null default 0,
"status" text check ("status" in ('open', 'closed')) not null default 'open',
"notes" text null,
constraint "cash_shifts_pkey" primary key ("id")
);
`);
this.addSql(`create index "cash_shifts_created_at_index" on "cash_shifts" ("created_at");`);
this.addSql(`create index "cash_shifts_deleted_at_index" on "cash_shifts" ("deleted_at");`);
this.addSql(`create index "cash_shifts_admin_id_index" on "cash_shifts" ("admin_id");`);
this.addSql(`create index "cash_shifts_restaurant_id_index" on "cash_shifts" ("restaurant_id");`);
this.addSql(`create index "cash_shifts_restaurant_id_status_index" on "cash_shifts" ("restaurant_id", "status");`);
this.addSql(`
alter table "cash_shifts"
add constraint "cash_shifts_admin_id_foreign"
foreign key ("admin_id") references "admins" ("id") on update cascade;
`);
this.addSql(`
alter table "cash_shifts"
add constraint "cash_shifts_restaurant_id_foreign"
foreign key ("restaurant_id") references "restaurants" ("id") on update cascade;
`);
this.addSql(`alter table "orders" add column "cash_shift_id" char(26) null;`);
this.addSql(`create index "orders_cash_shift_id_index" on "orders" ("cash_shift_id");`);
this.addSql(`
alter table "orders"
add constraint "orders_cash_shift_id_foreign"
foreign key ("cash_shift_id") references "cash_shifts" ("id") on update cascade on delete set null;
`);
}
override async down(): Promise<void> {
this.addSql(`alter table "orders" drop constraint if exists "orders_cash_shift_id_foreign";`);
this.addSql(`drop index if exists "orders_cash_shift_id_index";`);
this.addSql(`alter table "orders" drop column if exists "cash_shift_id";`);
this.addSql(`alter table "cash_shifts" drop constraint if exists "cash_shifts_restaurant_id_foreign";`);
this.addSql(`alter table "cash_shifts" drop constraint if exists "cash_shifts_admin_id_foreign";`);
this.addSql(`drop table if exists "cash_shifts" cascade;`);
}
}