2020-04-08 22:05:53 +02:00
|
|
|
import util from "util";
|
|
|
|
import fs from "graceful-fs";
|
|
|
|
import glob from "glob";
|
|
|
|
import path from "path";
|
2016-05-20 22:11:35 +02:00
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
import sqlite from "sqlite";
|
|
|
|
import sqlite3 from "sqlite3";
|
2016-05-20 22:11:35 +02:00
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
import {config} from "../config";
|
|
|
|
import Logger from "../logger";
|
2016-05-20 22:38:13 +02:00
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
const pglob = util.promisify(glob);
|
|
|
|
const pReadFile = util.promisify(fs.readFile);
|
|
|
|
|
|
|
|
async function applyPatch(db: sqlite.Database, file: string): Promise<void> {
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger.tag('database', 'migration').info('Checking if patch need to be applied: %s', file);
|
2016-05-24 16:40:57 +02:00
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
const contents = await pReadFile(file);
|
2018-12-17 21:52:19 +01:00
|
|
|
const version = path.basename(file, '.sql');
|
2016-05-20 22:11:35 +02:00
|
|
|
|
2018-12-17 21:52:19 +01:00
|
|
|
const row = await db.get('SELECT * FROM schema_version WHERE version = ?', version);
|
|
|
|
if (row) {
|
|
|
|
// patch is already applied. skip!
|
|
|
|
Logger.tag('database', 'migration').info('Patch already applied, skipping: %s', file);
|
|
|
|
return
|
|
|
|
}
|
2016-05-20 22:11:35 +02:00
|
|
|
|
2018-12-17 21:52:19 +01:00
|
|
|
const sql = 'BEGIN TRANSACTION;\n' +
|
|
|
|
contents.toString() + '\n' +
|
|
|
|
'INSERT INTO schema_version (version) VALUES (\'' + version + '\');\n' +
|
|
|
|
'END TRANSACTION;';
|
2016-05-24 16:40:57 +02:00
|
|
|
|
2018-12-17 21:52:19 +01:00
|
|
|
await db.exec(sql);
|
2016-05-24 16:40:57 +02:00
|
|
|
|
2018-12-17 21:52:19 +01:00
|
|
|
Logger.tag('database', 'migration').info('Patch successfully applied: %s', file);
|
2016-05-20 22:11:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
async function applyMigrations(db: sqlite.Database): Promise<void> {
|
2016-05-24 16:40:57 +02:00
|
|
|
Logger.tag('database', 'migration').info('Migrating database...');
|
|
|
|
|
2018-12-17 21:52:19 +01:00
|
|
|
const sql = 'BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n' +
|
2017-05-06 17:31:34 +02:00
|
|
|
' version VARCHAR(255) PRIMARY KEY ASC,\n' +
|
|
|
|
' applied_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL\n' +
|
|
|
|
'); END TRANSACTION;';
|
2018-12-17 21:52:19 +01:00
|
|
|
|
|
|
|
await db.exec(sql);
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
const files = await pglob(__dirname + '/patches/*.sql');
|
2018-12-17 21:52:19 +01:00
|
|
|
for (const file of files) {
|
|
|
|
await applyPatch(db, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
export async function init(): Promise<void> {
|
2018-12-17 21:52:19 +01:00
|
|
|
const file = config.server.databaseFile;
|
|
|
|
Logger.tag('database').info('Setting up database: %s', file);
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
let db: sqlite.Database;
|
2018-12-17 21:52:19 +01:00
|
|
|
try {
|
2019-03-29 22:26:54 +01:00
|
|
|
db = await sqlite.open(file);
|
2018-12-17 21:52:19 +01:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('database').error('Error initialzing database:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
db.on('profile', (sql, time) => Logger.tag('database').profile('[%sms]\t%s', time, sql));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await applyMigrations(db);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('database').error('Error migrating database:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
await db.close()
|
|
|
|
}
|
2019-03-29 22:26:54 +01:00
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
Logger.tag('database').info('Setting up legacy database: %s', config.server.databaseFile);
|
|
|
|
|
|
|
|
let legacyDB: sqlite3.Database;
|
|
|
|
try {
|
|
|
|
legacyDB = new sqlite3.Database(config.server.databaseFile);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('database').error('Error initialzing legacy database lib:', error);
|
|
|
|
throw error;
|
2016-05-20 22:11:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 22:05:53 +02:00
|
|
|
export const db = legacyDB;
|