larvitdbmigration
Advanced tools
Comparing version 1.3.2 to 1.3.3
102
index.js
@@ -10,4 +10,2 @@ 'use strict'; | ||
let dbCon; | ||
exports = module.exports = function(options) { | ||
@@ -109,63 +107,73 @@ options = options || {}; | ||
log.verbose('larvitdbmigration: runScripts() - Started with startVersion: "' + startVersion + '" in path: "' + options.migrationScriptsPath + '" for table ' + options.tableName); | ||
fs.readdir(options.migrationScriptsPath, function(err, items) { | ||
const sql = 'UPDATE `' + options.tableName + '` SET version = ' + parseInt(startVersion) + ';'; | ||
let localDbConf; | ||
try { | ||
fs.readdir(options.migrationScriptsPath, function(err, items) { | ||
const sql = 'UPDATE `' + options.tableName + '` SET version = ' + parseInt(startVersion) + ';'; | ||
if (err) { | ||
log.info('larvitdbmigration: runScripts() - Could not read migration script path "' + options.migrationScriptsPath + '"'); | ||
cb(); | ||
return; | ||
} | ||
let localDbConf; | ||
for (let i = 0; items[i] !== undefined; i ++) { | ||
if (items[i] === startVersion + '.js') { | ||
log.info('larvitdbmigration: runScripts() - Found js migration script #' + startVersion + ' for table ' + options.tableName + ', running it now.'); | ||
require(options.migrationScriptsPath + '/' + startVersion + '.js')(function(err) { | ||
if (err) { | ||
log.error('larvitdbmigration: runScripts() - Got error running migration script ' + options.migrationScriptsPath + '/' + startVersion + '.js' + ': ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
if (err) { | ||
log.info('larvitdbmigration: runScripts() - Could not read migration script path "' + options.migrationScriptsPath + '"'); | ||
cb(); | ||
return; | ||
} | ||
log.debug('larvitdbmigration: runScripts() - Js migration script #' + startVersion + ' for table ' + options.tableName + ' ran. Updating database version and moving on.'); | ||
db.query(sql, function(err) { | ||
if (err) { cb(err); return; } | ||
for (let i = 0; items[i] !== undefined; i ++) { | ||
if (items[i] === startVersion + '.js') { | ||
log.info('larvitdbmigration: runScripts() - Found js migration script #' + startVersion + ' for table ' + options.tableName + ', running it now.'); | ||
require(options.migrationScriptsPath + '/' + startVersion + '.js')(function(err) { | ||
if (err) { | ||
log.error('larvitdbmigration: runScripts() - Got error running migration script ' + options.migrationScriptsPath + '/' + startVersion + '.js' + ': ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
runScripts(parseInt(startVersion) + 1, cb); | ||
log.debug('larvitdbmigration: runScripts() - Js migration script #' + startVersion + ' for table ' + options.tableName + ' ran. Updating database version and moving on.'); | ||
db.query(sql, function(err) { | ||
if (err) { cb(err); return; } | ||
runScripts(parseInt(startVersion) + 1, cb); | ||
}); | ||
}); | ||
}); | ||
return; | ||
} else if (items[i] === startVersion + '.sql') { | ||
log.info('larvitdbmigration: runScripts() - Found sql migration script #' + startVersion + ' for table ' + options.tableName + ', running it now.'); | ||
return; | ||
} else if (items[i] === startVersion + '.sql') { | ||
let dbCon; | ||
localDbConf = _.cloneDeep(db.conf); | ||
localDbConf.multipleStatements = true; | ||
dbCon = mysql.createConnection(localDbConf); | ||
log.info('larvitdbmigration: runScripts() - Found sql migration script #' + startVersion + ' for table ' + options.tableName + ', running it now.'); | ||
dbCon.query(fs.readFileSync(options.migrationScriptsPath + '/' + items[i]).toString(), function(err) { | ||
if (err) { | ||
log.error('larvitdbmigration: Migration file: ' + items[i] + ' SQL error: ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
localDbConf = _.cloneDeep(db.conf); | ||
localDbConf.multipleStatements = true; | ||
dbCon = mysql.createConnection(localDbConf); | ||
log.info('larvitdbmigration: runScripts() - Sql migration script #' + startVersion + ' for table ' + options.tableName + ' ran. Updating database version and moving on.'); | ||
db.query(sql, function(err) { | ||
if (err) { cb(err); return; } | ||
dbCon.query(fs.readFileSync(options.migrationScriptsPath + '/' + items[i]).toString(), function(err) { | ||
if (err) { | ||
log.error('larvitdbmigration: Migration file: ' + items[i] + ' SQL error: ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
runScripts(parseInt(startVersion) + 1, cb); | ||
log.info('larvitdbmigration: runScripts() - Sql migration script #' + startVersion + ' for table ' + options.tableName + ' ran. Updating database version and moving on.'); | ||
db.query(sql, function(err) { | ||
if (err) { cb(err); return; } | ||
dbCon.end(); | ||
runScripts(parseInt(startVersion) + 1, cb); | ||
}); | ||
}); | ||
}); | ||
return; | ||
return; | ||
} | ||
} | ||
} | ||
log.info('larvitdbmigration: runScripts() - Database migrated and done. Final version is ' + (startVersion - 1) + ' in table ' + options.tableName); | ||
log.info('larvitdbmigration: runScripts() - Database migrated and done. Final version is ' + (startVersion - 1) + ' in table ' + options.tableName); | ||
// If we end up here, it means there are no more migration scripts to run | ||
cb(); | ||
}); | ||
// If we end up here, it means there are no more migration scripts to run | ||
cb(); | ||
}); | ||
} catch(err) { | ||
log.error('larvitdbmigration: runScripts() - Uncaught error: ' + err.message); | ||
cb(err); | ||
} | ||
} | ||
@@ -172,0 +180,0 @@ |
@@ -33,3 +33,3 @@ { | ||
}, | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"readmeFilename": "README.md", | ||
@@ -36,0 +36,0 @@ "scripts": { |
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
16466
335