8
8
multitenantKnex ,
9
9
lastMigrationName ,
10
10
runMigrationsOnTenant ,
11
+ progressiveMigrations ,
11
12
} from '../../../database'
12
13
import { dbSuperUser , storage } from '../../plugins'
13
14
@@ -179,7 +180,6 @@ export default async function routes(fastify: FastifyInstance) {
179
180
tracingMode,
180
181
} = request . body
181
182
182
- await runMigrationsOnTenant ( databaseUrl , tenantId )
183
183
await multitenantKnex ( 'tenants' ) . insert ( {
184
184
id : tenantId ,
185
185
anon_key : encrypt ( anonKey ) ,
@@ -191,10 +191,23 @@ export default async function routes(fastify: FastifyInstance) {
191
191
jwks,
192
192
service_key : encrypt ( serviceKey ) ,
193
193
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 ,
196
196
tracing_mode : tracingMode ,
197
197
} )
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
+
198
211
reply . code ( 201 ) . send ( )
199
212
} )
200
213
@@ -215,9 +228,7 @@ export default async function routes(fastify: FastifyInstance) {
215
228
tracingMode,
216
229
} = request . body
217
230
const { tenantId } = request . params
218
- if ( databaseUrl ) {
219
- await runMigrationsOnTenant ( databaseUrl , tenantId )
220
- }
231
+
221
232
await multitenantKnex ( 'tenants' )
222
233
. update ( {
223
234
anon_key : anonKey !== undefined ? encrypt ( anonKey ) : undefined ,
@@ -233,11 +244,24 @@ export default async function routes(fastify: FastifyInstance) {
233
244
jwks,
234
245
service_key : serviceKey !== undefined ? encrypt ( serviceKey ) : undefined ,
235
246
feature_image_transformation : features ?. imageTransformation ?. enabled ,
236
- migrations_version : databaseUrl ? await lastMigrationName ( ) : undefined ,
237
- migrations_status : databaseUrl ? TenantMigrationStatus . COMPLETED : undefined ,
238
247
tracing_mode : tracingMode ,
239
248
} )
240
249
. 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
+
241
265
reply . code ( 204 ) . send ( )
242
266
}
243
267
)
@@ -256,11 +280,8 @@ export default async function routes(fastify: FastifyInstance) {
256
280
tracingMode,
257
281
} = request . body
258
282
const { tenantId } = request . params
259
- await runMigrationsOnTenant ( databaseUrl , tenantId )
260
283
261
284
const tenantInfo : tenantDBInterface & {
262
- migrations_version : string
263
- migrations_status : TenantMigrationStatus
264
285
tracing_mode ?: string
265
286
} = {
266
287
id : tenantId ,
@@ -269,8 +290,6 @@ export default async function routes(fastify: FastifyInstance) {
269
290
jwt_secret : encrypt ( jwtSecret ) ,
270
291
jwks : jwks || null ,
271
292
service_key : encrypt ( serviceKey ) ,
272
- migrations_version : await lastMigrationName ( ) ,
273
- migrations_status : TenantMigrationStatus . COMPLETED ,
274
293
}
275
294
276
295
if ( fileSizeLimit ) {
@@ -294,6 +313,19 @@ export default async function routes(fastify: FastifyInstance) {
294
313
}
295
314
296
315
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
+
297
329
reply . code ( 204 ) . send ( )
298
330
} )
299
331
0 commit comments