不要怂,就是干,撸起袖子干!

Commit e0679969 by sdepold

extracted migration specific logic to separate class

1 parent 04c9410e
var moment = require("moment")
, Utils = require("./utils")
module.exports = (function() {
var Migration = function(path) {
var split = path.split('/')
this.path = path
this.filename = Utils._.last(this.path.split('/'))
this.date = Migration.stringToDate(split[split.length - 1])
}
///////////////
// static /////
///////////////
Migration.getFormattedDateString = function(s) {
var result = null
try {
result = s.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/).slice(1, 6).join('-')
} catch(e) {
throw new Error(s + ' is no valid migration timestamp format! Use YYYYMMDDHHmmss!')
}
return result
}
Migration.stringToDate = function(s) {
return moment(Migration.getFormattedDateString(s), "YYYYMMDDHHmmss")
}
///////////////
// member /////
///////////////
Migration.prototype.execute = function() {
}
Migration.prototype.isBefore = function(dateString, options) {
options = Utils._.extend({
withoutEquals: false
}, options || {})
var date = Migration.stringToDate(dateString.toString())
return options.withoutEqual ? (date > this.date) : (date >= this.date)
}
Migration.prototype.isAfter = function(dateString, options) {
options = Utils._.extend({
withoutEquals: false
}, options || {})
var date = Migration.stringToDate(dateString.toString())
return options.withoutEqual ? (date < this.date) : (date <= this.date)
}
return Migration
})()
......@@ -10,7 +10,7 @@ var Utils = require("./utils")
module.exports = (function() {
var Migrator = function(sequelize, options) {
this.sequelize = sequelize
this.queryInterface = new QueryInterface(this.sequelize)
this.queryInterface = sequelize.getQueryInterface()
this.options = Utils._.extend({
path: __dirname + '/../migrations',
from: null,
......@@ -22,8 +22,8 @@ module.exports = (function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.getUndoneMigrations(function(migrationFiles) {
migrationFiles.forEach(function() { self.executeMigration() })
self.getUndoneMigrations(function(migrations) {
console.log(migrations)
emitter.emit('success')
})
}).run()
......@@ -32,50 +32,39 @@ module.exports = (function() {
Migrator.prototype.getUndoneMigrations = function(callback) {
var self = this
var filterFrom = function(files, from, callback, options) {
var fromDate = stringToDate(from.toString())
, result = []
result = files.filter(function(file) {
var fileDate = stringToDate(file.split("-")[0])
return (options||{}).withoutEqual ? (fileDate > fromDate) : (fileDate >= fromDate)
})
var filterFrom = function(migrations, from, callback, options) {
var result = migrations.filter(function(migration) { return migration.isAfter(from, options) })
callback && callback(result)
}
var filterTo = function(files, to, callback) {
var toDate = stringToDate(to.toString())
, result = []
result = files.filter(function(file) {
var fileDate = stringToDate(file.split("-")[0])
return (fileDate <= toDate)
})
var filterTo = function(migrations, to, callback, options) {
var result = migrations.filter(function(migration) { return migration.isBefore(to, options) })
callback && callback(result)
}
var migrationFiles = fs.readdirSync(this.options.path)
var migrations = migrationFiles.map(function(file) {
return new Migration(self.options.path + '/' + file)
})
if(this.options.from) {
filterFrom(migrationFiles, this.options.from, function(files) {
filterFrom(migrations, this.options.from, function(migrations) {
if(self.options.to)
filterTo(files, self.options.to, callback)
filterTo(migrations, self.options.to, callback)
else
callback && callback(files)
callback && callback(migrations)
})
} else {
getLastMigrationIdFromDatabase.call(this).success(function(lastMigrationId) {
if(lastMigrationId) {
filterFrom(migrationFiles, lastMigrationId, function(files) {
filterFrom(migrations, lastMigrationId, function(migrations) {
if(self.options.to)
filterTo(files, self.options.to, callback)
filterTo(migrations, self.options.to, callback)
else
callback && callback(files)
callback && callback(migrations)
}, { withoutEqual: true })
} else {
callback && callback(migrationFiles)
callback && callback(migrations)
}
})
}
......@@ -103,6 +92,7 @@ module.exports = (function() {
}
Migrator.prototype.executeMigration = function(path, method) {
console.log(path)
var migration = require(path)
migration[method || 'up'](this.queryInterface, DataTypes)
}
......@@ -137,5 +127,29 @@ module.exports = (function() {
return moment(getFormattedDateString(s), "YYYYMMDDHHmmss")
}
var observeQueryInterfaceForSuccess = function(callback) {
var self = this
for(var method in QueryInterface.prototype) {
self[method] = function() {
var emitter = self.queryInterface
// bind listeners to the query interface
// the event will have the same name like the method
self.queryInterface.on(method, function(err) {
if(err) {
throw new Error(err)
} else {
}
})
}
}
console.log(proto)
//this.queryInterface.prototype.each ...
}
return Migrator
})()
......@@ -3,6 +3,7 @@ var config = require("./config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
, Helpers = new (require("./config/helpers"))(sequelize)
, Migrator = require("../lib/migrator")
, _ = Sequelize.Utils._
describe('Migrator', function() {
describe('getUndoneMigrations', function() {
......@@ -31,8 +32,8 @@ describe('Migrator', function() {
setup({ from: 20120101010101 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(files) {
expect(files.length).toEqual(0)
migrator.getUndoneMigrations(function(migrations) {
expect(migrations.length).toEqual(0)
done()
})
})
......@@ -42,9 +43,9 @@ describe('Migrator', function() {
setup({ from: 19700101000000, to: 20111117063700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(files) {
expect(files.length).toEqual(1)
expect(files[0]).toEqual('20111117063700-createPerson.js')
migrator.getUndoneMigrations(function(migrations) {
expect(migrations.length).toEqual(1)
expect(_.last(migrations).filename).toEqual('20111117063700-createPerson.js')
done()
})
})
......@@ -54,10 +55,10 @@ describe('Migrator', function() {
setup({ from: 20111117063700, to: 20111123060700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(files) {
expect(files.length).toEqual(2)
expect(files[0]).toEqual('20111117063700-createPerson.js')
expect(files[1]).toEqual('20111123060700-addBirthdateToPerson.js')
migrator.getUndoneMigrations(function(migrations) {
expect(migrations.length).toEqual(2)
expect(migrations[0].filename).toEqual('20111117063700-createPerson.js')
expect(migrations[1].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
})
......@@ -67,8 +68,8 @@ describe('Migrator', function() {
setup({ to: 20111123060700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(files) {
expect(files.length).toEqual(2)
migrator.getUndoneMigrations(function(migrations) {
expect(migrations.length).toEqual(2)
done()
})
})
......@@ -79,9 +80,9 @@ describe('Migrator', function() {
Helpers.async(function(done) {
SequelizeMeta.create({ lastMigrationId: '20111117063700' }).success(function() {
migrator.getUndoneMigrations(function(files) {
expect(files.length).toEqual(1)
expect(files[0]).toEqual('20111123060700-addBirthdateToPerson.js')
migrator.getUndoneMigrations(function(migrations) {
expect(migrations.length).toEqual(1)
expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
})
......@@ -99,16 +100,23 @@ describe('Migrator', function() {
})
})
it("should execute the migrations", function() {
/*Helpers.async(function(done) {
new Migrator(sequelize, {
path: __dirname + '/assets/migrations',
from: 2011111706370020111117063700,
to: 20111117063700
}).migrate().success(function() {
xit("should execute the specified migration (and e.g. create a file)", function() {
var migrator = new Migrator(sequelize, {
path: __dirname + '/assets/migrations',
from: 20111117063700,
to: 20111117063700
})
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables(function(tableName) {
expect(tableNames.length).toEqual(1)
done()
})
})*/
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!