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,11 +9,8 @@ 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.');
} }

View File

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