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

Commit c359834f by sdepold

changeColumn

1 parent 552fc607
...@@ -62,19 +62,18 @@ var QueryGenerator = module.exports = { ...@@ -62,19 +62,18 @@ var QueryGenerator = module.exports = {
return Utils._.template(query)({ tableName: tableName, attributeName: attributeName }) return Utils._.template(query)({ tableName: tableName, attributeName: attributeName })
}, },
/* changeColumnQuery: function(tableName, attributes) {
Returns a query for selecting elements in the database <tableName>. var query = "ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
Options: var attrString = Utils._.map(attributes, function(definition, attributeName) {
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: * return Utils._.template('`<%= attributeName %>` `<%= attributeName %>` <%= definition %>')({
- where -> A hash with conditions (e.g. {name: 'foo'}) attributeName: attributeName,
OR an ID as integer definition: definition
OR a string with conditions (e.g. 'name="foo"'). })
If you use a string, you have to escape it on your own. }).join(', ')
- order -> e.g. 'id DESC'
- group return Utils._.template(query)({ tableName: tableName, attributes: attrString })
- limit -> The maximum count you want to get. },
- offset -> An offset value to start from. Only useable with limit!
*/
selectQuery: function(tableName, options) { selectQuery: function(tableName, options) {
options = options || {} options = options || {}
options.table = Utils.addTicks(tableName) options.table = Utils.addTicks(tableName)
......
...@@ -29,13 +29,14 @@ module.exports = (function() { ...@@ -29,13 +29,14 @@ module.exports = (function() {
Returns a query, which adds an attribute to an existing table. Returns a query, which adds an attribute to an existing table.
Parameters: Parameters:
- tableName: Name of the existing table. - tableName: Name of the existing table.
- attributeName: Name of the new attribute. - attributes: A hash with attribute-attributeOptions-pairs.
- options: A hash with attribute specific options: - key: attributeName
- value: A hash with attribute specific options:
- type: DataType - type: DataType
- defaultValue: A String with the default value - defaultValue: A String with the default value
- allowNull: Boolean - allowNull: Boolean
*/ */
addColumnQuery: function(tableName, attributeName, options) { addColumnQuery: function(tableName, attributes) {
throw new Error('Define the method addColumnQuery!') throw new Error('Define the method addColumnQuery!')
}, },
...@@ -50,6 +51,20 @@ module.exports = (function() { ...@@ -50,6 +51,20 @@ module.exports = (function() {
}, },
/* /*
Returns a query, which modifies an existing attribute from a table.
- tableName: Name of the existing table.
- attributes: A hash with attribute-attributeOptions-pairs.
- key: attributeName
- value: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
*/
changeColumnQuery: function(tableName, attribute) {
throw new Error('DEfine the method modifyColumnQuery!')
},
/*
Returns a query for selecting elements in the table <tableName>. Returns a query for selecting elements in the table <tableName>.
Options: Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: * - attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
......
...@@ -137,7 +137,27 @@ module.exports = (function() { ...@@ -137,7 +137,27 @@ module.exports = (function() {
}).run() }).run()
} }
QueryInterface.prototype.changeColumn = function() { QueryInterface.prototype.changeColumn = function(tableName, attributeName, dataTypeOrOptions) {
var attributes = {}
if(Utils._.values(DataTypes).indexOf(dataTypeOrOptions) > -1)
attributes[attributeName] = { type: dataTypeOrOptions, allowNull: false }
else
attributes[attributeName] = dataTypeOrOptions
var options = Utils.simplifyAttributes(attributes)
, query = this.QueryGenerator.changeColumnQuery(tableName, options)
, self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query(query).success(function() {
self.emit('changeColumn', null)
emitter.emit('success', null)
}).error(function(err) {
self.emit('changeColumn', err)
emitter.emit('failure', err)
})
}).run()
} }
......
module.exports = {
up: function(migration, DataTypes) {
migration.changeColumn('User', 'signature', {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'Signature'
})
},
down: function(migration, DataTypes) {}
}
...@@ -97,7 +97,7 @@ describe('Migrator', function() { ...@@ -97,7 +97,7 @@ describe('Migrator', function() {
}) })
expect(err).toBeFalsy() expect(err).toBeFalsy()
expect(migrations.length).toEqual(5) expect(migrations.length).toEqual(6)
expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js') expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done() done()
}) })
...@@ -252,6 +252,31 @@ describe('Migrator', function() { ...@@ -252,6 +252,31 @@ describe('Migrator', function() {
}) })
}) })
describe('changeColumn', function() {
it('changes the signature column from user to default "" + notNull', function() {
setup({from: 20111205064000, to: 20111206063000})
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]
expect(signature.Field).toEqual('signature')
expect(signature.Type).toEqual('varchar(255)')
expect(signature.Null).toEqual('NO')
expect(signature.Default).toEqual('Signature')
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!