Merge branch 'database-migrate' into 'develop'
Implement database versioning and migration. It could not possibly be any... See merge request knotteye/satyr!24merge-requests/25/merge
commit
5ff40c7b37
|
@ -13,7 +13,13 @@ Follow the instructions after setup runs.
|
||||||
|
|
||||||
### Run the server
|
### Run the server
|
||||||
```bash
|
```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
|
## Contributing
|
||||||
|
|
|
@ -7,7 +7,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "ts-node src/index.ts",
|
"start": "ts-node src/index.ts",
|
||||||
"user": "ts-node src/cli.ts",
|
"user": "ts-node src/cli.ts",
|
||||||
"setup": "sh install/setup.sh"
|
"setup": "sh install/setup.sh",
|
||||||
|
"migrate": "ts-node src/migrate.ts"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
|
@ -1,10 +1,42 @@
|
||||||
import * as db from "./database";
|
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
|
//If satyr is restarted in the middle of a stream
|
||||||
//it causes problems
|
//it causes problems
|
||||||
//Live flags in the database stay live
|
//Live flags in the database stay live
|
||||||
await db.query('update user_meta set live=false');
|
await db.query('update user_meta set live=false');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
console.log('Done migrating database.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export { init };
|
export { init };
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {parseAsYaml as parse} from "parse-yaml";
|
||||||
import {readFileSync as read} from "fs";
|
import {readFileSync as read} from "fs";
|
||||||
try {
|
try {
|
||||||
var localconfig: Object = parse(read('config/config.yml'));
|
var localconfig: Object = parse(read('config/config.yml'));
|
||||||
|
console.log('Config file found.');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('No config file found. Exiting.');
|
console.log('No config file found. Exiting.');
|
||||||
process.exit();
|
process.exit();
|
||||||
|
|
|
@ -9,6 +9,7 @@ var cryptoconfig: Object;
|
||||||
function init (){
|
function init (){
|
||||||
raw = mysql.createPool(config['database']);
|
raw = mysql.createPool(config['database']);
|
||||||
cryptoconfig = config['crypto'];
|
cryptoconfig = config['crypto'];
|
||||||
|
console.log('Connected to database.');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addUser(name: string, password: string){
|
async function addUser(name: string, password: string){
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import * as db from "../database";
|
||||||
|
|
||||||
|
async function run () {
|
||||||
|
await db.query('CREATE TABLE IF NOT EXISTS db_meta(version SMALLINT)');
|
||||||
|
await db.query('INSERT INTO db_meta (version) VALUES (0)');
|
||||||
|
}
|
||||||
|
|
||||||
|
export { run }
|
|
@ -7,7 +7,7 @@ import { config } from "./config";
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
await initDB();
|
await initDB();
|
||||||
await clean();
|
await clean(process.argv.indexOf('--skip-migrate') !== -1);
|
||||||
await initHTTP();
|
await initHTTP();
|
||||||
await initRTMP();
|
await initRTMP();
|
||||||
await initChat();
|
await initChat();
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import {init as initDB} from "./database";
|
||||||
|
import {init as clean} from "./cleanup";
|
||||||
|
import { config } from "./config";
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
await initDB();
|
||||||
|
await clean(false);
|
||||||
|
}
|
||||||
|
run().then(() => {process.exit()});
|
Reference in New Issue