1
0
Fork 0
mirror of synced 2024-06-30 20:10:54 +12:00
budibase/packages/server/src/migrations/index.ts

119 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-09-28 20:56:45 +13:00
import { migrations, redis } from "@budibase/backend-core"
import { Migration, MigrationOptions, MigrationName } from "@budibase/types"
import env from "../environment"
2022-01-24 23:48:59 +13:00
// migration functions
import * as userEmailViewCasing from "./functions/userEmailViewCasing"
2022-09-29 01:13:37 +13:00
import * as syncQuotas from "./functions/syncQuotas"
2022-01-27 23:40:31 +13:00
import * as appUrls from "./functions/appUrls"
2022-09-28 20:56:45 +13:00
import * as backfill from "./functions/backfill"
2022-01-24 23:48:59 +13:00
/**
2022-09-28 20:56:45 +13:00
* Populate the migration function and additional configuration from
* the static migration definitions.
2022-01-24 23:48:59 +13:00
*/
2022-09-28 20:56:45 +13:00
export const buildMigrations = () => {
const definitions = migrations.DEFINITIONS
const serverMigrations: Migration[] = []
for (const definition of definitions) {
switch (definition.name) {
case MigrationName.USER_EMAIL_VIEW_CASING: {
serverMigrations.push({
...definition,
fn: userEmailViewCasing.run,
})
break
}
2022-09-29 01:13:37 +13:00
case MigrationName.SYNC_QUOTAS: {
2022-09-28 20:56:45 +13:00
serverMigrations.push({
...definition,
2022-09-29 01:13:37 +13:00
fn: syncQuotas.run,
2022-09-28 20:56:45 +13:00
})
break
}
case MigrationName.APP_URLS: {
serverMigrations.push({
...definition,
appOpts: { all: true },
fn: appUrls.run,
})
break
}
case MigrationName.EVENT_APP_BACKFILL: {
serverMigrations.push({
...definition,
appOpts: { all: true },
fn: backfill.app.run,
silent: !!env.SELF_HOSTED, // reduce noisy logging
preventRetry: !!env.SELF_HOSTED, // only ever run once
})
break
}
case MigrationName.EVENT_GLOBAL_BACKFILL: {
serverMigrations.push({
...definition,
fn: backfill.global.run,
silent: !!env.SELF_HOSTED, // reduce noisy logging
preventRetry: !!env.SELF_HOSTED, // only ever run once
})
break
}
case MigrationName.EVENT_INSTALLATION_BACKFILL: {
serverMigrations.push({
...definition,
fn: backfill.installation.run,
silent: !!env.SELF_HOSTED, // reduce noisy logging
preventRetry: !!env.SELF_HOSTED, // only ever run once
})
break
}
}
2022-01-24 23:48:59 +13:00
}
2022-09-28 20:56:45 +13:00
return serverMigrations
2022-01-24 23:48:59 +13:00
}
2022-09-28 20:56:45 +13:00
export const MIGRATIONS = buildMigrations()
2022-01-24 23:48:59 +13:00
export const migrate = async (options?: MigrationOptions) => {
2022-09-28 20:56:45 +13:00
if (env.SELF_HOSTED) {
// self host runs migrations on startup
// make sure only a single instance runs them
await migrateWithLock(options)
} else {
await migrations.runMigrations(MIGRATIONS, options)
}
}
const migrateWithLock = async (options?: MigrationOptions) => {
// get a new lock client
const redlock = await redis.clients.getMigrationsRedlock()
// lock for 15 minutes
const ttl = 1000 * 60 * 15
let migrationLock
// acquire lock
try {
migrationLock = await redlock.lock("migrations", ttl)
} catch (e: any) {
if (e.name === "LockError") {
return
} else {
throw e
}
}
// run migrations
try {
await migrations.runMigrations(MIGRATIONS, options)
} finally {
// release lock
try {
await migrationLock.unlock()
} catch (e) {
console.error("unable to release migration lock")
}
}
2022-01-24 23:48:59 +13:00
}