Skip to content

Commit 02826bb

Browse files
thebengeuinian
authored andcommitted
fix: do not cache tenant config on migration error
1 parent 4d6e163 commit 02826bb

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/utils/migrate.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { getConfig } from './config'
44

55
const { multitenantDatabaseUrl } = getConfig()
66

7-
async function connectAndMigrate(
8-
databaseUrl: string | undefined,
9-
migrationsDirectory: string,
10-
logOnError = false
11-
) {
7+
async function connectAndMigrate(databaseUrl: string | undefined, migrationsDirectory: string) {
128
const dbConfig = {
139
connectionString: databaseUrl,
1410
connectionTimeoutMillis: 1000,
@@ -17,12 +13,6 @@ async function connectAndMigrate(
1713
try {
1814
await client.connect()
1915
await migrate({ client }, migrationsDirectory)
20-
} catch (error) {
21-
if (logOnError) {
22-
console.error('Migration error:', error.message)
23-
} else {
24-
throw error
25-
}
2616
} finally {
2717
await client.end()
2818
}
@@ -41,5 +31,5 @@ export async function runMultitenantMigrations(): Promise<void> {
4131
}
4232

4333
export async function runMigrationsOnTenant(databaseUrl: string): Promise<void> {
44-
await connectAndMigrate(databaseUrl, './migrations/tenant', true)
34+
await connectAndMigrate(databaseUrl, './migrations/tenant')
4535
}

src/utils/tenant.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@ const tenantConfigCache = new Map<string, TenantConfig>()
1414

1515
export async function cacheTenantConfigAndRunMigrations(
1616
tenantId: string,
17-
config: TenantConfig
17+
config: TenantConfig,
18+
logOnError = false
1819
): Promise<void> {
19-
await runMigrationsOnTenant(config.databaseUrl)
20+
try {
21+
await runMigrationsOnTenant(config.databaseUrl)
22+
} catch (error) {
23+
if (logOnError) {
24+
console.error('Migration error:', error.message)
25+
return
26+
} else {
27+
throw error
28+
}
29+
}
2030
tenantConfigCache.set(tenantId, config)
2131
}
2232

@@ -30,12 +40,16 @@ export async function cacheTenantConfigsFromDbAndRunMigrations(): Promise<void>
3040
await Promise.all(
3141
tenants.map(({ id, anon_key, database_url, jwt_secret, service_key }) =>
3242
limit(() =>
33-
cacheTenantConfigAndRunMigrations(id, {
34-
anonKey: decrypt(anon_key),
35-
databaseUrl: decrypt(database_url),
36-
jwtSecret: decrypt(jwt_secret),
37-
serviceKey: decrypt(service_key),
38-
})
43+
cacheTenantConfigAndRunMigrations(
44+
id,
45+
{
46+
anonKey: decrypt(anon_key),
47+
databaseUrl: decrypt(database_url),
48+
jwtSecret: decrypt(jwt_secret),
49+
serviceKey: decrypt(service_key),
50+
},
51+
true
52+
)
3953
)
4054
)
4155
)

0 commit comments

Comments
 (0)