Skip to content

Commit

Permalink
Added auto-db migration back.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed May 20, 2024
1 parent 1ac07f4 commit 8e61f93
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,6 +86,16 @@ async function main() : Promise<void>

await permsMan.init();

//------------------------------------------------------------------------------------------------------------------
// Database
//------------------------------------------------------------------------------------------------------------------

if(!devMode)
{
logger.info('Running database migrations...');
await runMigrations();
}

//------------------------------------------------------------------------------------------------------------------

const store = new KnexSessionStore({
Expand Down
21 changes: 21 additions & 0 deletions src/server/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,25 @@ export async function getDB() : Promise<Knex>
}
}

export async function runMigrations(runSeeds = true, convertToJs = true) : Promise<void>
{
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' });
}
}

// ---------------------------------------------------------------------------------------------------------------------

0 comments on commit 8e61f93

Please sign in to comment.