60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { Migration } from '@mikro-orm/migrations';
|
|
|
|
export class Migration20260723090000_addAiChats extends Migration {
|
|
override async up(): Promise<void> {
|
|
this.addSql(`
|
|
create table "ai_chats" (
|
|
"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,
|
|
"user_id" char(26) null,
|
|
"question" text not null,
|
|
"consumed_tokens" int not null,
|
|
"cost" numeric(10,0) not null,
|
|
constraint "ai_chats_pkey" primary key ("id")
|
|
);
|
|
`);
|
|
this.addSql(`create index "ai_chats_created_at_index" on "ai_chats" ("created_at");`);
|
|
this.addSql(`create index "ai_chats_deleted_at_index" on "ai_chats" ("deleted_at");`);
|
|
this.addSql(`create index "ai_chats_restaurant_id_index" on "ai_chats" ("restaurant_id");`);
|
|
this.addSql(`
|
|
alter table "ai_chats"
|
|
add constraint "ai_chats_restaurant_id_foreign"
|
|
foreign key ("restaurant_id") references "restaurants" ("id") on update cascade;
|
|
`);
|
|
this.addSql(`
|
|
alter table "ai_chats"
|
|
add constraint "ai_chats_user_id_foreign"
|
|
foreign key ("user_id") references "users" ("id") on update cascade on delete set null;
|
|
`);
|
|
|
|
this.addSql(`
|
|
alter table "restaurant_credit_transactions"
|
|
drop constraint if exists "restaurant_credit_transactions_reason_check";
|
|
`);
|
|
this.addSql(`
|
|
alter table "restaurant_credit_transactions"
|
|
add constraint "restaurant_credit_transactions_reason_check"
|
|
check ("reason" in ('sms_send', 'deposit', 'adjustment', 'ai_chat'));
|
|
`);
|
|
}
|
|
|
|
override async down(): Promise<void> {
|
|
this.addSql(`
|
|
alter table "restaurant_credit_transactions"
|
|
drop constraint if exists "restaurant_credit_transactions_reason_check";
|
|
`);
|
|
this.addSql(`
|
|
alter table "restaurant_credit_transactions"
|
|
add constraint "restaurant_credit_transactions_reason_check"
|
|
check ("reason" in ('sms_send', 'deposit', 'adjustment'));
|
|
`);
|
|
|
|
this.addSql(`alter table "ai_chats" drop constraint if exists "ai_chats_user_id_foreign";`);
|
|
this.addSql(`alter table "ai_chats" drop constraint if exists "ai_chats_restaurant_id_foreign";`);
|
|
this.addSql(`drop table if exists "ai_chats" cascade;`);
|
|
}
|
|
}
|