mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(schemas): fix init account center alter run in chunks (#6823)
This commit is contained in:
parent
7ebef18e36
commit
80ae0bb6c7
1 changed files with 27 additions and 9 deletions
|
@ -4,16 +4,34 @@ import type { AlterationScript } from '../lib/types/alteration.js';
|
|||
|
||||
const alteration: AlterationScript = {
|
||||
up: async (pool) => {
|
||||
const tenants = await pool.many<{ id: string }>(sql`
|
||||
select id from tenants;
|
||||
`);
|
||||
const values = tenants.map((tenant) => sql`(${tenant.id}, 'default')`);
|
||||
// Process in chunks of 1000 tenants
|
||||
const batchSize = 1000;
|
||||
// eslint-disable-next-line @silverhand/fp/no-let
|
||||
let offset = 0;
|
||||
|
||||
// Other fileds have default values so we don't need to set them here
|
||||
await pool.query(sql`
|
||||
insert into account_centers (tenant_id, id)
|
||||
values ${sql.join(values, sql`, `)};
|
||||
`);
|
||||
// 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
|
||||
order by created_at asc
|
||||
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)
|
||||
values ${sql.join(values, sql`, `)};
|
||||
`);
|
||||
|
||||
// eslint-disable-next-line @silverhand/fp/no-mutation
|
||||
offset += batchSize;
|
||||
}
|
||||
},
|
||||
down: async (pool) => {
|
||||
await pool.query(sql`
|
||||
|
|
Loading…
Reference in a new issue