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

Commit 9a341ed6 by Jonathan M. Altman

Fixed failing tests, tried to match style guidelines

1 parent b7489045
...@@ -728,36 +728,36 @@ module.exports = (function() { ...@@ -728,36 +728,36 @@ module.exports = (function() {
, '<%= eventType %> <%= eventSpec %>' , '<%= eventType %> <%= eventSpec %>'
, 'ON <%= tableName %>' , 'ON <%= tableName %>'
, '<%= optionsSpec %>' , '<%= optionsSpec %>'
, 'EXECUTE PROCEDURE <%= functionName %>(<%= paramList %>)' , 'EXECUTE PROCEDURE <%= functionName %>(<%= paramList %>);'
].join('\n\t'); ].join('\n\t')
return Utils._.template(sql)({ return Utils._.template(sql)({
constraintVal: this.triggerEventTypeIsConstraint(eventType) constraintVal: this.triggerEventTypeIsConstraint(eventType),
, triggerName: triggerName triggerName: triggerName,
, eventType: this.decodeTriggerEventType(eventType) eventType: this.decodeTriggerEventType(eventType),
, eventSpec: this.expandTriggerEventSpec(fireOnSpec) eventSpec: this.expandTriggerEventSpec(fireOnSpec),
, tableName: tableName tableName: tableName,
, optionsSpec: this.expandOptions(optionsArray) optionsSpec: this.expandOptions(optionsArray),
, functionName: functionName functionName: functionName,
, paramList: this.expandFunctionParamList(functionParams) paramList: this.expandFunctionParamList(functionParams)
}) })
}, },
dropTrigger: function(tableName, triggerName) { dropTrigger: function(tableName, triggerName) {
var sql = 'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT' var sql = 'DROP TRIGGER <%= triggerName %> ON <%= tableName %> RESTRICT;'
return Utils._.template(sql)({ return Utils._.template(sql)({
triggerName: triggerName triggerName: triggerName,
, tableName: tableName tableName: tableName
}); })
}, },
renameTrigger: function(tableName, oldTriggerName, newTriggerName) { renameTrigger: function(tableName, oldTriggerName, newTriggerName) {
var sql = 'ALTER TRIGGER <%= oldTriggerName %> ON <%= tableName %> RENAME TO <%= newTriggerName%>' var sql = 'ALTER TRIGGER <%= oldTriggerName %> ON <%= tableName %> RENAME TO <%= newTriggerName%>;'
return Utils._.template(sql)({ return Utils._.template(sql)({
tableName: tableName tableName: tableName,
, oldTriggerName: oldTriggerName oldTriggerName: oldTriggerName,
, newTriggerName: newTriggerName newTriggerName: newTriggerName
}); })
}, },
createFunction: function(functionName, params, returnType, language, body, options) { createFunction: function(functionName, params, returnType, language, body, options) {
...@@ -767,7 +767,7 @@ module.exports = (function() { ...@@ -767,7 +767,7 @@ module.exports = (function() {
, "\t<%= body %>" , "\t<%= body %>"
, "END;" , "END;"
, "$$ language '<%= language %>'<%= options %>;" , "$$ language '<%= language %>'<%= options %>;"
].join('\n'); ].join('\n')
return Utils._.template(sql)({ return Utils._.template(sql)({
functionName: functionName, functionName: functionName,
...@@ -781,21 +781,20 @@ module.exports = (function() { ...@@ -781,21 +781,20 @@ module.exports = (function() {
dropFunction: function(functionName, params) { dropFunction: function(functionName, params) {
// RESTRICT is (currently, as of 9.2) default but we'll be explicit // RESTRICT is (currently, as of 9.2) default but we'll be explicit
var sql = 'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT'; var sql = 'DROP FUNCTION <%= functionName %>(<%= paramList %>) RESTRICT;'
return Utils._.template(sql)({ return Utils._.template(sql)({
functionName: functionName, functionName: functionName,
paramList: this.expandFunctionParamList(params) paramList: this.expandFunctionParamList(params)
}); })
}, },
renameFunction: function(oldFunctionName, params, newFunctionName) { renameFunction: function(oldFunctionName, params, newFunctionName) {
// RESTRICT is (currently, as of 9.2) default but we'll be explicit var sql = 'ALTER FUNCTION <%= oldFunctionName %>(<%= paramList %>) RENAME TO <%= newFunctionName %>;'
var sql = 'ALTER FUNCTION <%= oldFunctionName %>(<%= paramList %>) RENAME TO <%= newFunctionName %>';
return Utils._.template(sql)({ return Utils._.template(sql)({
oldFunctionName: oldFunctionName, oldFunctionName: oldFunctionName,
paramList: this.expandFunctionParamList(params), paramList: this.expandFunctionParamList(params),
newFunctionName: newFunctionName newFunctionName: newFunctionName
}); })
}, },
databaseConnectionUri: function(config) { databaseConnectionUri: function(config) {
...@@ -817,74 +816,73 @@ module.exports = (function() { ...@@ -817,74 +816,73 @@ module.exports = (function() {
expandFunctionParamList: function expandFunctionParamList(params) { expandFunctionParamList: function expandFunctionParamList(params) {
if (Utils._.isUndefined(params) || !Utils._.isArray(params)) { if (Utils._.isUndefined(params) || !Utils._.isArray(params)) {
throw new Error("expandFunctionParamList: function parameters array required, including an empty one for no arguments"); throw new Error("expandFunctionParamList: function parameters array required, including an empty one for no arguments")
} }
var paramList = Utils._.each(params, function expandParam(curParam){ var paramList = Utils._.each(params, function expandParam(curParam){
paramDef = []; paramDef = []
if (Utils._.has(curParam, 'type')) { if (Utils._.has(curParam, 'type')) {
if (Utils._.has(curParam, 'direction')) { paramDef.push(curParam['direction']); } if (Utils._.has(curParam, 'direction')) { paramDef.push(curParam.direction) }
if (Utils._.has(curParam, 'name')) { paramDef.push(curParam['name']); } if (Utils._.has(curParam, 'name')) { paramDef.push(curParam.name) }
paramDef.push(curParam['type']); paramDef.push(curParam.type)
} else { } else {
throw new Error('createFunction called with a parameter with no type'); throw new Error('createFunction called with a parameter with no type')
} }
return paramDef.join(' '); return paramDef.join(' ')
}); })
return paramList.join(', '); return paramList.join(', ')
}, },
expandOptions: function expandOptions(options) { expandOptions: function expandOptions(options) {
return Utils._.isUndefined(options) || Utils._.isEmpty(options) ? return Utils._.isUndefined(options) || Utils._.isEmpty(options) ?
'' : '' : '\n\t' + options.join('\n\t')
'\n\t' + options.join('\n\t');
}, },
decodeTriggerEventType: function decodeTriggerEventType(eventSpecifier) { decodeTriggerEventType: function decodeTriggerEventType(eventSpecifier) {
var EVENT_DECODER = { var EVENT_DECODER = {
'after': 'AFTER' 'after': 'AFTER',
, 'before': 'BEFORE' 'before': 'BEFORE',
, 'instead_of': 'INSTEAD OF' 'instead_of': 'INSTEAD OF',
, 'after_constraint': 'AFTER' 'after_constraint': 'AFTER'
} }
if (!Utils._.has(EVENT_DECODER, eventSpecifier)) { if (!Utils._.has(EVENT_DECODER, eventSpecifier)) {
throw new Error('Invalid trigger event specified: ' + eventSpecifier); throw new Error('Invalid trigger event specified: ' + eventSpecifier)
} }
return EVENT_DECODER[eventSpecifier]; return EVENT_DECODER[eventSpecifier]
}, },
triggerEventTypeIsConstraint: function triggerEventTypeIsConstraint(eventSpecifier) { triggerEventTypeIsConstraint: function triggerEventTypeIsConstraint(eventSpecifier) {
return eventSpecifier === 'after_constrain' ? 'CONSTRAINT ' : ''; return eventSpecifier === 'after_constrain' ? 'CONSTRAINT ' : ''
}, },
expandTriggerEventSpec: function expandTriggerEventSpec(fireOnSpec) { expandTriggerEventSpec: function expandTriggerEventSpec(fireOnSpec) {
if (Utils._.isEmpty(fireOnSpec)) { if (Utils._.isEmpty(fireOnSpec)) {
throw new Error('no table change events specified to trigger on'); throw new Error('no table change events specified to trigger on')
} }
return Utils._.map(fireOnSpec, function parseTriggerEventSpec(fireValue, fireKey){ return Utils._.map(fireOnSpec, function parseTriggerEventSpec(fireValue, fireKey){
var EVENT_MAP = { var EVENT_MAP = {
'insert': 'INSERT' 'insert': 'INSERT',
, 'update': 'UPDATE' 'update': 'UPDATE',
, 'delete': 'DELETE' 'delete': 'DELETE',
, 'truncate': 'TRUNCATE' 'truncate': 'TRUNCATE'
}; }
if (!Utils._.has(EVENT_MAP, fireKey)) { if (!Utils._.has(EVENT_MAP, fireKey)) {
throw new Error('parseTriggerEventSpec: undefined trigger event ' + fireKey); throw new Error('parseTriggerEventSpec: undefined trigger event ' + fireKey)
} }
var eventSpec = EVENT_MAP[fireKey]; var eventSpec = EVENT_MAP[fireKey]
if (eventSpec === 'UPDATE') { if (eventSpec === 'UPDATE') {
if (Utils._.isArray(fireValue) && fireValue.length > 0) { if (Utils._.isArray(fireValue) && fireValue.length > 0) {
eventSpec += ' OF ' + fireValue.join(', '); eventSpec += ' OF ' + fireValue.join(', ')
} }
} }
return eventSpec; return eventSpec
}).join(' OR '); }).join(' OR ')
}, },
pgListEnums: function(tableName, attrName, options) { pgListEnums: function(tableName, attrName, options) {
......
...@@ -631,7 +631,7 @@ module.exports = (function() { ...@@ -631,7 +631,7 @@ module.exports = (function() {
QueryInterface.prototype.createTrigger = function(tableName, triggerName, timingType, fireOnArray, QueryInterface.prototype.createTrigger = function(tableName, triggerName, timingType, fireOnArray,
functionName, functionParams, optionsArray) { functionName, functionParams, optionsArray) {
var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName
, functionParams, optionsArray); , functionParams, optionsArray)
if (sql){ if (sql){
return queryAndEmit.call(this, sql, 'createTrigger') return queryAndEmit.call(this, sql, 'createTrigger')
} else { } else {
...@@ -667,7 +667,7 @@ module.exports = (function() { ...@@ -667,7 +667,7 @@ module.exports = (function() {
} }
QueryInterface.prototype.createFunction = function(functionName, params, returnType, language, body, options) { QueryInterface.prototype.createFunction = function(functionName, params, returnType, language, body, options) {
var sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, options); var sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, options)
if (sql){ if (sql){
return queryAndEmit.call(this, sql, 'createFunction') return queryAndEmit.call(this, sql, 'createFunction')
} else { } else {
...@@ -679,7 +679,7 @@ module.exports = (function() { ...@@ -679,7 +679,7 @@ module.exports = (function() {
} }
QueryInterface.prototype.dropFunction = function(functionName, params) { QueryInterface.prototype.dropFunction = function(functionName, params) {
var sql = this.QueryGenerator.dropFunction(functionName, params); var sql = this.QueryGenerator.dropFunction(functionName, params)
if (sql){ if (sql){
return queryAndEmit.call(this, sql, 'dropFunction') return queryAndEmit.call(this, sql, 'dropFunction')
} else { } else {
...@@ -691,7 +691,7 @@ module.exports = (function() { ...@@ -691,7 +691,7 @@ module.exports = (function() {
} }
QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newFunctionName) { QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newFunctionName) {
var sql = this.QueryGenerator.renameFunction(oldFunctionName, params, newFunctionName); var sql = this.QueryGenerator.renameFunction(oldFunctionName, params, newFunctionName)
if (sql){ if (sql){
return queryAndEmit.call(this, sql, 'renameFunction') return queryAndEmit.call(this, sql, 'renameFunction')
} else { } else {
......
...@@ -311,41 +311,41 @@ describe(Support.getTestDialectTeaser("Migrator"), function() { ...@@ -311,41 +311,41 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
if (dialect.match(/^postgres/)) { if (dialect.match(/^postgres/)) {
describe('function migrations', function(done) { describe('function migrations', function() {
var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, languageOid) { var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, langName) {
return 'SELECT * FROM pg_proc where prolang = ' + languageOid + ' AND proname = \'' + functionName + '\''; return [
}; 'SELECT * FROM pg_proc p LEFT OUTER JOIN pg_language l ON (l.oid = p.prolang)',
var FUNC_NAME = 'get_an_answer'; 'WHERE p.proname = \'' + functionName + '\' AND l.lanname = \'' + langName + '\';'
var RENAME_FUNC_NAME = 'get_the_answer'; ].join('\n')
}
var FUNC_NAME = 'get_an_answer'
var RENAME_FUNC_NAME = 'get_the_answer'
// Set up the table and trigger // Set up the table and trigger
before(function(done){ before(function(done){
var self = this
this.init({ from: 20130909174103, to: 20130909174103}, function(migrator) { this.init({ from: 20130909174103, to: 20130909174103}, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
done(); done()
}) })
}) })
}) })
it("creates a function " + FUNC_NAME + "()", function(done) { it("creates a function " + FUNC_NAME + "()", function(done) {
var self = this; this.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 'plpgsql')).success(function(rows){
this.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 11771)).success(function(rows){
expect(rows.length).to.equal(1) expect(rows.length).to.equal(1)
done() done()
}) })
}) })
it("renames a function " + FUNC_NAME + "() to " + RENAME_FUNC_NAME + "()", function(done) { it("renames a function " + FUNC_NAME + "() to " + RENAME_FUNC_NAME + "()", function(done) {
var self = this; var self = this
this.init({ from: 20130909174253, to: 20130909174253 }, function(migrator) { this.init({ from: 20130909174253, to: 20130909174253 }, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 11771)).success(function(rows){ self.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 'plpgsql')).success(function(rows){
expect(rows.length).to.equal(0); expect(rows.length).to.equal(0)
self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 11771)).success(function(rows){ self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 'plpgsql')).success(function(rows){
expect(rows.length).to.equal(1); expect(rows.length).to.equal(1)
done() done()
}) })
}) })
...@@ -354,27 +354,27 @@ describe(Support.getTestDialectTeaser("Migrator"), function() { ...@@ -354,27 +354,27 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
}) })
it("deletes a function " + RENAME_FUNC_NAME + "()", function(done) { it("deletes a function " + RENAME_FUNC_NAME + "()", function(done) {
var self = this; var self = this
this.init({ from: 20130909175000, to: 20130909175000 }, function(migrator) { this.init({ from: 20130909175000, to: 20130909175000 }, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 11771)).success(function(rows){ self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 'plpgsql')).success(function(rows){
expect(rows.length).to.equal(0); expect(rows.length).to.equal(0)
done(); done()
}) })
}) })
}) })
}) })
}) })
describe('test trigger migrations', function(done) { describe('test trigger migrations', function() {
var generateTriggerCountQuery = function generateTriggerCountQuery(triggerName) { var generateTriggerCountQuery = function generateTriggerCountQuery(triggerName) {
return 'SELECT * FROM pg_trigger where tgname = \'' + triggerName + '\''; return 'SELECT * FROM pg_trigger where tgname = \'' + triggerName + '\''
}; }
var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, schemaName) { var generateTableCountQuery = function generateTableCountQuery(functionName, schemaName) {
return 'SELECT * FROM pg_tables where tablename = \'' + functionName + '\' and schemaname = \'' + schemaName + '\''; return 'SELECT * FROM pg_tables where tablename = \'' + functionName + '\' and schemaname = \'' + schemaName + '\''
}; }
var TRIGGER_NAME = 'updated_at'; var TRIGGER_NAME = 'updated_at'
var RENAME_TRIGGER_NAME = 'update_updated_at'; var RENAME_TRIGGER_NAME = 'update_updated_at'
var TABLE_NAME = 'trigger_test' var TABLE_NAME = 'trigger_test'
var CATALOG_NAME = 'public' var CATALOG_NAME = 'public'
...@@ -387,24 +387,24 @@ describe(Support.getTestDialectTeaser("Migrator"), function() { ...@@ -387,24 +387,24 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
"RETURN NEW;\n" + "RETURN NEW;\n" +
"END;\n" + "END;\n" +
"$$ language 'plpgsql';" "$$ language 'plpgsql';"
).success(function() {done()}); ).success(function() {done()})
}) })
// Clean up the function // Clean up the function
after(function(done){ after(function(done){
this.sequelize.query("DROP FUNCTION IF EXISTS bump_updated_at()").success(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) { it("creates a trigger updated_at on trigger_test", function(done) {
var self = this; var self = this
this.init({ from: 20130909175939, to: 20130909180846}, function(migrator) { this.init({ from: 20130909175939, to: 20130909180846}, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(1); expect(rows.length).to.equal(1)
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(1); expect(rows.length).to.equal(1)
done(); done()
}) })
}) })
}) })
...@@ -412,33 +412,33 @@ describe(Support.getTestDialectTeaser("Migrator"), function() { ...@@ -412,33 +412,33 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
}) })
it("renames a trigger on " + TABLE_NAME + " from " + TRIGGER_NAME + " to " + RENAME_TRIGGER_NAME, function(done){ it("renames a trigger on " + TABLE_NAME + " from " + TRIGGER_NAME + " to " + RENAME_TRIGGER_NAME, function(done){
var self = this; var self = this
this.init({ from: 20130909175939, to: 20130909181148}, function(migrator) { this.init({ from: 20130909175939, to: 20130909181148}, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(1); expect(rows.length).to.equal(1)
self.sequelize.query(generateTriggerCountQuery(RENAME_TRIGGER_NAME)).success(function(rows){ self.sequelize.query(generateTriggerCountQuery(RENAME_TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(1); expect(rows.length).to.equal(1)
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(0); expect(rows.length).to.equal(0)
done(); done()
})
}) })
}) })
}) })
}) })
}) })
})
it("deletes a trigger " + TRIGGER_NAME + " on trigger_test", function(done) { it("deletes a trigger " + TRIGGER_NAME + " on trigger_test", function(done) {
var self = this; var self = this
this.init({ from: 20130909175939, to: 20130909185621}, function(migrator) { this.init({ from: 20130909175939, to: 20130909185621}, function(migrator) {
migrator.migrate().success(function(){ migrator.migrate().success(function(){
self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){
expect(rows.length).to.equal(0); expect(rows.length).to.equal(0)
migrator.migrate({method: 'down'}).success(function(){ migrator.migrate({method: 'down'}).success(function(){
self.sequelize.query(generateFunctionCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){
expect(rows.length).to.equal(0); expect(rows.length).to.equal(0)
done(); done()
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!