migrate with command
This commit is contained in:
parent
e7f767f650
commit
e4fdf4e197
4 changed files with 58 additions and 22 deletions
|
|
@ -6,11 +6,18 @@ import Main, { LoadMainSettingsFromEnv } from './services/main/index.js'
|
||||||
import Storage from './services/storage/index.js'
|
import Storage from './services/storage/index.js'
|
||||||
import { LoadNosrtSettingsFromEnv } from './services/nostr/index.js'
|
import { LoadNosrtSettingsFromEnv } from './services/nostr/index.js'
|
||||||
import nostrMiddleware from './nostrMiddleware.js'
|
import nostrMiddleware from './nostrMiddleware.js'
|
||||||
|
import { TypeOrmMigrationRunner } from './services/storage/migrations/runner.js';
|
||||||
|
import { getLogger } from './services/helpers/logger.js';
|
||||||
|
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
|
const log = getLogger({})
|
||||||
const mainSettings = LoadMainSettingsFromEnv()
|
const mainSettings = LoadMainSettingsFromEnv()
|
||||||
const storageManager = new Storage(mainSettings.storageSettings)
|
const storageManager = new Storage(mainSettings.storageSettings)
|
||||||
await storageManager.Connect()
|
const manualMigration = await TypeOrmMigrationRunner(log, storageManager, mainSettings.storageSettings.dbSettings, process.argv[2])
|
||||||
|
if (manualMigration) {
|
||||||
|
log("migrations run sucessfully, exiting")
|
||||||
|
return
|
||||||
|
}
|
||||||
const mainHandler = new Main(mainSettings, storageManager)
|
const mainHandler = new Main(mainSettings, storageManager)
|
||||||
await mainHandler.lnd.Warmup()
|
await mainHandler.lnd.Warmup()
|
||||||
const serverMethods = GetServerMethods(mainHandler)
|
const serverMethods = GetServerMethods(mainHandler)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import "reflect-metadata"
|
import "reflect-metadata"
|
||||||
import { DataSource } from "typeorm"
|
import { DataSource, Migration } from "typeorm"
|
||||||
import { AddressReceivingTransaction } from "./entity/AddressReceivingTransaction.js"
|
import { AddressReceivingTransaction } from "./entity/AddressReceivingTransaction.js"
|
||||||
import { User } from "./entity/User.js"
|
import { User } from "./entity/User.js"
|
||||||
import { UserReceivingAddress } from "./entity/UserReceivingAddress.js"
|
import { UserReceivingAddress } from "./entity/UserReceivingAddress.js"
|
||||||
|
|
@ -24,20 +24,16 @@ import { LndMetrics1703170330183 } from "./migrations/1703170330183-lnd_metrics.
|
||||||
export type DbSettings = {
|
export type DbSettings = {
|
||||||
databaseFile: string
|
databaseFile: string
|
||||||
migrate: boolean
|
migrate: boolean
|
||||||
doInitialMigration: boolean
|
|
||||||
}
|
}
|
||||||
export const LoadDbSettingsFromEnv = (test = false): DbSettings => {
|
export const LoadDbSettingsFromEnv = (test = false): DbSettings => {
|
||||||
return {
|
return {
|
||||||
databaseFile: test ? ":memory:" : EnvMustBeNonEmptyString("DATABASE_FILE"),
|
databaseFile: test ? ":memory:" : EnvMustBeNonEmptyString("DATABASE_FILE"),
|
||||||
migrate: process.env.MIGRATE_DB === 'true' || false,
|
migrate: process.env.MIGRATE_DB === 'true' || false,
|
||||||
doInitialMigration: process.env.DO_INITIAL_MIGRATION === 'true' || false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (settings: DbSettings) => {
|
export default async (settings: DbSettings, migrations: Function[]): Promise<{ source: DataSource, executedMigrations: Migration[] }> => {
|
||||||
const migrations = settings.doInitialMigration ? [Initial1703170309875] : []
|
const source = await new DataSource({
|
||||||
migrations.push(LndMetrics1703170330183)
|
|
||||||
const s = await new DataSource({
|
|
||||||
type: "sqlite",
|
type: "sqlite",
|
||||||
database: settings.databaseFile,
|
database: settings.databaseFile,
|
||||||
// logging: true,
|
// logging: true,
|
||||||
|
|
@ -47,18 +43,11 @@ export default async (settings: DbSettings) => {
|
||||||
migrations
|
migrations
|
||||||
}).initialize()
|
}).initialize()
|
||||||
const log = getLogger({})
|
const log = getLogger({})
|
||||||
|
const pendingMigrations = await source.showMigrations()
|
||||||
const pendingMigrations = await s.showMigrations()
|
|
||||||
if (pendingMigrations) {
|
if (pendingMigrations) {
|
||||||
if (!settings.migrate) {
|
|
||||||
throw new Error("pending migrations found, run with: MIGRATE_DB=true")
|
|
||||||
} else {
|
|
||||||
log("migrations found, migrating...")
|
log("migrations found, migrating...")
|
||||||
const migrations = await s.runMigrations()
|
const executedMigrations = await source.runMigrations({ transaction: 'all' })
|
||||||
log(migrations)
|
return { source, executedMigrations }
|
||||||
}
|
}
|
||||||
}
|
return { source, executedMigrations: [] }
|
||||||
await s.getRepository(RoutingEvent).find()
|
|
||||||
await s.getRepository(Application).find()
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
@ -26,13 +26,15 @@ export default class {
|
||||||
constructor(settings: StorageSettings) {
|
constructor(settings: StorageSettings) {
|
||||||
this.settings = settings
|
this.settings = settings
|
||||||
}
|
}
|
||||||
async Connect() {
|
async Connect(migrations: Function[]) {
|
||||||
this.DB = await NewDB(this.settings.dbSettings)
|
const { source, executedMigrations } = await NewDB(this.settings.dbSettings, migrations)
|
||||||
|
this.DB = source
|
||||||
this.userStorage = new UserStorage(this.DB)
|
this.userStorage = new UserStorage(this.DB)
|
||||||
this.productStorage = new ProductStorage(this.DB)
|
this.productStorage = new ProductStorage(this.DB)
|
||||||
this.applicationStorage = new ApplicationStorage(this.DB, this.userStorage)
|
this.applicationStorage = new ApplicationStorage(this.DB, this.userStorage)
|
||||||
this.paymentStorage = new PaymentStorage(this.DB, this.userStorage)
|
this.paymentStorage = new PaymentStorage(this.DB, this.userStorage)
|
||||||
this.metricsStorage = new MetricsStorage(this.DB)
|
this.metricsStorage = new MetricsStorage(this.DB)
|
||||||
|
return executedMigrations
|
||||||
}
|
}
|
||||||
|
|
||||||
StartTransaction(exec: TX) {
|
StartTransaction(exec: TX) {
|
||||||
|
|
|
||||||
38
src/services/storage/migrations/runner.ts
Normal file
38
src/services/storage/migrations/runner.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { PubLogger } from '../../helpers/logger.js'
|
||||||
|
import { DbSettings } from '../db.js'
|
||||||
|
import Storage, { StorageSettings } from '../index.js'
|
||||||
|
import { Initial1703170309875 } from './1703170309875-initial.js'
|
||||||
|
import { LndMetrics1703170330183 } from './1703170330183-lnd_metrics.js'
|
||||||
|
const allMigrations = [Initial1703170309875, LndMetrics1703170330183]
|
||||||
|
export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Storage, settings: DbSettings, arg: string | undefined): Promise<boolean> => {
|
||||||
|
if (arg === 'initial_migration') {
|
||||||
|
await connectAndMigrate(log, storageManager, true, settings, [Initial1703170309875])
|
||||||
|
return true
|
||||||
|
} else if (arg === 'lnd_metrics_migration') {
|
||||||
|
await connectAndMigrate(log, storageManager, true, settings, [LndMetrics1703170330183])
|
||||||
|
return true
|
||||||
|
} else if (arg === 'all_migrations') {
|
||||||
|
await connectAndMigrate(log, storageManager, true, settings, allMigrations)
|
||||||
|
return true
|
||||||
|
} else if (settings.migrate) {
|
||||||
|
await connectAndMigrate(log, storageManager, false, settings, allMigrations)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
await connectAndMigrate(log, storageManager, false, settings, [])
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectAndMigrate = async (log: PubLogger, storageManager: Storage, manual: boolean, settings: DbSettings, migrations: Function[]) => {
|
||||||
|
if (manual && settings.migrate) {
|
||||||
|
throw new Error("auto migration is enabled, no need to run manual migration")
|
||||||
|
}
|
||||||
|
if (migrations.length > 0) {
|
||||||
|
log("will add", migrations.length, "typeorm migrations...")
|
||||||
|
}
|
||||||
|
const executedMigrations = await storageManager.Connect(migrations)
|
||||||
|
if (migrations.length > 0) {
|
||||||
|
log(executedMigrations.length, "of", migrations.length, "migrations were executed correctly")
|
||||||
|
log(executedMigrations)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue