2024-11-08 19:57:43 +08:00
|
|
|
import { sql } from '@silverhand/slonik';
|
|
|
|
|
|
|
|
import type { AlterationScript } from '../lib/types/alteration.js';
|
|
|
|
|
|
|
|
const alteration: AlterationScript = {
|
|
|
|
up: async (pool) => {
|
2024-11-20 13:28:49 +08:00
|
|
|
// Process in chunks of 1000 tenants
|
|
|
|
const batchSize = 1000;
|
|
|
|
// eslint-disable-next-line @silverhand/fp/no-let
|
|
|
|
let offset = 0;
|
2024-11-08 19:57:43 +08:00
|
|
|
|
2024-11-20 13:28:49 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
|
|
|
|
while (true) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const tenants = await pool.any<{ id: string }>(sql`
|
|
|
|
select id from tenants
|
2024-11-21 10:36:00 +08:00
|
|
|
order by created_at asc, id asc
|
2024-11-20 13:28:49 +08:00
|
|
|
limit ${batchSize} offset ${offset};
|
|
|
|
`);
|
|
|
|
|
|
|
|
if (tenants.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const values = tenants.map((tenant) => sql`(${tenant.id}, 'default')`);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await pool.query(sql`
|
|
|
|
insert into account_centers (tenant_id, id)
|
2024-11-20 16:12:00 +08:00
|
|
|
values ${sql.join(values, sql`, `)}
|
2024-11-20 13:28:49 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
|
|
offset += batchSize;
|
|
|
|
}
|
2024-11-08 19:57:43 +08:00
|
|
|
},
|
|
|
|
down: async (pool) => {
|
|
|
|
await pool.query(sql`
|
|
|
|
delete from account_centers where id = 'default';
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default alteration;
|