larvitdbmigration
Advanced tools
Comparing version 1.2.0 to 1.3.0
70
index.js
@@ -26,14 +26,62 @@ 'use strict'; | ||
function getLock(cb) { | ||
db.query('UPDATE `' + options.tableName + '` SET running = 1', function(err, res) { | ||
if (err) { cb(err); return; } | ||
const tasks = []; | ||
if (res.changedRows === 0) { | ||
log.info('larvitdbmigration: Another process is running the migrations for table ' + options.tableName + ', wait and try again soon.'); | ||
setTimeout(function() { | ||
getLock(cb); | ||
}, 500); | ||
} else { | ||
cb(); | ||
} | ||
let dbCon; | ||
tasks.push(function(cb) { | ||
db.pool.getConnection(function(err, res) { | ||
if (err) { | ||
log.error('larvitdbmigration: getLock() - getConnection() err: ' + err.message); | ||
} | ||
dbCon = res; | ||
cb(err); | ||
}); | ||
}); | ||
tasks.push(function(cb) { | ||
dbCon.query('LOCK TABLES `' + options.tableName + '` WRITE;', cb); | ||
}); | ||
tasks.push(function(cb) { | ||
dbCon.query('SELECT running FROM `' + options.tableName + '`', function(err, rows) { | ||
if (err) { | ||
log.error('larvitdbmigration: getLock() - SQL err: ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
if (rows.length === 0) { | ||
const err = 'No database records in ' + options.tableName; | ||
log.error('larvitdbmigration: getLock() - ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
if (rows[0].running === 0) { | ||
cb(); | ||
} else { | ||
log.info('larvitdbmigration: getLock() - Another process is running the migrations for table ' + options.tableName + ', wait and try again soon.'); | ||
setTimeout(function() { | ||
getLock(cb); | ||
}, 500); | ||
} | ||
}); | ||
}); | ||
tasks.push(function(cb) { | ||
dbCon.query('UPDATE `' + options.tableName + '` SET running = 1', cb); | ||
}); | ||
tasks.push(function(cb) { | ||
dbCon.query('UNLOCK TABLES;', cb); | ||
}); | ||
tasks.push(function(cb) { | ||
dbCon.release(); | ||
cb(); | ||
}); | ||
async.series(tasks, cb); | ||
} | ||
@@ -87,3 +135,3 @@ | ||
if (err) { | ||
log.error('larvitdbmigration: SQL error: ' + err.message); | ||
log.error('larvitdbmigration: Migration file: ' + items[i] + ' SQL error: ' + err.message); | ||
cb(err); | ||
@@ -90,0 +138,0 @@ return; |
@@ -38,3 +38,3 @@ { | ||
}, | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"readmeFilename": "README.md", | ||
@@ -41,0 +41,0 @@ "scripts": { |
@@ -87,2 +87,5 @@ 'use strict'; | ||
describe('Migrations', function() { | ||
this.timeout(10000); | ||
this.slow(300); | ||
it('Run them', function(done) { | ||
@@ -89,0 +92,0 @@ let dbMigrations; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16051
312