From 8e61f93c4ab9bd74771b455310a71ebaf5d35f3f Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Sun, 19 May 2024 20:51:35 -0500 Subject: [PATCH] Added auto-db migration back. --- src/server/server.ts | 12 +++++++++++- src/server/utils/database.ts | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/server/server.ts b/src/server/server.ts index 5ceea12b..af471361 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -44,7 +44,7 @@ import { errorLogger, requestLogger, serveIndex } from './routes/utils'; import { setSIOInstance } from './utils/sio'; import program from './utils/args'; import { getVersion } from './utils/version'; -import { getDB } from './utils/database'; +import { getDB, runMigrations } from './utils/database'; // --------------------------------------------------------------------------------------------------------------------- // Server Configuration @@ -86,6 +86,16 @@ async function main() : Promise await permsMan.init(); + //------------------------------------------------------------------------------------------------------------------ + // Database + //------------------------------------------------------------------------------------------------------------------ + + if(!devMode) + { + logger.info('Running database migrations...'); + await runMigrations(); + } + //------------------------------------------------------------------------------------------------------------------ const store = new KnexSessionStore({ diff --git a/src/server/utils/database.ts b/src/server/utils/database.ts index ffa21666..67c77743 100644 --- a/src/server/utils/database.ts +++ b/src/server/utils/database.ts @@ -141,4 +141,25 @@ export async function getDB() : Promise } } +export async function runMigrations(runSeeds = true, convertToJs = true) : Promise +{ + const db = await getDB(); + + if(convertToJs) + { + // Ensure that the migrations are in .js format + await db.update({ name: db.raw('replace(name, \'.ts\', \'.js\')') }) + .from('knex_migrations'); + } + + // Run the migrations + await db.migrate.latest({ directory: './dist/server/knex/migrations' }); + + // Run the seeds + if(runSeeds) + { + await db.seed.run({ directory: './dist/server/knex/seeds' }); + } +} + // ---------------------------------------------------------------------------------------------------------------------