Select scripts to run based on comparing version strings and script names.

This means scripts need to insert a version value the same as their name to db_meta. This is nothing new, it just works better now.
pull/12/head
knotteye 2020-10-30 07:20:23 -05:00
parent 3e073e7f66
commit 33accfb8b7
1 changed files with 11 additions and 11 deletions

View File

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