Socket
Socket
Sign inDemoInstall

larvitdbmigration

Package Overview
Dependencies
Maintainers
1
Versions
170
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitdbmigration - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

89

dbMigrations.js
'use strict';
var mysql = require('mysql'),
async = require('async'),
var async = require('async'),
log = require('winston'),
fs = require('fs'),
db = require('larvitdb'),
_ = require('lodash');

@@ -17,3 +17,3 @@

log.verbose('larvitdbmigration: Started with tableName: ' + options.tableName + ' and migrationScriptsPath: ' + options.migrationScriptsPath);
log.verbose('larvitdbmigration: Started with options: ' + JSON.stringify(options));

@@ -25,10 +25,6 @@ // Resolve ./ paths to be relative to application path

// We need to use a new connection to be sure it is not dropped on heavy migration scripts
return function(cb) {
var dbCon = mysql.createConnection(options),
tasks = [],
var tasks = [],
curVer;
dbCon.connect();
function runScripts(startVersion, cb) {

@@ -58,5 +54,5 @@ log.verbose('larvitdbmigration: runScripts() - Started with startVersion: "' + startVersion + '"');

log.info('larvitdbmigration: runScripts() - Migration script #' + startVersion + ' ran. Update database and move on.');
log.info('larvitdbmigration: runScripts() - Migration script #' + startVersion + ' ran. Update database version and move on.');
log.debug('larvitdbmigration: runScripts() - Running SQL: "' + sql + '"');
dbCon.query(sql, function(err) {
db.query(sql, function(err) {
if (err) {

@@ -86,3 +82,3 @@ cb(err);

log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, function(err, rows) {
db.query(sql, function(err, rows) {
var customErr;

@@ -98,5 +94,15 @@

} else if (rows.length === 0) {
sql = 'CREATE TABLE `' + options.tableName + '` (`version` int unsigned NOT NULL DEFAULT \'0\') ENGINE=\'InnoDB\' COLLATE \'ascii_bin\';';
sql = 'CREATE TABLE `' + options.tableName + '` (`version` int(10) unsigned NOT NULL DEFAULT \'0\', `running` tinyint(3) unsigned NOT NULL DEFAULT \'0\') ENGINE=InnoDB DEFAULT CHARSET=ascii COLLATE=ascii_bin;';
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, cb);
db.query(sql, function(err) {
var sql = 'INSERT INTO `' + options.tableName + '` (version, running) VALUES(0, 0);';
if (err) {
cb(err);
return;
}
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
db.query(sql, cb);
});
} else {

@@ -110,14 +116,40 @@ customErr = new Error('larvitdbmigration: SHOW TABLES like \'' + options.tableName + '\'; returned either 0 or 1 rows, but: "' + rows.length + '"');

// Lock table
// Lock table by setting the running column to 1
tasks.push(function(cb) {
var sql = 'LOCK TABLES `' + options.tableName + '` WRITE;';
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, cb);
function getLock(cb) {
db.query('SELECT running FROM `' + options.tableName + '`;', function(err, rows) {
if (err) {
cb(err);
return;
}
if (parseInt(rows[0].running) === 1) {
log.verbose('larvitdbmigration: Another process is running the migrations, wait and try again soon.');
setTimeout(function() {
getlock(cb);
}, 500);
} else {
cb();
}
});
}
getLock(function(err) {
var sql = 'UPDATE `' + options.tableName + '` SET running = 1;';
if (err) {
cb(err);
return;
}
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
db.query(sql, cb);
});
});
// Insert first row if it does not exist
// Get current version
tasks.push(function(cb) {
var sql = 'SELECT version FROM `' + options.tableName + '`;';
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, function(err, rows) {
db.query(sql, function(err, rows) {
if (err) {

@@ -128,13 +160,4 @@ cb(err);

if (rows.length === 0) {
curVer = '0';
sql = 'INSERT INTO `' + options.tableName + '` (version) VALUES(0);';
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, cb);
curVer = parseInt(rows[0].version);
return;
} else {
curVer = rows[0].version;
}
cb();

@@ -146,3 +169,3 @@ });

tasks.push(function(cb) {
runScripts(parseInt(curVer) + 1, cb);
runScripts(curVer + 1, cb);
});

@@ -152,9 +175,9 @@

tasks.push(function(cb) {
var sql = 'UNLOCK TABLES;';
var sql = 'UPDATE `' + options.tableName + '` SET running = 0;';
log.debug('larvitdbmigration: Running SQL: "' + sql + '"');
dbCon.query(sql, cb);
db.query(sql, cb);
});
async.series(tasks, function(err) {
dbCon.end();
db.end();

@@ -161,0 +184,0 @@ cb(err);

@@ -33,3 +33,3 @@ {

},
"version": "0.1.0",
"version": "0.2.0",
"readmeFilename": "README.md",

@@ -36,0 +36,0 @@ "scripts": {},

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc