forked from tsai/budibase
6 changed files with 99 additions and 6 deletions
@ -0,0 +1,65 @@ |
|||||
|
const { DocumentTypes } = require("../db/constants") |
||||
|
const { getGlobalDB } = require("../tenancy") |
||||
|
|
||||
|
exports.MIGRATION_DBS = { |
||||
|
GLOBAL_DB: "GLOBAL_DB", |
||||
|
} |
||||
|
|
||||
|
exports.MIGRATIONS = { |
||||
|
USER_EMAIL_VIEW_CASING: "user_email_view_casing", |
||||
|
} |
||||
|
|
||||
|
const DB_LOOKUP = { |
||||
|
[exports.MIGRATION_DBS.GLOBAL_DB]: [ |
||||
|
exports.MIGRATIONS.USER_EMAIL_VIEW_CASING, |
||||
|
], |
||||
|
} |
||||
|
|
||||
|
const getMigrationsDoc = async db => { |
||||
|
// get the migrations doc
|
||||
|
try { |
||||
|
return await db.get(DocumentTypes.MIGRATIONS) |
||||
|
} catch (err) { |
||||
|
if (err.status && err.status === 404) { |
||||
|
return { _id: DocumentTypes.MIGRATIONS } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => { |
||||
|
let db |
||||
|
if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) { |
||||
|
db = getGlobalDB() |
||||
|
} else { |
||||
|
throw new Error(`Unrecognised migration db [${migrationDb}]`) |
||||
|
} |
||||
|
|
||||
|
if (!DB_LOOKUP[migrationDb].includes(migrationName)) { |
||||
|
throw new Error( |
||||
|
`Unrecognised migration name [${migrationName}] for db [${migrationDb}]` |
||||
|
) |
||||
|
} |
||||
|
return tryMigrate(db, migrationName, migrateFn) |
||||
|
} |
||||
|
|
||||
|
const tryMigrate = async (db, migrationName, migrateFn) => { |
||||
|
try { |
||||
|
const doc = await getMigrationsDoc(db) |
||||
|
|
||||
|
// exit if the migration has been performed
|
||||
|
if (doc[migrationName]) { |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
console.log(`Performing migration: ${migrationName}`) |
||||
|
await migrateFn() |
||||
|
console.log(`Migration complete: ${migrationName}`) |
||||
|
|
||||
|
// mark as complete
|
||||
|
doc[migrationName] = Date.now() |
||||
|
await db.put(doc) |
||||
|
} catch (err) { |
||||
|
console.error(`Error performing migration: ${migrationName}: `, err) |
||||
|
throw err |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue