Rename
This commit is contained in:
parent
e12fc874c8
commit
a8070829c9
|
@ -4,7 +4,12 @@ import { MIGRATIONS } from "./migrations"
|
|||
|
||||
export * from "./appMigrationMetadata"
|
||||
|
||||
export const latestMigration = MIGRATIONS.map(m => m.migrationId)
|
||||
export type AppMigration = {
|
||||
id: string
|
||||
func: () => Promise<void>
|
||||
}
|
||||
|
||||
export const latestMigration = MIGRATIONS.map(m => m.id)
|
||||
.sort()
|
||||
.reverse()[0]
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
// This file should never be manually modified, use `yarn add-app-migration` in order to add a new one
|
||||
|
||||
export const MIGRATIONS: {
|
||||
migrationId: string
|
||||
migrationFunc: () => Promise<void>
|
||||
}[] = [
|
||||
import { AppMigration } from "."
|
||||
|
||||
export const MIGRATIONS: AppMigration[] = [
|
||||
// Migrations will be executed sorted by migrationId
|
||||
]
|
||||
|
|
|
@ -5,13 +5,11 @@ import {
|
|||
getAppMigrationVersion,
|
||||
updateAppMigrationMetadata,
|
||||
} from "./appMigrationMetadata"
|
||||
import { AppMigration } from "."
|
||||
|
||||
export async function processMigrations(
|
||||
appId: string,
|
||||
migrations: {
|
||||
migrationId: string
|
||||
migrationFunc: () => Promise<void>
|
||||
}[]
|
||||
migrations: AppMigration[]
|
||||
) {
|
||||
console.log(`Processing app migration for "${appId}"`)
|
||||
|
||||
|
@ -26,31 +24,31 @@ export async function processMigrations(
|
|||
let currentVersion = await getAppMigrationVersion(appId)
|
||||
|
||||
const pendingMigrations = migrations
|
||||
.filter(m => m.migrationId > currentVersion)
|
||||
.sort((a, b) => a.migrationId.localeCompare(b.migrationId))
|
||||
.filter(m => m.id > currentVersion)
|
||||
.sort((a, b) => a.id.localeCompare(b.id))
|
||||
|
||||
const migrationIds = migrations.map(m => m.migrationId).sort()
|
||||
const migrationIds = migrations.map(m => m.id).sort()
|
||||
|
||||
let index = 0
|
||||
for (const { migrationId, migrationFunc } of pendingMigrations) {
|
||||
for (const { id, func } of pendingMigrations) {
|
||||
const expectedMigration =
|
||||
migrationIds[migrationIds.indexOf(currentVersion) + 1]
|
||||
|
||||
if (expectedMigration !== migrationId) {
|
||||
throw `Migration ${migrationId} could not run, update for "${migrationId}" is running but ${expectedMigration} is expected`
|
||||
if (expectedMigration !== id) {
|
||||
throw `Migration ${id} could not run, update for "${id}" is running but ${expectedMigration} is expected`
|
||||
}
|
||||
|
||||
const counter = `(${++index}/${pendingMigrations.length})`
|
||||
console.info(`Running migration ${migrationId}... ${counter}`, {
|
||||
migrationId,
|
||||
console.info(`Running migration ${id}... ${counter}`, {
|
||||
migrationId: id,
|
||||
appId,
|
||||
})
|
||||
await migrationFunc()
|
||||
await func()
|
||||
await updateAppMigrationMetadata({
|
||||
appId,
|
||||
version: migrationId,
|
||||
version: id,
|
||||
})
|
||||
currentVersion = migrationId
|
||||
currentVersion = id
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -10,10 +10,10 @@ describe("migration", () => {
|
|||
await config.doInContext(config.getAppId(), async () => {
|
||||
const db = context.getAppDB()
|
||||
for (const migration of MIGRATIONS) {
|
||||
await migration.migrationFunc()
|
||||
await migration.func()
|
||||
const docs = await db.allDocs({ include_docs: true })
|
||||
|
||||
await migration.migrationFunc()
|
||||
await migration.func()
|
||||
const latestDocs = await db.allDocs({ include_docs: true })
|
||||
|
||||
expect(docs).toEqual(latestDocs)
|
||||
|
|
|
@ -2,16 +2,14 @@ import * as setup from "../../api/routes/tests/utilities"
|
|||
import { processMigrations } from "../migrationsProcessor"
|
||||
import { getAppMigrationVersion } from "../appMigrationMetadata"
|
||||
import { context } from "@budibase/backend-core"
|
||||
import { AppMigration } from ".."
|
||||
|
||||
describe("migrationsProcessor", () => {
|
||||
it("running migrations will update the latest applied migration", async () => {
|
||||
const testMigrations: {
|
||||
migrationId: string
|
||||
migrationFunc: () => Promise<void>
|
||||
}[] = [
|
||||
{ migrationId: "123", migrationFunc: async () => {} },
|
||||
{ migrationId: "124", migrationFunc: async () => {} },
|
||||
{ migrationId: "125", migrationFunc: async () => {} },
|
||||
const testMigrations: AppMigration[] = [
|
||||
{ id: "123", func: async () => {} },
|
||||
{ id: "124", func: async () => {} },
|
||||
{ id: "125", func: async () => {} },
|
||||
]
|
||||
|
||||
const config = setup.getConfig()
|
||||
|
@ -29,13 +27,10 @@ describe("migrationsProcessor", () => {
|
|||
})
|
||||
|
||||
it("no context can be initialised within a migration", async () => {
|
||||
const testMigrations: {
|
||||
migrationId: string
|
||||
migrationFunc: () => Promise<void>
|
||||
}[] = [
|
||||
const testMigrations: AppMigration[] = [
|
||||
{
|
||||
migrationId: "123",
|
||||
migrationFunc: async () => {
|
||||
id: "123",
|
||||
func: async () => {
|
||||
await context.doInAppMigrationContext("any", () => {})
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue