Skip to content

Commit

Permalink
Basic migrate up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kunkle committed Dec 13, 2011
1 parent a6ed47b commit ab27b1b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
migrations/
node_modules/
*.db
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test
migrations
node_modules
database.json
*.db
3 changes: 3 additions & 0 deletions bin/db-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ var argv = optimist
.default({
verbose: false,
db: process.cwd() + '/database.json',
'migrations-dir': process.cwd() + '/migrations',
env: 'dev' })
.usage('Usage: db-migrate [up|down|create] migrationName [options]')
.alias('e', 'env')
.alias('m', 'migrations-dir')
.alias('v', 'verbose')
.alias('h', 'help')
.alias('h', '?')
.describe('env', 'The environment to run the migrations under (dev, test, prod).')
.describe('migrations-dir', 'The directory containing your migration files.')
.describe('db', 'Location of the database.json file.')
.describe('verbose', 'Verbose mode.')
.argv
Expand Down
71 changes: 69 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
var util = require('util');
var Migration = require('./migration');
var config = require('./config');
var fs = require('fs');
var path = require('path');

exports.create = function(options) {
var migration = new Migration(options.title);
var migration = new Migration(options.title, new Date());
migration.write(function(err) {
if (err) {
console.error('[FAILED]', err);
Expand All @@ -12,9 +16,72 @@ exports.create = function(options) {
};

exports.up = function(options) {

var env = config.getCurrent();
var driver = require('./driver/' + env.driver);
driver.connect(env, function(err, db) {
if (err) { console.error('[FAILED]', err); return; }
db.createMigrationsTable(function(err) {
if (err) { console.error('[FAILED]', err); return; }
db.all('SELECT * FROM migrations ORDER BY name', function(err, dbResults) {
if (err) { console.error('[FAILED]', err); return; }
fs.readdir(options['migrations-dir'], function(err, files) {
if (err) { console.error('[FAILED]', err); return; }
files = files.sort();
files = files.filter(function(file) {
return /\.js$/.test(file);
});
files.forEach(function(file) {
if (verbose) {
console.log('processing file ' + file);
}
var migration = new Migration(path.join(options['migrations-dir'], file));
var hasRun = dbResults.some(function(result) {
return result.name === migration.name;
});
if (!hasRun) {
migration.up(db, function(err) {
if (err) { console.error('[FAILED]', err); return; }
});
}
});
});
});
});
});
};

exports.down = function(options) {
var env = config.getCurrent();
var driver = require('./driver/' + env.driver);
driver.connect(env, function(err, db) {
if (err) { console.error('[FAILED]', err); return; }
db.createMigrationsTable(function(err) {
if (err) { console.error('[FAILED]', err); return; }
db.all('SELECT * FROM migrations ORDER BY name DESC', function(err, dbResults) {
if (err) { console.error('[FAILED]', err); return; }
fs.readdir(options['migrations-dir'], function(err, files) {
if (err) { console.error('[FAILED]', err); return; }
files = files.sort();
files = files.filter(function(file) {
return /\.js$/.test(file);
});
files.forEach(function(file) {
if (verbose) {
console.log('processing file ' + file);
}
var migration = new Migration(path.join(options['migrations-dir'], file));
var hasRun = dbResults.some(function(result) {
return result.name === migration.name;
});
if (!hasRun) {
migration.up(db, function(err) {
if (err) { console.error('[FAILED]', err); return; }
});
}
});
});
});
});
});

};
8 changes: 8 additions & 0 deletions lib/driver/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var events = require('events');
module.exports = Base = function() { };
util.inherits(Base, events.EventEmitter);

Base.prototype.createMigrationsTable = function(callback) {
throw new Error('not yet implemented');
};

Base.prototype.createTable = function(tableName, columnSpecs, callback) {
throw new Error('not yet implemented');
};
Expand Down Expand Up @@ -55,6 +59,10 @@ Base.prototype.runSql = function(sql, callback) {
throw new Error('not yet implemented');
};

Base.prototype.all = function(sql, params, callback) {
throw new Error('not yet implemented');
};

Base.prototype.escape = function(str) {
return str.replace(/'/g, "\'");
};
13 changes: 13 additions & 0 deletions lib/driver/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ Driver = function(connection) {
};
util.inherits(Driver, Base);

Driver.prototype.createMigrationsTable = function(callback) {
var columnDefs = [
createColumnDef('name', { type: type.STRING, notNull: true, primaryKey: true}),
createColumnDef('run_on', { type: type.DATE_TIME, notNull: true}),
];
var sql = util.format('CREATE TABLE IF NOT EXISTS %s (%s)', 'migrations', columnDefs.join(', '));
this.runSql(sql, callback);
};

Driver.prototype.createTable = function(tableName, columnSpecs, callback) {
var columnDefs = [];
for (var columnName in columnSpecs) {
Expand Down Expand Up @@ -122,6 +131,10 @@ Driver.prototype.runSql = function(sql, callback) {
this.connection.run(sql, callback);
};

Driver.prototype.all = function() {
this.connection.all.apply(this.connection, arguments);
};

exports.connect = function(config, callback) {
var mode = config.mode || defaultMode;
var db = new sqlite3.Database(config.filename, mode);
Expand Down
7 changes: 0 additions & 7 deletions lib/migrate.js

This file was deleted.

13 changes: 11 additions & 2 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ Migration = function() {
};

Migration.prototype.write = function(callback) {
fs.writeFileSync(this.path, migrationTemplate);
return this;
fs.writeFile(this.path, migrationTemplate, callback);
};

Migration.prototype.up = function(db, callback) {
var migrationFile = require(this.path);
migrationFile.up(db, callback);
};

Migration.prototype.down = function(db, callback) {
var migrationFile = require(this.path);
migrationFile.down(db, callback);
};

module.exports = Migration;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"dependencies": {
"sqlite3": "~2.1.1",
"mysql": "~0.9.5"
"mysql": "~0.9.5",
"optimist": "~0.3.0"
},
"devDependencies": {
"vows": "~0.5.13",
Expand Down
5 changes: 5 additions & 0 deletions test/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"test": {
"driver": "sqlite3",
"filename": ":memory:"
},

"prod": {
"driver": "sqlite3",
"filename": "prod.db"
}
}

0 comments on commit ab27b1b

Please sign in to comment.