Skip to content

Commit

Permalink
Merge pull request #69 from versx/develop
Browse files Browse the repository at this point in the history
Database Migrations
  • Loading branch information
versx authored Jul 27, 2020
2 parents d3c7430 + 455340e commit 3d485e2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
8 changes: 4 additions & 4 deletions migrations/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ CREATE TABLE IF NOT EXISTS `devices` (
`config` varchar(64) DEFAULT NULL,
`last_seen` int DEFAULT NULL,
CONSTRAINT `fk_config_name`
FOREIGN KEY (`config`)
REFERENCES `configs` (`name`)
ON DELETE SET NULL
ON UPDATE CASCADE
FOREIGN KEY (`config`)
REFERENCES `configs` (`name`)
ON DELETE SET NULL
ON UPDATE CASCADE
);
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const utils = require('./services/utils.js');
// TODO: Webhook for device reboots
// TODO: Send coord changes to DCM via client
// TODO: Replace request with axios
// TODO: Device stats

// If more than 300 devices, increase
require('events').defaultMaxListeners = 300;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ router.post('/config', async (req, res) => {
// Device exists
device.lastSeen = utils.convertTz(new Date()) / 1000;
// Only update client IP if it hasn't been set yet.
if (device.clientip === null) {
if (device.clientip === null || device.clientip !== clientip) {
device.clientip = clientip;
}
device.iosVersion = ios_version;
Expand Down
48 changes: 17 additions & 31 deletions src/services/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ class Migrator {
}

const newestVersion = this.getNewestDbVersion();
logger('dcm').info(`[DbController] Current: ${version}, Latest: ${newestVersion}`);
console.log(`[DbController] Current: ${version}, Latest: ${newestVersion}`);
logger('dcm').info(`[DBController] Current: ${version}, Latest: ${newestVersion}`);
console.log(`[DBController] Current: ${version}, Latest: ${newestVersion}`);
if (version < newestVersion) {
// Wait 30 seconds and let user know we are about to migrate the database and for them to make a backup until we handle backups and rollbacks.
logger('dcm').info('[DBController] MIGRATION IS ABOUT TO START IN 30 SECONDS, PLEASE MAKE SURE YOU HAVE A BACKUP!!!');
console.log('[DBController] MIGRATION IS ABOUT TO START IN 30 SECONDS, PLEASE MAKE SURE YOU HAVE A BACKUP!!!');
await utils.snooze(30 * 1000);
}
this.migrate(version, newestVersion);
}
static async getEntries() {
Expand All @@ -70,12 +76,9 @@ class Migrator {
}
async migrate(fromVersion, toVersion) {
if (fromVersion < toVersion) {
// Wait 30 seconds and let user know we are about to migrate the database and for them to make a backup until we handle backups and rollbacks.
logger('dcm').info('[DBController] MIGRATION IS ABOUT TO START IN 30 SECONDS, PLEASE MAKE SURE YOU HAVE A BACKUP!!!');
console.log('[DBController] MIGRATION IS ABOUT TO START IN 30 SECONDS, PLEASE MAKE SURE YOU HAVE A BACKUP!!!');
await utils.snooze(30 * 1000);
logger('dcm').info(`[DBController] Migrating database to version ${(fromVersion + 1)}`);
console.log(`[DBController] Migrating database to version ${(fromVersion + 1)}`);
await utils.snooze(5 * 1000);
let migrateSQL;
try {
const sqlFile = `${migrationsDir}${path.sep}${fromVersion + 1}.sql`;
Expand All @@ -84,42 +87,25 @@ class Migrator {
} catch (err) {
logger('dcm').error(`[DBController] Migration failed: ${err}`);
console.error(`[DBController] Migration failed: ${err}`);
console.error(`[DBController] Migration failed: ${err}`);
process.exit(-1);
}
const sqlSplit = migrateSQL.split(';');
sqlSplit.forEach(async sql => {
//sqlSplit.forEach(async sql => {
for (let i = 0; i < sqlSplit.length; i++) {
const sql = sqlSplit[i];
const msql = sql.replace('&semi', ';').trim();
if (msql !== '') {
logger('dcm').info(`[DBController] Executing: ${msql}`);
console.log(`[DBController] Executing: ${msql}`);
const result = await query(msql)
//logger('dcm').info(`[DBController] Executing: ${msql}`);
//console.log(`[DBController] Executing: ${msql}`);
await query(msql)
.then(x => x)
.catch(async err => {
logger('dcm').error(`[DBController] Migration failed: ${err}`);
console.error(`[DBController] Migration failed: ${err}`);
/*
if (noBackup === undefined || noBackup === null || noBackup === false) {
for (let i = 0; i < 10; i++) {
logger.warn(`[DBController] Rolling back migration in ${(10 - i)} seconds`);
await utils.snooze(1000);
}
logger('dcm').info('[DBController] Rolling back migration now. Do not kill RDM!');
rollback(
backupFileSchema.path.toString(),
backupFileTrigger.path.toString(),
backupFileData.path.toString()
);
}
//fatalError(message);
return null;
*/
});
logger('dcm').info(`[DBController] Migration execution result: ${result}`);
console.log(`[DBController] Migration execution result: ${result}`);
await utils.snooze(2000);
//await utils.snooze(2000);
}
});
}

const newVersion = fromVersion + 1;
const updateVersionSQL = `
Expand Down

0 comments on commit 3d485e2

Please sign in to comment.