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

Commit c7739be1 by Jonathan M. Altman

Unit tests for function and trigger migrations

1 parent d8b1565d
......@@ -750,7 +750,7 @@ module.exports = (function() {
},
dropTrigger: function(tableName, triggerName) {
var sql = 'DROP TRIGGER IF EXISTS <%= triggerName %> ON <%= tableName %> RESTRICT'
var sql = 'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT'
return Utils._.template(sql)({
triggerName: triggerName
, tableName: tableName
......@@ -787,7 +787,7 @@ module.exports = (function() {
dropFunction: function(functionName, params) {
// RESTRICT is (currently, as of 9.2) default but we'll be explicit
var sql = 'DROP FUNCTION IF EXISTS <%= functionName %>(<%= paramList %>) RESTRICT';
var sql = 'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT';
return Utils._.template(sql)({
functionName: functionName,
paramList: this.expandFunctionParamList(params)
......
......@@ -507,7 +507,7 @@ module.exports = (function() {
return queryAndEmit.call(this, sql, 'renameTrigger')
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('dropTrigger', null)
this.emit('renameTrigger', null)
emitter.emit('success')
}).run()
}
......
module.exports = {
up: function(migration, DataTypes, done) {
migration.createFunction('get_an_answer', [], 'int', 'plpgsql',
'RETURN 42;'
).complete(done);
},
down: function(migration, DataTypes, done) {
migration.dropFunction('get_an_answer', []).complete(done);
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration.renameFunction('get_an_answer', [], 'get_the_answer').complete(done);
},
down: function(migration, DataTypes, done) {
migration.renameFunction('get_the_answer', [], 'get_an_answer').complete(done);
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration.dropFunction('get_the_answer', []).complete(done);
},
down: function(migration, DataTypes, done) {
migration.createFunction('get_the_answer', 'int', 'plpgsql',
'RETURN 42;'
).complete(done);
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration
.createTable('trigger_test', {
name: DataTypes.STRING,
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
}
})
.complete(done)
},
down: function(migration, DataTypes, done) {
migration.dropTable('trigger_test').complete(done)
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration.createTrigger('trigger_test', 'updated_at', 'before', {update: true},
'bump_updated_at', []).complete(done);
},
down: function(migration, DataTypes, done) {
migration.dropTrigger('trigger_test', 'updated_at').complete(done);
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration.renameTrigger('trigger_test', 'updated_at', 'update_updated_at').complete(done);
},
down: function(migration, DataTypes, done) {
migration.renameTrigger('trigger_test', 'update_updated_at', 'updated_at').complete(done);
}
}
module.exports = {
up: function(migration, DataTypes, done) {
migration.dropTrigger('trigger_test', 'update_updated_at').complete(done);
},
down: function(migration, DataTypes, done) {
migration.createTrigger('trigger_test', 'update_updated_at', 'before', {update: true},
'bump_updated_at', []).complete(done);
}
}
......@@ -14,7 +14,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
logging: function(){}
}, options || {})
// this.sequelize.options.logging = console.log
//this.sequelize.options.logging = console.log
var migrator = new Migrator(this.sequelize, options)
migrator
......@@ -28,7 +28,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
describe('getUndoneMigrations', function() {
it("returns no files if timestamps are after the files timestamp", function(done) {
this.init({ from: 20120101010101 }, function(migrator) {
this.init({ from: 20140101010101 }, function(migrator) {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).to.be.null
expect(migrations.length).to.equal(0)
......@@ -86,7 +86,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
SequelizeMeta.create({ from: null, to: 20111117063700 }).success(function() {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).to.be.null
expect(migrations).to.have.length(7)
expect(migrations).to.have.length(15)
expect(migrations[0].filename).to.equal('20111130161100-emptyMigration.js')
done()
})
......@@ -286,7 +286,8 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
})
describe('renameColumn', function() {
it("renames the signature column from user to sig", function(done) {
......@@ -307,5 +308,145 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
if (dialect.match(/^postgres/)) {
describe('function migrations', function(done) {
var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, languageOid) {
return 'SELECT * FROM pg_proc where prolang = ' + languageOid + ' AND proname = \'' + functionName + '\'';
};
var FUNC_NAME = 'get_an_answer';
var RENAME_FUNC_NAME = 'get_the_answer';
// Set up the table and trigger
before(function(done){
var self = this
this.init({ from: 20130909174103, to: 20130909174103}, function(migrator) {
migrator.migrate().success(function(){
done();
})
})
})
it("creates a function " + FUNC_NAME + "()", function(done) {
var self = this;
this.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 11771)).success(function(rows){
expect(rows.length).to.equal(1)
done()
})
})
it("renames a function " + FUNC_NAME + "() to " + RENAME_FUNC_NAME + "()", function(done) {
var self = this;
this.init({ from: 20130909174253, to: 20130909174253 }, function(migrator) {
migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 11771)).success(function(rows){
expect(rows.length).to.equal(0);
self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 11771)).success(function(rows){
expect(rows.length).to.equal(1);
done()
})
})
})
})
})
it("deletes a function " + RENAME_FUNC_NAME + "()", function(done) {
var self = this;
this.init({ from: 20130909175000, to: 20130909175000 }, function(migrator) {
migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 11771)).success(function(rows){
expect(rows.length).to.equal(0);
done();
})
})
})
})
})
describe('test trigger migrations', function(done) {
var generateTriggerCountQuery = function generateTriggerCountQuery(triggerName) {
return 'SELECT * FROM pg_trigger where tgname = \'' + triggerName + '\'';
};
var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, schemaName) {
return 'SELECT * FROM pg_tables where tablename = \'' + functionName + '\' and schemaname = \'' + schemaName + '\'';
};
var TRIGGER_NAME = 'updated_at';
var RENAME_TRIGGER_NAME = 'update_updated_at';
var TABLE_NAME = 'trigger_test'
var CATALOG_NAME = 'public'
// Make sure the function is present
before(function(done){
this.sequelize.query("CREATE FUNCTION bump_updated_at()\n" +
"RETURNS TRIGGER AS $$\n" +
"BEGIN\n" +
"NEW.updated_at = now();\n" +
"RETURN NEW;\n" +
"END;\n" +
"$$ language 'plpgsql';"
).success(function() {done()});
})
// Clean up the function
after(function(done){
this.sequelize.query("DROP FUNCTION IF EXISTS bump_updated_at()").success(function(){ done(); });
})
it("creates a trigger updated_at on trigger_test", function(done) {
var self = this;
this.init({ from: 20130909175939, to: 20130909180846}, function(migrator) {
migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(1);
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(1);
done();
})
})
})
})
})
it("renames a trigger on " + TABLE_NAME + " from " + TRIGGER_NAME + " to " + RENAME_TRIGGER_NAME, function(done){
var self = this;
this.init({ from: 20130909175939, to: 20130909181148}, function(migrator) {
migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(1);
self.sequelize.query(generateTriggerCountQuery(RENAME_TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(1);
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(0);
done();
})
})
})
})
})
})
it("deletes a trigger " + TRIGGER_NAME + " on trigger_test", function(done) {
var self = this;
this.init({ from: 20130909175939, to: 20130909185621}, function(migrator) {
migrator.migrate().success(function(){
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(0);
migrator.migrate({method: 'down'}).success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(0);
done();
})
})
})
})
})
})
})
} // if dialect postgres
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!