Skip to content
Snippets Groups Projects
Commit 58ef17be authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Added automated database migration script

parent 553e7fb8
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,7 @@ function FileDAL(params) { ...@@ -33,6 +33,7 @@ function FileDAL(params) {
// DALs // DALs
this.confDAL = new ConfDAL(rootPath, myFS, null, that, CFSStorage); this.confDAL = new ConfDAL(rootPath, myFS, null, that, CFSStorage);
this.metaDAL = new (require('./sqliteDAL/MetaDAL'))(sqlite);
this.peerDAL = new (require('./sqliteDAL/PeerDAL'))(sqlite); this.peerDAL = new (require('./sqliteDAL/PeerDAL'))(sqlite);
this.blockDAL = new (require('./sqliteDAL/BlockDAL'))(sqlite); this.blockDAL = new (require('./sqliteDAL/BlockDAL'))(sqlite);
this.sourcesDAL = new (require('./sqliteDAL/SourcesDAL'))(sqlite); this.sourcesDAL = new (require('./sqliteDAL/SourcesDAL'))(sqlite);
...@@ -45,6 +46,7 @@ function FileDAL(params) { ...@@ -45,6 +46,7 @@ function FileDAL(params) {
this.msDAL = new (require('./sqliteDAL/MembershipDAL'))(sqlite); this.msDAL = new (require('./sqliteDAL/MembershipDAL'))(sqlite);
this.newDals = { this.newDals = {
'metaDAL': that.metaDAL,
'blockDAL': that.blockDAL, 'blockDAL': that.blockDAL,
'certDAL': that.certDAL, 'certDAL': that.certDAL,
'msDAL': that.msDAL, 'msDAL': that.msDAL,
...@@ -77,8 +79,11 @@ function FileDAL(params) { ...@@ -77,8 +79,11 @@ function FileDAL(params) {
this.init = () => co(function *() { this.init = () => co(function *() {
yield _.values(that.newDals).map((dal) => dal.init()); yield _.values(that.newDals).map((dal) => dal.init());
yield that.metaDAL.upgradeDatabase();
}); });
this.getDBVersion = () => that.metaDAL.getVersion();
this.getCurrency = function() { this.getCurrency = function() {
return currency; return currency;
}; };
......
/**
* Created by cgeek on 22/08/15.
*/
var co = require('co');
var logger = require('../../../../app/lib/logger')('linksDAL');
var AbstractSQLite = require('./AbstractSQLite');
module.exports = MetaDAL;
function MetaDAL(db) {
"use strict";
AbstractSQLite.call(this, db);
let that = this;
this.table = 'meta';
this.fields = [
'id',
'version'
];
this.arrays = [];
this.booleans = [];
this.pkFields = ['version'];
this.translated = {};
let migrations = {
0: 'BEGIN; COMMIT;',
1: 'BEGIN; COMMIT;'
};
this.init = () => co(function *() {
return that.exec('BEGIN;' +
'CREATE TABLE IF NOT EXISTS ' + that.table + ' (' +
'id INTEGER NOT NULL,' +
'version INTEGER NOT NULL,' +
'PRIMARY KEY (id)' +
');' +
'INSERT INTO ' + that.table + ' VALUES (1,0);' +
'COMMIT;', []);
});
this.upgradeDatabase = () => co(function *() {
let version = yield that.getVersion();
while(migrations[version]) {
yield that.exec(migrations[version]);
// Automated increment
yield that.exec('UPDATE meta SET version = version + 1');
version++;
}
});
this.getRow = () => that.sqlFindOne({ id: 1 });
this.getVersion = () => co(function *() {
let row = yield that.getRow();
return row.version;
});
}
...@@ -170,6 +170,12 @@ describe("DAL", function(){ ...@@ -170,6 +170,12 @@ describe("DAL", function(){
return fileDAL.saveConf({ currency: "meta_brouzouf" }); return fileDAL.saveConf({ currency: "meta_brouzouf" });
})); }));
it('should have DB version 2', () => co(function *() {
let version = yield fileDAL.getDBVersion();
should.exist(version);
version.should.equal(2);
}));
it('should have no peer in a first time', function(){ it('should have no peer in a first time', function(){
return fileDAL.listAllPeers().then(function(peers){ return fileDAL.listAllPeers().then(function(peers){
peers.should.have.length(0); peers.should.have.length(0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment