From ee3527f2928ec27474c77d7e2ea41f56c54f50d8 Mon Sep 17 00:00:00 2001 From: knotteye Date: Sat, 10 Oct 2020 15:55:32 -0500 Subject: [PATCH] Implement database versioning and migration. It could not possibly be any simpler or easier to break, but it works. And it can be used to automatically migrate to a better system for migration later. For now, the way it works is by creating a new migration script with the name of the version (increment by one, whole numbers) in the src/db folder On start up, it will compare version numbers and run new scripts. The user can also manually check for migrations and skip the automatic checking. Added a bit of additional logging to see what's happening in the startup process as well. --- README.md | 8 +++++++- package.json | 3 ++- src/cleanup.ts | 34 +++++++++++++++++++++++++++++++++- src/config.ts | 1 + src/database.ts | 1 + src/db/0.ts | 8 ++++++++ src/index.ts | 2 +- src/migrate.ts | 9 +++++++++ 8 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/db/0.ts create mode 100644 src/migrate.ts diff --git a/README.md b/README.md index cddcc6a..2018270 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,13 @@ Follow the instructions after setup runs. ### Run the server ```bash -npm start +npm run start +``` +You can also run this to skip checking the database version on startup. +```bash +npm run start -- --skip-migrate +# don't forget to migrate manually when you update +npm run migrate ``` ## Contributing diff --git a/package.json b/package.json index f77afd1..840082b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "scripts": { "start": "ts-node src/index.ts", "user": "ts-node src/cli.ts", - "setup": "sh install/setup.sh" + "setup": "sh install/setup.sh", + "migrate": "ts-node src/migrate.ts" }, "repository": { "type": "git", diff --git a/src/cleanup.ts b/src/cleanup.ts index 00e55ae..56c9dc9 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -1,10 +1,42 @@ import * as db from "./database"; +import {readdirSync} from "fs"; -async function init() { +async function init(m?: boolean) { + if(!m){ + console.log('Checking database version.'); + var tmp: string[] = await db.query('show tables like \"db_meta\"'); + if(tmp.length === 0){ + console.log('No database version info, running initial migration.'); + await require('./db/0').run(); + await bringUpToDate(); + } + else { + await bringUpToDate(); + } + } + else { + console.log('Skipping database version check.'); + } //If satyr is restarted in the middle of a stream //it causes problems //Live flags in the database stay live await db.query('update user_meta set live=false'); } +async function bringUpToDate(): Promise{ + var versions: Object[] = await db.query('select * from db_meta'); + var scripts: Buffer[] | string[] = readdirSync('./src/db/', {withFileTypes: false}); + var diff: number = scripts.length - versions.length + if(diff === 0){ + console.log('No migration needed.'); + } else { + console.log('Versions differ, migrating now.'); + for(let i=0;i {process.exit()}); \ No newline at end of file