Skip to content

Commit

Permalink
fix: include tenant ID in migration error message (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebengeu authored Apr 5, 2022
1 parent 0728cd0 commit 9d94b4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/routes/tenant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ export default async function routes(fastify: FastifyInstance) {

fastify.post<tenantRequestInterface>('/:tenantId', { schema }, async (request, reply) => {
const { anonKey, databaseUrl, fileSizeLimit, jwtSecret, serviceKey } = request.body
await runMigrations(databaseUrl)
const { tenantId } = request.params
await runMigrations(tenantId, databaseUrl)
await knex('tenants').insert({
id: request.params.tenantId,
id: tenantId,
anon_key: encrypt(anonKey),
database_url: encrypt(databaseUrl),
file_size_limit: fileSizeLimit,
Expand All @@ -92,8 +93,9 @@ export default async function routes(fastify: FastifyInstance) {
{ schema: patchSchema },
async (request, reply) => {
const { anonKey, databaseUrl, fileSizeLimit, jwtSecret, serviceKey } = request.body
const { tenantId } = request.params
if (databaseUrl) {
await runMigrations(databaseUrl)
await runMigrations(tenantId, databaseUrl)
}
await knex('tenants')
.update({
Expand All @@ -103,17 +105,18 @@ export default async function routes(fastify: FastifyInstance) {
jwt_secret: jwtSecret !== undefined ? encrypt(jwtSecret) : undefined,
service_key: serviceKey !== undefined ? encrypt(serviceKey) : undefined,
})
.where('id', request.params.tenantId)
.where('id', tenantId)
reply.code(204).send()
}
)

fastify.put<tenantRequestInterface>('/:tenantId', { schema }, async (request, reply) => {
const { anonKey, databaseUrl, fileSizeLimit, jwtSecret, serviceKey } = request.body
await runMigrations(databaseUrl)
const { tenantId } = request.params
await runMigrations(tenantId, databaseUrl)
await knex('tenants')
.insert({
id: request.params.tenantId,
id: tenantId,
anon_key: encrypt(anonKey),
database_url: encrypt(databaseUrl),
file_size_limit: fileSizeLimit,
Expand Down
10 changes: 7 additions & 3 deletions src/utils/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ const { multitenantDatabaseUrl } = getConfig()

const tenantConfigCache = new Map<string, TenantConfig>()

export async function runMigrations(databaseUrl: string, logOnError = false): Promise<void> {
export async function runMigrations(
tenantId: string,
databaseUrl: string,
logOnError = false
): Promise<void> {
try {
await runMigrationsOnTenant(databaseUrl)
} catch (error: any) {
if (logOnError) {
console.error('Migration error:', error.message)
console.error(`${tenantId} migration error:`, error.message)
return
} else {
throw error
Expand All @@ -35,7 +39,7 @@ export async function cacheTenantConfigAndRunMigrations(
config: TenantConfig,
logOnError = false
): Promise<void> {
await runMigrations(config.databaseUrl, logOnError)
await runMigrations(tenantId, config.databaseUrl, logOnError)
tenantConfigCache.set(tenantId, config)
}

Expand Down

0 comments on commit 9d94b4b

Please sign in to comment.