1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00
budibase/packages/server/src/migrations/index.ts

107 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-10-10 20:21:17 +13:00
import { locks, migrations } from "@budibase/backend-core"
import {
Migration,
MigrationOptions,
MigrationName,
LockType,
LockName,
} from "@budibase/types"
2022-09-28 20:56:45 +13:00
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) => {
2022-10-10 20:21:17 +13:00
await locks.doWithLock(
{
type: LockType.TRY_ONCE,
name: LockName.MIGRATIONS,
ttl: 1000 * 60 * 15, // auto expire the migration lock after 15 minutes
2022-10-26 01:37:26 +13:00
systemLock: true,
2022-10-10 20:21:17 +13:00
},
async () => {
await migrations.runMigrations(MIGRATIONS, options)
2022-09-28 20:56:45 +13:00
}
2022-10-10 20:21:17 +13:00
)
2022-01-24 23:48:59 +13:00
}