Skip to content

Commit fe64a3a

Browse files
authored
fix: run migrations after tenant creation (#506)
1 parent 72d4a17 commit fe64a3a

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed

src/database/migrations/migrate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const backportMigrations = [
4040

4141
export const progressiveMigrations = new ProgressiveMigrations({
4242
maxSize: 200,
43-
interval: 1000 * 60, // 1m
43+
interval: 1000 * 5, // 5s
4444
watch: pgQueueEnable,
4545
})
4646

src/database/migrations/progressive.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,31 @@ export class ProgressiveMigrations {
1919
signal.addEventListener('abort', () => {
2020
if (this.watchInterval) {
2121
clearInterval(this.watchInterval)
22+
this.drain().catch((e) => {
23+
logSchema.error(logger, '[Migrations] Error creating migration jobs', {
24+
type: 'migrations',
25+
error: e,
26+
metadata: JSON.stringify({
27+
strategy: 'progressive',
28+
}),
29+
})
30+
})
2231
}
2332
})
2433
}
2534

35+
async drain() {
36+
return this.createJobs(this.tenants.length).catch((e) => {
37+
logSchema.error(logger, '[Migrations] Error creating migration jobs', {
38+
type: 'migrations',
39+
error: e,
40+
metadata: JSON.stringify({
41+
strategy: 'progressive',
42+
}),
43+
})
44+
})
45+
}
46+
2647
addTenant(tenant: string) {
2748
const tenantIndex = this.tenants.indexOf(tenant)
2849

@@ -36,7 +57,7 @@ export class ProgressiveMigrations {
3657
return
3758
}
3859

39-
this.createJobs().catch((e) => {
60+
this.createJobs(this.options.maxSize).catch((e) => {
4061
logSchema.error(logger, '[Migrations] Error creating migration jobs', {
4162
type: 'migrations',
4263
error: e,
@@ -56,7 +77,7 @@ export class ProgressiveMigrations {
5677
return
5778
}
5879

59-
this.createJobs().catch((e) => {
80+
this.createJobs(this.options.maxSize).catch((e) => {
6081
logSchema.error(logger, '[Migrations] Error creating migration jobs', {
6182
type: 'migrations',
6283
error: e,
@@ -68,9 +89,9 @@ export class ProgressiveMigrations {
6889
}, this.options.interval)
6990
}
7091

71-
protected async createJobs() {
92+
protected async createJobs(maxJobs: number) {
7293
this.emittingJobs = true
73-
const tenantsBatch = this.tenants.splice(0, this.options.maxSize)
94+
const tenantsBatch = this.tenants.splice(0, maxJobs)
7495
const jobs = await Promise.allSettled(
7596
tenantsBatch.map(async (tenant) => {
7697
const tenantConfig = await getTenantConfig(tenant)

src/http/routes/admin/tenants.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
multitenantKnex,
99
lastMigrationName,
1010
runMigrationsOnTenant,
11+
progressiveMigrations,
1112
} from '../../../database'
1213
import { dbSuperUser, storage } from '../../plugins'
1314

@@ -179,7 +180,6 @@ export default async function routes(fastify: FastifyInstance) {
179180
tracingMode,
180181
} = request.body
181182

182-
await runMigrationsOnTenant(databaseUrl, tenantId)
183183
await multitenantKnex('tenants').insert({
184184
id: tenantId,
185185
anon_key: encrypt(anonKey),
@@ -191,10 +191,23 @@ export default async function routes(fastify: FastifyInstance) {
191191
jwks,
192192
service_key: encrypt(serviceKey),
193193
feature_image_transformation: features?.imageTransformation?.enabled ?? false,
194-
migrations_version: await lastMigrationName(),
195-
migrations_status: TenantMigrationStatus.COMPLETED,
194+
migrations_version: null,
195+
migrations_status: null,
196196
tracing_mode: tracingMode,
197197
})
198+
199+
try {
200+
await runMigrationsOnTenant(databaseUrl, tenantId)
201+
await multitenantKnex('tenants')
202+
.where('id', tenantId)
203+
.update({
204+
migrations_version: await lastMigrationName(),
205+
migrations_status: TenantMigrationStatus.COMPLETED,
206+
})
207+
} catch (e) {
208+
progressiveMigrations.addTenant(tenantId)
209+
}
210+
198211
reply.code(201).send()
199212
})
200213

@@ -215,9 +228,7 @@ export default async function routes(fastify: FastifyInstance) {
215228
tracingMode,
216229
} = request.body
217230
const { tenantId } = request.params
218-
if (databaseUrl) {
219-
await runMigrationsOnTenant(databaseUrl, tenantId)
220-
}
231+
221232
await multitenantKnex('tenants')
222233
.update({
223234
anon_key: anonKey !== undefined ? encrypt(anonKey) : undefined,
@@ -233,11 +244,24 @@ export default async function routes(fastify: FastifyInstance) {
233244
jwks,
234245
service_key: serviceKey !== undefined ? encrypt(serviceKey) : undefined,
235246
feature_image_transformation: features?.imageTransformation?.enabled,
236-
migrations_version: databaseUrl ? await lastMigrationName() : undefined,
237-
migrations_status: databaseUrl ? TenantMigrationStatus.COMPLETED : undefined,
238247
tracing_mode: tracingMode,
239248
})
240249
.where('id', tenantId)
250+
251+
if (databaseUrl) {
252+
try {
253+
await runMigrationsOnTenant(databaseUrl, tenantId)
254+
await multitenantKnex('tenants')
255+
.where('id', tenantId)
256+
.update({
257+
migrations_version: await lastMigrationName(),
258+
migrations_status: TenantMigrationStatus.COMPLETED,
259+
})
260+
} catch (e) {
261+
progressiveMigrations.addTenant(tenantId)
262+
}
263+
}
264+
241265
reply.code(204).send()
242266
}
243267
)
@@ -256,11 +280,8 @@ export default async function routes(fastify: FastifyInstance) {
256280
tracingMode,
257281
} = request.body
258282
const { tenantId } = request.params
259-
await runMigrationsOnTenant(databaseUrl, tenantId)
260283

261284
const tenantInfo: tenantDBInterface & {
262-
migrations_version: string
263-
migrations_status: TenantMigrationStatus
264285
tracing_mode?: string
265286
} = {
266287
id: tenantId,
@@ -269,8 +290,6 @@ export default async function routes(fastify: FastifyInstance) {
269290
jwt_secret: encrypt(jwtSecret),
270291
jwks: jwks || null,
271292
service_key: encrypt(serviceKey),
272-
migrations_version: await lastMigrationName(),
273-
migrations_status: TenantMigrationStatus.COMPLETED,
274293
}
275294

276295
if (fileSizeLimit) {
@@ -294,6 +313,19 @@ export default async function routes(fastify: FastifyInstance) {
294313
}
295314

296315
await multitenantKnex('tenants').insert(tenantInfo).onConflict('id').merge()
316+
317+
try {
318+
await runMigrationsOnTenant(databaseUrl, tenantId)
319+
await multitenantKnex('tenants')
320+
.where('id', tenantId)
321+
.update({
322+
migrations_version: await lastMigrationName(),
323+
migrations_status: TenantMigrationStatus.COMPLETED,
324+
})
325+
} catch (e) {
326+
progressiveMigrations.addTenant(tenantId)
327+
}
328+
297329
reply.code(204).send()
298330
})
299331

0 commit comments

Comments
 (0)