diff --git a/migrations/1.sql b/migrations/1.sql index 5b340b6..6f905fc 100644 --- a/migrations/1.sql +++ b/migrations/1.sql @@ -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 ); diff --git a/src/index.js b/src/index.js index cfa9640..e998caf 100644 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/src/routes/api.js b/src/routes/api.js index cec9877..6ce91dc 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -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; diff --git a/src/services/migrator.js b/src/services/migrator.js index 19839d9..07191e1 100644 --- a/src/services/migrator.js +++ b/src/services/migrator.js @@ -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() { @@ -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`; @@ -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 = `