better-migration -> develop #12

Merged
knotteye merged 2 commits from better-migration into develop 2020-10-30 07:27:56 -05:00
2 changed files with 12 additions and 11 deletions

View File

@ -9,12 +9,9 @@ async function init() {
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.');
}
@ -34,16 +31,19 @@ async function init() {
}
async function bringUpToDate(): Promise<void>{
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){
var versions: Object[] = JSON.parse(JSON.stringify(await db.query('select * from db_meta'))); //ugh, don't ask
var scripts: any[] = readdirSync('./src/db/', {withFileTypes: false});
if(scripts.length - versions.length === 0){
console.log('No migration needed.');
} else {
console.log('Versions differ, migrating now.');
for(let i=0;i<diff;i++){
console.log('Migration to version '+Math.floor(scripts.length-(diff-i)));
await require('./db/'+scripts[Math.floor(scripts.length-(diff-i))]).run();
var diff: string[] = scripts.filter(n => {
//we have to use versions.some because {version: 0} === {version: 0} returns false lmao
return !versions.some(o => o['version']+''=== n.substring(0, n.length - 3))
});
for(let i=0;i<diff.length;i++){
console.log('Running migration '+diff[i]);
await require('./db/'+diff[i]).run();
}
console.log('Done migrating database.');
}

View File

@ -3,6 +3,7 @@ import {init as clean} from "./cleanup";
import { config } from "./config";
async function run() {
process.argv = process.argv.concat(['--skip-compile']);
await initDB();
await clean();
}