41 lines
1.3 KiB
TypeScript
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";`);
|
|
}
|
|
|
|
}
|