1
0
Fork 0
mirror of synced 2024-09-30 00:57:16 +13:00
budibase/packages/backend-core/src/migrations/index.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-01-24 23:48:59 +13:00
const { DEFAULT_TENANT_ID } = require("../constants")
const { getDB } = require("../db")
2021-10-27 00:40:30 +13:00
const { DocumentTypes } = require("../db/constants")
2022-01-24 23:48:59 +13:00
const { getAllApps } = require("../db/utils")
const environment = require("../environment")
2022-01-27 23:40:31 +13:00
const {
doInTenant,
getTenantIds,
getGlobalDBName,
getTenantId,
} = require("../tenancy")
2021-10-27 00:40:30 +13:00
2022-01-24 23:48:59 +13:00
exports.MIGRATION_TYPES = {
GLOBAL: "global", // run once, recorded in global db, global db is provided as an argument
APP: "app", // run per app, recorded in each app db, app db is provided as an argument
2021-10-27 00:40:30 +13:00
}
2021-10-27 03:47:36 +13:00
exports.getMigrationsDoc = async db => {
2021-10-27 00:40:30 +13:00
// get the migrations doc
try {
return await db.get(DocumentTypes.MIGRATIONS)
} catch (err) {
if (err.status && err.status === 404) {
return { _id: DocumentTypes.MIGRATIONS }
}
2022-02-19 06:36:23 +13:00
console.error(err)
2021-10-27 00:40:30 +13:00
}
}
const runMigration = async (migration, options = {}) => {
2022-01-27 23:40:31 +13:00
const tenantId = getTenantId()
2022-01-24 23:48:59 +13:00
const migrationType = migration.type
const migrationName = migration.name
// get the db to store the migration in
let dbNames
if (migrationType === exports.MIGRATION_TYPES.GLOBAL) {
2022-01-27 23:40:31 +13:00
dbNames = [getGlobalDBName()]
2022-01-24 23:48:59 +13:00
} else if (migrationType === exports.MIGRATION_TYPES.APP) {
2022-02-22 20:49:33 +13:00
const apps = await getAllApps(migration.opts)
2022-01-27 23:40:31 +13:00
dbNames = apps.map(app => app.appId)
2022-01-24 23:48:59 +13:00
} else {
throw new Error(
`[Tenant: ${tenantId}] Unrecognised migration type [${migrationType}]`
)
}
// run the migration against each db
for (const dbName of dbNames) {
const db = getDB(dbName)
2022-01-24 23:48:59 +13:00
try {
const doc = await exports.getMigrationsDoc(db)
2021-10-27 03:47:36 +13:00
2022-01-24 23:48:59 +13:00
// exit if the migration has been performed already
if (doc[migrationName]) {
if (
options.force &&
options.force[migrationType] &&
options.force[migrationType].includes(migrationName)
) {
console.log(
2022-01-27 23:40:31 +13:00
`[Tenant: ${tenantId}] [Migration: ${migrationName}] [DB: ${dbName}] Forcing`
2022-01-24 23:48:59 +13:00
)
} else {
// the migration has already been performed
2022-01-29 00:32:28 +13:00
continue
2022-01-24 23:48:59 +13:00
}
}
console.log(
2022-01-27 23:40:31 +13:00
`[Tenant: ${tenantId}] [Migration: ${migrationName}] [DB: ${dbName}] Running`
2021-10-27 03:47:36 +13:00
)
2022-01-24 23:48:59 +13:00
// run the migration with tenant context
2022-01-27 23:40:31 +13:00
await migration.fn(db)
console.log(
`[Tenant: ${tenantId}] [Migration: ${migrationName}] [DB: ${dbName}] Complete`
)
2021-10-27 00:40:30 +13:00
2022-01-24 23:48:59 +13:00
// mark as complete
doc[migrationName] = Date.now()
await db.put(doc)
} catch (err) {
console.error(
2022-01-27 23:40:31 +13:00
`[Tenant: ${tenantId}] [Migration: ${migrationName}] [DB: ${dbName}] Error: `,
2022-01-24 23:48:59 +13:00
err
)
throw err
2021-10-27 00:40:30 +13:00
}
2022-01-24 23:48:59 +13:00
}
}
2021-10-27 00:40:30 +13:00
exports.runMigrations = async (migrations, options = {}) => {
2022-01-24 23:48:59 +13:00
console.log("Running migrations")
let tenantIds
if (environment.MULTI_TENANCY) {
if (!options.tenantIds || !options.tenantIds.length) {
// run for all tenants
tenantIds = await getTenantIds()
} else {
tenantIds = options.tenantIds
2022-01-24 23:48:59 +13:00
}
} else {
// single tenancy
tenantIds = [DEFAULT_TENANT_ID]
}
2021-10-27 00:40:30 +13:00
2022-01-24 23:48:59 +13:00
// for all tenants
for (const tenantId of tenantIds) {
// for all migrations
for (const migration of migrations) {
// run the migration
await doInTenant(tenantId, () => runMigration(migration, options))
2022-01-24 23:48:59 +13:00
}
2021-10-27 00:40:30 +13:00
}
2022-01-24 23:48:59 +13:00
console.log("Migrations complete")
2021-10-27 00:40:30 +13:00
}