Skip to content
Browse files

Unify code style.

  • Loading branch information...
1 parent 49db530 commit c2fcb2077e5a1d76bafeac7dcdf37a759c9348d0 @uu1101 committed
Showing with 102 additions and 95 deletions.
  1. +11 −6 lib/config.js
  2. +10 −12 lib/create.js
  3. +6 −5 lib/downgrade.js
  4. +32 −31 lib/migration.js
  5. +34 −33 lib/migrations.js
  6. +9 −8 lib/upgrade.js
View
17 lib/config.js
@@ -8,14 +8,19 @@ var config_path = 'config.js'
, exists = fspath.existsSync(config_path)
if(!exists) {
- console.log('Could not find configuration file "' + config_path + '", using default parameters.');
- module.exports = default_config;
+ console.log( 'Could not find configuration file'
+ + ' "' + config_path + '",'
+ + ' using default parameters.'
+ )
+ module.exports = default_config
} else {
- var config = require(config_path);
+ var config = require(config_path)
if(!config.migrations) {
- console.log('Configuration file does not export a `migrations` property, using default parameters.');
- module.exports = default_config;
+ console.log( 'Configuration file does not export a `migrations` property,
+ + ' using default parameters.'
+ )
+ module.exports = default_config
} else {
- module.exports = config.migrations;
+ module.exports = config.migrations
}
}
View
22 lib/create.js
@@ -3,16 +3,13 @@ var fspath = require('path')
var config = require('./config')
-// Check there is a description; slashify.
var touch = function(path) {
- fs.closeSync(fs.openSync(path, 'w', 0644));
- console.log('Created ' + path);
-};
+ fs.closeSync(fs.openSync(path, 'w', 0644))
+ console.log('Created ' + path)
+}
module.exports = function(args, next) {
- if(!args.length) {
- next('Must supply a description');
- }
+ if(!args.length) return next('Must supply a description')
var description = args.join('-')
, time = Date.now().toString()
@@ -20,14 +17,15 @@ module.exports = function(args, next) {
, migration_path = fspath.join(config.migrations_directory,
directory_name)
, up_path = fspath.join(migration_path, 'up.sql')
- , down_path = fspath.join(migration_path, 'down.sql');
+ , down_path = fspath.join(migration_path, 'down.sql')
fs.mkdir(migration_path, 0755, function(err) {
if(err) return next( 'Could not create directory "'
+ migration_path
- + '".');
- console.log('Created ' + migration_path);
- [up_path, down_path].map(touch);
- next(null);
+ + '".'
+ )
+ console.log('Created ' + migration_path)
+ ;[up_path, down_path].map(touch)
+ next(null)
})
}
View
11 lib/downgrade.js
@@ -2,13 +2,14 @@ var m = require('./migrations')
module.exports = function(next) {
m.applied(function(err, applied_migrations) {
- if(err) return next(err);
+ if(err) return next(err)
+
if(!applied_migrations.length) {
- return next('There aren\'t any applied migrations.');
+ return next('There aren\'t any applied migrations.')
}
- var a_m = applied_migrations[applied_migrations.length - 1];
- console.log('Reverting ' + a_m.id);
- a_m.downgrade(next);
+ var a_m = applied_migrations[applied_migrations.length - 1]
+ console.log('Reverting ' + a_m.id)
+ a_m.downgrade(next)
})
}
View
63 lib/migration.js
@@ -5,74 +5,75 @@ var fspath = require('path')
var config = require('./config')
var Migration = function(directory_name) {
- this.id = parseInt(directory_name);
+ this.id = parseInt(directory_name, 10)
var path_prefix = fspath.join(this.migrations_directory, directory_name)
- this.js = {
- path: './' + fspath.join(path_prefix, 'index.js'),
- up: 'upgrade',
- down: 'downgrade'
- }
-
- this.up = {
- path: fspath.join(path_prefix, 'up.sql'),
- sql: "INSERT INTO " + this.migrations_table + " VALUES (" + this.id + ");",
- }
- this.down = {
- path: fspath.join(path_prefix, 'down.sql'),
- sql: "DELETE FROM " + this.migrations_table + " WHERE id = " + this.id + ";",
- }
+ this.js = { path: './' + fspath.join(path_prefix, 'index.js')
+ , up: 'upgrade'
+ , down: 'downgrade'
+ }
+
+ this.up = { path: fspath.join(path_prefix, 'up.sql')
+ , sql: "INSERT INTO " + this.migrations_table
+ + " VALUES (" + this.id + ");"
+ }
+ this.down = { path: fspath.join(path_prefix, 'down.sql')
+ , sql: "DELETE FROM " + this.migrations_table
+ + " WHERE id = " + this.id + ";"
+ }
}
-Migration.prototype = config;
+Migration.prototype = config
Migration.prototype.wrap_transaction = function(sql) {
- return "START TRANSACTION; " + sql + "COMMIT;";
+ return "START TRANSACTION; " + sql + "COMMIT;"
}
-Migration.prototype.execute = function(config, values, cb) {
+Migration.prototype.execute = function(config, values, next) {
var args = arguments
pg.connect(this.database_url, function(err, client) {
- err && cb(err);
- client.query.apply(client, args);
+ if(err) return next(err)
+
+ client.query.apply(client, args)
})
}
Migration.prototype.run_sql = function(direction, next) {
var self = this
, path = self[direction].path
+
fspath.exists(path, function(exists) {
- if(!exists) return next("Can't find '" + path + "'.");
+ if(!exists) return next("Can't find '" + path + "'.")
fs.readFile(path, function(err, content) {
- if(err) return next(err);
+ if(err) return next(err)
var sql = content.toString() + ';'
+ self[direction].sql
, transaction = self.wrap_transaction(sql)
- self.execute(transaction, next);
+ self.execute(transaction, next)
})
})
}
-Migration.prototype.upgrade_sql = function(next) { this.run_sql('up', next); }
-Migration.prototype.downgrade_sql = function(next) { this.run_sql('down', next); }
+Migration.prototype.upgrade_sql = function(next) { this.run_sql('up', next) }
+Migration.prototype.downgrade_sql = function(next) { this.run_sql('down', next) }
Migration.prototype.run_migration = function(direction, next) {
- var self = this;
+ var self = this
fspath.exists(self.js.path, function(exists) {
if(exists) {
require(self.js.path)[self.js[direction]].call(self, function(err) {
- if(err) return next(err);
- self.execute(self[direction].sql, next);
+ if(err) return next(err)
+ self.execute(self[direction].sql, next)
})
} else {
- self.run_sql(direction, next);
+ self.run_sql(direction, next)
}
})
}
-Migration.prototype.upgrade = function(next) { this.run_migration('up', next); }
-Migration.prototype.downgrade = function(next) { this.run_migration('down', next); }
+Migration.prototype.upgrade = function(next) { this.run_migration('up', next) }
+Migration.prototype.downgrade = function(next) { this.run_migration('down', next) }
module.exports = Migration
View
67 lib/migrations.js
@@ -6,77 +6,78 @@ var async = require('async')
var config = require('./config')
, Migration = require('./migration')
-var pending_migrations
- , applied_migrations
- , migrations_db
-
var applied_ids = async.memoize(function(next) {
var sql = "SELECT id "
+ "FROM " + config.migrations_table + " "
- + "ORDER BY id ";
+ + "ORDER BY id "
pg.connect(config.database_url, function(err, client) {
- if(err) return next(err);
+ if(err) return next(err)
client.query(sql, function(err, result) {
- if(err) return next(err);
- next(null, result.rows);
- });
- });
+ if(err) return next(err)
+ next(null, result.rows)
+ })
+ })
})
var all = async.memoize(function(next) {
fs.readdir(config.migrations_directory, function(err, filenames) {
- if(err) return next(err);
+ if(err) return next(err)
var is_directory = function(filename, next) {
- var path = fspath.join(config.migrations_directory, filename);
+ var path = fspath.join(config.migrations_directory, filename)
fs.stat(path, function(err, stats) {
- if(err) return next(null); // doesn't accept error arg
- next(stats.isDirectory());
+ if(err) return next(null) // doesn't accept error arg
+ next(stats.isDirectory())
})
}
- async.filter(filenames ,is_directory
- ,function(directories) {
- var ms = directories.map(function(directory) {
- return new Migration(directory);
- })
- next(null, ms);
- })
+ async.filter( filenames ,is_directory
+ , function(directories) {
+ var ms = directories.map(function(directory) {
+ return new Migration(directory)
+ })
+ next(null, ms)
+ }
+ )
})
})
var filtered = function(condition, next) {
all(function(err, migrations) {
- if(err) return next(err);
+ if(err) return next(err)
var sorted_migrations = migrations.sort(function(x, y) {
- return x.id > y.id;
+ return x.id > y.id
})
- , filtered_migrations = sorted_migrations.filter(condition);
+ , filtered_migrations = sorted_migrations.filter(condition)
- next(null, filtered_migrations);
+ next(null, filtered_migrations)
})
}
var pending = async.memoize(function(next) {
applied_ids(function(err, ids) {
- if(err) return next(err);
+ if(err) return next(err)
- filtered(function(m) {
- return ids.every(function(a) { return a.id != m.id })
- }, next);
+ filtered( function(m) {
+ return ids.every(function(a) { return a.id != m.id })
+ }
+ , next
+ )
})
})
var applied = async.memoize(function(next) {
applied_ids(function(err, ids) {
- if(err) return next(err);
+ if(err) return next(err)
- filtered(function(m) {
- return ids.some(function(a) { return a.id == m.id })
- }, next);
+ filtered( function(m) {
+ return ids.some(function(a) { return a.id == m.id })
+ }
+ , next
+ )
})
})
View
17 lib/upgrade.js
@@ -3,17 +3,18 @@ var m = require('./migrations')
module.exports = function(next) {
m.pending(function(err, pending_migrations) {
- if(err) return next(err);
+ if(err) return next(err)
+
if(!pending_migrations.length) {
- return next('There aren\'t any pending migrations.');
+ return next('There aren\'t any pending migrations.')
}
- async.forEachSeries(pending_migrations
- ,function(p_m, next) {
- console.log( 'Upgrading to ' + p_m.id);
- p_m.upgrade(next);
- }
- ,next
+ async.forEachSeries( pending_migrations
+ , function(p_m, next) {
+ console.log( 'Upgrading to ' + p_m.id)
+ p_m.upgrade(next)
+ }
+ , next
)
})
}

0 comments on commit c2fcb20

Please sign in to comment.
Something went wrong with that request. Please try again.