ffffng/server/db/database.js

112 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-05-20 22:11:35 +02:00
'use strict';
var async = require('async');
var fs = require('graceful-fs');
2016-05-20 22:11:35 +02:00
var glob = require('glob');
var path = require('path');
2016-05-20 22:38:13 +02:00
var config = require('../config');
2016-05-24 16:40:57 +02:00
var Logger = require('../logger');
2016-05-20 22:38:13 +02:00
2016-05-20 22:11:35 +02:00
function applyPatch(db, file, callback) {
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
2016-05-20 22:11:35 +02:00
fs.readFile(file, function (err, contents) {
if (err) {
return callback(err);
}
var version = path.basename(file, '.sql');
db.get('SELECT * FROM schema_version WHERE version = ?', version, function (err, row) {
if (err) {
return callback(err);
}
if (row) {
// patch is already applied. skip!
2016-05-24 21:08:34 +02:00
Logger.tag('database', 'migration').info('Patch already applied, skipping: %s', file);
2016-05-20 22:11:35 +02:00
return callback(null);
}
2017-05-06 17:31:34 +02:00
var sql = 'BEGIN TRANSACTION;\n' +
contents.toString() + '\n' +
'INSERT INTO schema_version (version) VALUES (\'' + version + '\');\n' +
'END TRANSACTION;';
2016-05-20 22:11:35 +02:00
2016-05-24 16:40:57 +02:00
db.exec(sql, function (err) {
if (err) {
return callback(err);
}
2016-05-24 21:08:34 +02:00
Logger.tag('database', 'migration').info('Patch successfully applied: %s', file);
2016-05-24 16:40:57 +02:00
callback(null);
});
2016-05-20 22:11:35 +02:00
});
});
}
function applyMigrations(db, callback) {
2016-05-24 16:40:57 +02:00
Logger.tag('database', 'migration').info('Migrating database...');
2017-05-06 17:31:34 +02:00
var sql = 'BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n' +
' version VARCHAR(255) PRIMARY KEY ASC,\n' +
' applied_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL\n' +
'); END TRANSACTION;';
2016-05-20 22:11:35 +02:00
db.exec(sql, function (err) {
if (err) {
return callback(err);
}
glob(__dirname + '/patches/*.sql', function (err, files) {
if (err) {
return callback(err);
}
2016-05-24 16:40:57 +02:00
async.eachSeries(
2016-05-20 22:11:35 +02:00
files,
function (file, fileCallback) {
applyPatch(db, file, fileCallback);
},
callback
);
});
});
}
module.exports = {
init: function (callback) {
var SQLite3 = require('sqlite3');
2016-05-24 16:40:57 +02:00
var file = config.server.databaseFile;
2016-05-24 21:08:34 +02:00
Logger.tag('database').info('Setting up database: %s', file);
2016-05-24 16:40:57 +02:00
var db;
try {
db = new SQLite3.Database(file);
}
catch (error) {
Logger.tag('database').error('Error initialzing database:', error);
throw error;
}
2016-05-20 22:11:35 +02:00
db.on('profile', function (sql, time) {
Logger.tag('database').profile('[%sms]\t%s', time, sql);
});
2016-05-20 22:11:35 +02:00
applyMigrations(db, function (err) {
if (err) {
2016-05-24 16:40:57 +02:00
Logger.tag('database').error('Error migrating database:', err);
2016-05-20 22:11:35 +02:00
throw err;
}
angular.module('ffffng').factory('Database', function () {
return db;
});
callback();
});
}
};