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

You need to sign in or sign up before continuing.
Commit 058666ae by sdepold

renameColumn

1 parent c359834f
......@@ -74,6 +74,19 @@ var QueryGenerator = module.exports = {
return Utils._.template(query)({ tableName: tableName, attributes: attrString })
},
renameColumnQuery: function(tableName, attrBefore, attributes) {
var query = "ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var attrString = Utils._.map(attributes, function(definition, attributeName) {
return Utils._.template('`<%= before %>` `<%= after %>` <%= definition %>')({
before: attrBefore,
after: attributeName,
definition: definition
})
}).join(', ')
return Utils._.template(query)({ tableName: tableName, attributes: attrString })
},
selectQuery: function(tableName, options) {
options = options || {}
options.table = Utils.addTicks(tableName)
......
......@@ -52,6 +52,7 @@ module.exports = (function() {
/*
Returns a query, which modifies an existing attribute from a table.
Parameters:
- tableName: Name of the existing table.
- attributes: A hash with attribute-attributeOptions-pairs.
- key: attributeName
......@@ -61,7 +62,18 @@ module.exports = (function() {
- allowNull: Boolean
*/
changeColumnQuery: function(tableName, attribute) {
throw new Error('DEfine the method modifyColumnQuery!')
throw new Error('Define the method modifyColumnQuery!')
},
/*
Returns a query, which renames an existing attribute.
Parameters:
- tableName: Name of an existing table.
- attrNameBefore: The name of the attribute, which shall be renamed.
- attrNameAfter: The name of the attribute, after renaming.
*/
renameColumnQuery: function(tableName, attrNameBefore, attrNameAfter) {
throw new Error('Define the method renameColumnQuery!')
},
/*
......
......@@ -9,25 +9,17 @@ module.exports = (function() {
Utils.addEventEmitter(QueryInterface)
QueryInterface.prototype.createTable = function(tableName, attributes, options) {
var _query = query.call(this, this.QueryGenerator.createTableQuery(tableName, attributes, options))
var sql = this.QueryGenerator.createTableQuery(tableName, attributes, options)
, self = this
_query
.success(function(){ self.emit('createTable', null) })
.error(function(err){ self.emit('createTable', err)})
return _query
return queryAndEmit.call(this, sql, 'createTable')
}
QueryInterface.prototype.dropTable = function(tableName) {
var _query = query.call(this, this.QueryGenerator.dropTableQuery(tableName))
var sql = this.QueryGenerator.dropTableQuery(tableName)
, self = this
_query
.success(function(){ self.emit('dropTable', null) })
.error(function(err){ self.emit('dropTable', err)})
return _query
return queryAndEmit.call(this, sql, 'dropTable')
}
QueryInterface.prototype.dropAllTables = function() {
......@@ -58,15 +50,10 @@ module.exports = (function() {
}
QueryInterface.prototype.renameTable = function(before, after) {
var _query = query.call(this, this.QueryGenerator.renameTableQuery(before, after))
var sql = this.QueryGenerator.renameTableQuery(before, after)
, self = this
_query
.success(function(){ self.emit('renameTable', null) })
.error(function(err){ self.emit('renameTable', err)})
return _query
return queryAndEmit.call(this, sql, 'renameTable')
}
QueryInterface.prototype.showAllTables = function() {
......@@ -108,33 +95,17 @@ module.exports = (function() {
attributes[attributeName] = dataTypeOrOptions
var options = Utils.simplifyAttributes(attributes)
, query = this.QueryGenerator.addColumnQuery(tableName, options)
, sql = this.QueryGenerator.addColumnQuery(tableName, options)
, self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query(query).success(function() {
self.emit('addColumn', null)
emitter.emit('success', null)
}).error(function(err) {
self.emit('addColumn', err)
emitter.emit('failure', err)
})
}).run()
return queryAndEmit.call(this, sql, 'addColumn')
}
QueryInterface.prototype.removeColumn = function(tableName, attributeName) {
var self = this
, query = this.QueryGenerator.removeColumnQuery(tableName, attributeName)
var sql = this.QueryGenerator.removeColumnQuery(tableName, attributeName)
, self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query(query).success(function() {
self.emit('removeColumn', null)
emitter.emit('success', null)
}).error(function(err) {
self.emit('removeColumn', err)
emitter.emit('failure', err)
})
}).run()
return queryAndEmit.call(this, sql, 'removeColumn')
}
QueryInterface.prototype.changeColumn = function(tableName, attributeName, dataTypeOrOptions) {
......@@ -146,23 +117,44 @@ module.exports = (function() {
attributes[attributeName] = dataTypeOrOptions
var options = Utils.simplifyAttributes(attributes)
, query = this.QueryGenerator.changeColumnQuery(tableName, options)
, sql = this.QueryGenerator.changeColumnQuery(tableName, options)
, self = this
return queryAndEmit.call(this, sql, 'changeColumn')
}
QueryInterface.prototype.renameColumn = function(tableName, attrNameBefore, attrNameAfter) {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query(query).success(function() {
self.emit('changeColumn', null)
self.describeTable(tableName).success(function(data) {
data = data.filter(function(h) { return h.Field == attrNameBefore })[0]
var options = {}
options[attrNameAfter] = {
type: data.Type,
allowNull: data.Null == 'YES',
defaultValue: data.Default
}
var sql = self.QueryGenerator.renameColumnQuery(tableName,
attrNameBefore,
Utils.simplifyAttributes(options)
)
self.sequelize.query(sql).success(function() {
self.emit('renameColumn', null)
emitter.emit('success', null)
}).error(function(err) {
self.emit('changeColumn', err)
self.emit('renameColumn', err)
emitter.emit('failure', err)
})
}).error(function(err) {
self.emit('renameColumn', err)
emitter.emit('failure', err)
})
}).run()
}
QueryInterface.prototype.renameColumn = function() {
}
QueryInterface.prototype.addIndex = function() {
......@@ -194,31 +186,19 @@ module.exports = (function() {
QueryInterface.prototype.update = function(model, tableName, values, identifier) {
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier)
, _query = query.call(this, sql, model)
, self = this
_query
.success(function(){ self.emit('update', null) })
.error(function(err){ self.emit('update', err)})
return _query
return queryAndEmit.call(this, sql, 'update')
}
QueryInterface.prototype.delete = function(model, tableName, identifier) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier)
, _query = query.call(this, sql, model)
, self = this
_query
.success(function(){ self.emit('delete', null) })
.error(function(err){ self.emit('delete', err)})
return _query
return queryAndEmit.call(this, sql, 'delete')
}
QueryInterface.prototype.select = function(factory, tableName, options, queryOptions) {
queryOptions = queryOptions || {}
var sql = this.QueryGenerator.selectQuery(tableName, options)
, _query = query.call(this, sql, factory, queryOptions)
, self = this
......@@ -250,9 +230,22 @@ module.exports = (function() {
}).run()
}
// private
var queryAndEmit = function(sql, methodName) {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query(sql).success(function() {
self.emit(methodName, null)
emitter.emit('success', null)
}).error(function(err) {
self.emit(methodName, err)
emitter.emit('failure', err)
})
}).run()
}
var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
......
module.exports = {
up: function(migration, DataTypes) {
migration.renameColumn('User', 'signature', 'sig')
},
down: function(migration, DataTypes) {}
}
......@@ -92,12 +92,8 @@ describe('Migrator', function() {
Helpers.async(function(done) {
SequelizeMeta.create({ lastMigrationId: '20111117063700' }).success(function() {
migrator.getUndoneMigrations(function(err, migrations) {
migrations = migrations.sort(function(a,b){
return parseInt(a.filename.split('-')[0]) - parseInt(b.filename.split('-')[0])
})
expect(err).toBeFalsy()
expect(migrations.length).toEqual(6)
expect(migrations.length).toEqual(7)
expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
......@@ -254,7 +250,7 @@ describe('Migrator', function() {
})
describe('changeColumn', function() {
it('changes the signature column from user to default "" + notNull', function() {
it('changes the signature column from user to default "signature" + notNull', function() {
setup({from: 20111205064000, to: 20111206063000})
Helpers.async(function(done) {
......@@ -278,5 +274,29 @@ describe('Migrator', function() {
})
})
})
describe('renameColumn', function() {
it("renames the signature column from user to sig", function() {
setup({from: 20111117063700, to: 20111206163300})
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
sequelize.getQueryInterface().describeTable('User').success(function(data) {
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0]
, sig = data.filter(function(hash){ return hash.Field == 'sig' })[0]
expect(signature).toBeFalsy()
expect(sig).toBeTruthy()
done()
}).error(function(err) {
console.log(err)
})
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!