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 { LoadNosrtSettingsFromEnv } from './services/nostr/index.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 log = getLogger({})
|
||||
const mainSettings = LoadMainSettingsFromEnv()
|
||||
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)
|
||||
await mainHandler.lnd.Warmup()
|
||||
const serverMethods = GetServerMethods(mainHandler)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import "reflect-metadata"
|
||||
import { DataSource } from "typeorm"
|
||||
import { DataSource, Migration } from "typeorm"
|
||||
import { AddressReceivingTransaction } from "./entity/AddressReceivingTransaction.js"
|
||||
import { User } from "./entity/User.js"
|
||||
import { UserReceivingAddress } from "./entity/UserReceivingAddress.js"
|
||||
|
|
@ -24,20 +24,16 @@ import { LndMetrics1703170330183 } from "./migrations/1703170330183-lnd_metrics.
|
|||
export type DbSettings = {
|
||||
databaseFile: string
|
||||
migrate: boolean
|
||||
doInitialMigration: boolean
|
||||
}
|
||||
export const LoadDbSettingsFromEnv = (test = false): DbSettings => {
|
||||
return {
|
||||
databaseFile: test ? ":memory:" : EnvMustBeNonEmptyString("DATABASE_FILE"),
|
||||
migrate: process.env.MIGRATE_DB === 'true' || false,
|
||||
doInitialMigration: process.env.DO_INITIAL_MIGRATION === 'true' || false
|
||||
}
|
||||
}
|
||||
|
||||
export default async (settings: DbSettings) => {
|
||||
const migrations = settings.doInitialMigration ? [Initial1703170309875] : []
|
||||
migrations.push(LndMetrics1703170330183)
|
||||
const s = await new DataSource({
|
||||
export default async (settings: DbSettings, migrations: Function[]): Promise<{ source: DataSource, executedMigrations: Migration[] }> => {
|
||||
const source = await new DataSource({
|
||||
type: "sqlite",
|
||||
database: settings.databaseFile,
|
||||
// logging: true,
|
||||
|
|
@ -47,18 +43,11 @@ export default async (settings: DbSettings) => {
|
|||
migrations
|
||||
}).initialize()
|
||||
const log = getLogger({})
|
||||
|
||||
const pendingMigrations = await s.showMigrations()
|
||||
const pendingMigrations = await source.showMigrations()
|
||||
if (pendingMigrations) {
|
||||
if (!settings.migrate) {
|
||||
throw new Error("pending migrations found, run with: MIGRATE_DB=true")
|
||||
} else {
|
||||
log("migrations found, migrating...")
|
||||
const migrations = await s.runMigrations()
|
||||
log(migrations)
|
||||
}
|
||||
log("migrations found, migrating...")
|
||||
const executedMigrations = await source.runMigrations({ transaction: 'all' })
|
||||
return { source, executedMigrations }
|
||||
}
|
||||
await s.getRepository(RoutingEvent).find()
|
||||
await s.getRepository(Application).find()
|
||||
return s
|
||||
return { source, executedMigrations: [] }
|
||||
}
|
||||
|
|
@ -26,13 +26,15 @@ export default class {
|
|||
constructor(settings: StorageSettings) {
|
||||
this.settings = settings
|
||||
}
|
||||
async Connect() {
|
||||
this.DB = await NewDB(this.settings.dbSettings)
|
||||
async Connect(migrations: Function[]) {
|
||||
const { source, executedMigrations } = await NewDB(this.settings.dbSettings, migrations)
|
||||
this.DB = source
|
||||
this.userStorage = new UserStorage(this.DB)
|
||||
this.productStorage = new ProductStorage(this.DB)
|
||||
this.applicationStorage = new ApplicationStorage(this.DB, this.userStorage)
|
||||
this.paymentStorage = new PaymentStorage(this.DB, this.userStorage)
|
||||
this.metricsStorage = new MetricsStorage(this.DB)
|
||||
return executedMigrations
|
||||
}
|
||||
|
||||
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