2019-10-22 18:24:10 -05:00
|
|
|
import * as db from "./database";
|
2020-10-10 15:55:32 -05:00
|
|
|
import {readdirSync} from "fs";
|
2020-10-16 21:31:23 -05:00
|
|
|
import { execSync } from "child_process";
|
2019-10-22 18:24:10 -05:00
|
|
|
|
2020-10-16 21:31:23 -05:00
|
|
|
async function init() {
|
|
|
|
if(process.argv.indexOf('--skip-migrate') === -1){
|
2020-10-10 15:55:32 -05:00
|
|
|
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();
|
|
|
|
}
|
2020-10-30 07:20:23 -05:00
|
|
|
await bringUpToDate();
|
2020-10-10 15:55:32 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('Skipping database version check.');
|
|
|
|
}
|
2020-10-16 21:31:23 -05:00
|
|
|
|
|
|
|
if(!require('./config').config['http']['server_side_render'] && process.argv.indexOf('--skip-compile') === -1) {
|
|
|
|
console.log("Compiling templates for client-side frontend.");
|
|
|
|
execSync(process.cwd()+'/node_modules/.bin/nunjucks-precompile -i [\"\\.html$\",\"\\.njk$\"] templates > site/templates.js');
|
|
|
|
}
|
|
|
|
else if(!require('./config').config['http']['server_side_render']){
|
|
|
|
console.log("Skipped compiling templates for client-side frontend.");
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:24:10 -05:00
|
|
|
//If satyr is restarted in the middle of a stream
|
|
|
|
//it causes problems
|
|
|
|
//Live flags in the database stay live
|
2021-01-15 10:56:32 -06:00
|
|
|
await db.query('update user_meta set live=false');
|
|
|
|
await db.query('update user_meta set viewers=0');
|
2019-10-22 18:24:10 -05:00
|
|
|
}
|
|
|
|
|
2020-10-10 15:55:32 -05:00
|
|
|
async function bringUpToDate(): Promise<void>{
|
2020-10-30 07:20:23 -05:00
|
|
|
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){
|
2020-10-10 15:55:32 -05:00
|
|
|
console.log('No migration needed.');
|
|
|
|
} else {
|
|
|
|
console.log('Versions differ, migrating now.');
|
2020-10-30 07:20:23 -05:00
|
|
|
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();
|
2020-10-10 15:55:32 -05:00
|
|
|
}
|
|
|
|
console.log('Done migrating database.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 11:34:16 -06:00
|
|
|
export { init };
|