refactor: migration code

This commit is contained in:
Ben Allfree
2022-11-20 05:09:07 -08:00
parent 3c9783485d
commit 2be8fd6709
2 changed files with 7 additions and 6 deletions

View File

@@ -9,8 +9,9 @@ import {
import { backupInstance } from '../util/backupInstance'
import { error } from '../util/dbg'
import { safeCatch } from '../util/safeAsync'
import { applyDbMigrations } from './applyDbMigrations'
import { pexec } from './pexec'
import { schema } from './schema'
import { withInstance } from './withInstance'
const PB_BIN = resolve(DAEMON_PB_BIN_DIR, binFor('lollipop'))
@@ -26,7 +27,9 @@ safeCatch(`root`, async () => {
console.log(`Upgrading`)
await pexec(`${PB_BIN} upgrade --dir=pb_data`)
await applyDbMigrations(async (client) => {
await withInstance(async (client) => {
await client.applySchema(schema)
await client.updateInstances((instance) => {
const src = resolve(DAEMON_PB_DATA_DIR, instance.subdomain)
const dst = resolve(DAEMON_PB_DATA_DIR, instance.id)

View File

@@ -12,10 +12,9 @@ import { mkInternalUrl } from '../util/internal'
import { safeCatch } from '../util/safeAsync'
import { spawnInstance } from '../util/spawnInstance'
import { tryFetch } from '../util/tryFetch'
import { schema } from './schema'
export const applyDbMigrations = safeCatch(
`applyDbMigrations`,
export const withInstance = safeCatch(
`withInstance`,
async (cb: (client: PocketbaseClientApi) => Promise<void>) => {
// Add `platform` and `bin` required columns (migrate db json)
try {
@@ -30,7 +29,6 @@ export const applyDbMigrations = safeCatch(
const client = createPbClient(coreInternalUrl)
await tryFetch(coreInternalUrl)
await client.adminAuthViaEmail(DAEMON_PB_USERNAME, DAEMON_PB_PASSWORD)
await client.applySchema(schema)
await cb(client)
} catch (e) {
console.error(