Files
dmenu-api/database/migrations/Migration20260701170000_updateSmsLogsEntity.ts
T
morteza d2838f0976
deploy to danak / build_and_deploy (push) Has been cancelled
add new sms-queue
2026-07-01 20:26:00 +03:30

41 lines
1.3 KiB
TypeScript

import { Migration } from '@mikro-orm/migrations';
export class Migration20260701170000_updateSmsLogsEntity extends Migration {
override async up(): Promise<void> {
this.addSql(`alter table "sms_logs" add column "count" int not null default 0;`);
this.addSql(`
with aggregated as (
select restaurant_id, count(*)::int as total_count
from sms_logs
group by restaurant_id
)
update sms_logs sl
set count = a.total_count
from aggregated a
where sl.restaurant_id = a.restaurant_id
and sl.id = (
select min(id) from sms_logs where restaurant_id = sl.restaurant_id
);
`);
this.addSql(`
delete from sms_logs sl1
using sms_logs sl2
where sl1.restaurant_id = sl2.restaurant_id
and sl1.id > sl2.id;
`);
this.addSql(`alter table "sms_logs" drop column "phone";`);
this.addSql(`alter table "sms_logs" add constraint "sms_logs_restaurant_id_unique" unique ("restaurant_id");`);
}
override async down(): Promise<void> {
this.addSql(`alter table "sms_logs" drop constraint if exists "sms_logs_restaurant_id_unique";`);
this.addSql(`alter table "sms_logs" add column "phone" varchar(255) not null default '';`);
this.addSql(`alter table "sms_logs" drop column "count";`);
}
}