Skip to content

Commit

Permalink
fix: run migrations when tenant config is created or updated (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebengeu authored Mar 3, 2022
1 parent fade3d0 commit 1e55aa3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
29 changes: 18 additions & 11 deletions src/routes/tenant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FromSchema } from 'json-schema-to-ts'
import apiKey from '../../plugins/apikey'
import { decrypt, encrypt } from '../../utils/crypto'
import { knex } from '../../utils/multitenant-db'
import { deleteTenantConfig } from '../../utils/tenant'
import { deleteTenantConfig, runMigrations } from '../../utils/tenant'

const patchSchema = {
body: {
Expand Down Expand Up @@ -74,13 +74,15 @@ 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)
await knex('tenants').insert({
id: request.params.tenantId,
anon_key: encrypt(request.body.anonKey),
database_url: encrypt(request.body.databaseUrl),
file_size_limit: request.body.fileSizeLimit,
jwt_secret: encrypt(request.body.jwtSecret),
service_key: encrypt(request.body.serviceKey),
anon_key: encrypt(anonKey),
database_url: encrypt(databaseUrl),
file_size_limit: fileSizeLimit,
jwt_secret: encrypt(jwtSecret),
service_key: encrypt(serviceKey),
})
reply.code(201).send()
})
Expand All @@ -90,6 +92,9 @@ export default async function routes(fastify: FastifyInstance) {
{ schema: patchSchema },
async (request, reply) => {
const { anonKey, databaseUrl, fileSizeLimit, jwtSecret, serviceKey } = request.body
if (databaseUrl) {
await runMigrations(databaseUrl)
}
await knex('tenants')
.update({
anon_key: anonKey !== undefined ? encrypt(anonKey) : undefined,
Expand All @@ -104,14 +109,16 @@ export default async function routes(fastify: FastifyInstance) {
)

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

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

export async function cacheTenantConfigAndRunMigrations(
tenantId: string,
config: TenantConfig,
logOnError = false
): Promise<void> {
export async function runMigrations(databaseUrl: string, logOnError = false): Promise<void> {
try {
await runMigrationsOnTenant(config.databaseUrl)
await runMigrationsOnTenant(databaseUrl)
} catch (error: any) {
if (logOnError) {
console.error('Migration error:', error.message)
Expand All @@ -32,6 +28,14 @@ export async function cacheTenantConfigAndRunMigrations(
throw error
}
}
}

export async function cacheTenantConfigAndRunMigrations(
tenantId: string,
config: TenantConfig,
logOnError = false
): Promise<void> {
await runMigrations(config.databaseUrl, logOnError)
tenantConfigCache.set(tenantId, config)
}

Expand Down

0 comments on commit 1e55aa3

Please sign in to comment.