From a88d6ca4867001e5e096c466f0a24e5a83b7c5b6 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Tue, 19 Dec 2023 15:20:55 +0100 Subject: [PATCH 1/2] enable db logging --- src/services/storage/db.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/storage/db.ts b/src/services/storage/db.ts index 582cafcf..9c7870a8 100644 --- a/src/services/storage/db.ts +++ b/src/services/storage/db.ts @@ -23,7 +23,7 @@ export default async (settings: DbSettings) => { return new DataSource({ type: "sqlite", database: settings.databaseFile, - //logging: true, + logging: true, entities: [User, UserReceivingInvoice, UserReceivingAddress, AddressReceivingTransaction, UserInvoicePayment, UserTransactionPayment, UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment], synchronize: true, }).initialize() From ef3b6ed860735b73b260ae8b46fe059efeac8473 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Wed, 20 Dec 2023 17:58:48 +0100 Subject: [PATCH 2/2] disable sync, run migrations --- src/services/storage/db.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/services/storage/db.ts b/src/services/storage/db.ts index 9c7870a8..5fbc1018 100644 --- a/src/services/storage/db.ts +++ b/src/services/storage/db.ts @@ -13,18 +13,36 @@ import { Product } from "./entity/Product.js" import { UserToUserPayment } from "./entity/UserToUserPayment.js" import { Application } from "./entity/Application.js" import { ApplicationUser } from "./entity/ApplicationUser.js" +import { getLogger } from "../helpers/logger.js" export type DbSettings = { databaseFile: string + migrate: boolean } 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) => { - return new DataSource({ + const s = await new DataSource({ type: "sqlite", database: settings.databaseFile, - logging: true, + // logging: true, entities: [User, UserReceivingInvoice, UserReceivingAddress, AddressReceivingTransaction, UserInvoicePayment, UserTransactionPayment, UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment], - synchronize: true, + // synchronize: true, }).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 } \ No newline at end of file