Merge branch 'master' into lnd-metrics
This commit is contained in:
commit
198f939c7c
1 changed files with 22 additions and 4 deletions
|
|
@ -16,19 +16,37 @@ import { ApplicationUser } from "./entity/ApplicationUser.js"
|
||||||
import { RoutingEvent } from "./entity/RoutingEvent.js"
|
import { RoutingEvent } from "./entity/RoutingEvent.js"
|
||||||
import { BalanceEvent } from "./entity/BalanceEvent.js"
|
import { BalanceEvent } from "./entity/BalanceEvent.js"
|
||||||
import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js"
|
import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js"
|
||||||
|
import { getLogger } from "../helpers/logger.js"
|
||||||
export type DbSettings = {
|
export type DbSettings = {
|
||||||
databaseFile: string
|
databaseFile: string
|
||||||
|
migrate: boolean
|
||||||
}
|
}
|
||||||
export const LoadDbSettingsFromEnv = (test = false): DbSettings => {
|
export const LoadDbSettingsFromEnv = (test = false): DbSettings => {
|
||||||
return { databaseFile: test ? ":memory:" : EnvMustBeNonEmptyString("DATABASE_FILE") }
|
return {
|
||||||
|
databaseFile: test ? ":memory:" : EnvMustBeNonEmptyString("DATABASE_FILE"),
|
||||||
|
migrate: process.env.MIGRATE_DB === 'true' || false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
export default async (settings: DbSettings) => {
|
export default async (settings: DbSettings) => {
|
||||||
return new DataSource({
|
const s = await new DataSource({
|
||||||
type: "sqlite",
|
type: "sqlite",
|
||||||
database: settings.databaseFile,
|
database: settings.databaseFile,
|
||||||
//logging: true,
|
// logging: true,
|
||||||
entities: [User, UserReceivingInvoice, UserReceivingAddress, AddressReceivingTransaction, UserInvoicePayment, UserTransactionPayment,
|
entities: [User, UserReceivingInvoice, UserReceivingAddress, AddressReceivingTransaction, UserInvoicePayment, UserTransactionPayment,
|
||||||
UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment, RoutingEvent, BalanceEvent, ChannelBalanceEvent],
|
UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment, RoutingEvent, BalanceEvent, ChannelBalanceEvent],
|
||||||
synchronize: true,
|
// synchronize: true,
|
||||||
}).initialize()
|
}).initialize()
|
||||||
|
const log = getLogger({})
|
||||||
|
|
||||||
|
const pendingMigrations = await s.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue