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

Commit 552fc607 by sdepold

remove column

1 parent a83e88d2
......@@ -57,6 +57,11 @@ var QueryGenerator = module.exports = {
return Utils._.template(query)({ tableName: tableName, attributes: attrString })
},
removeColumnQuery: function(tableName, attributeName) {
var query = "ALTER TABLE `<%= tableName %>` DROP `<%= attributeName %>`;"
return Utils._.template(query)({ tableName: tableName, attributeName: attributeName })
},
/*
Returns a query for selecting elements in the database <tableName>.
Options:
......
......@@ -16,6 +16,40 @@ module.exports = (function() {
},
/*
Returns a rename table query.
Parameters:
- originalTableName: Name of the table before execution.
- futureTableName: Name of the table after execution.
*/
renameTableQuery: function(originalTableName, futureTableName) {
throw new Error('Define the method renameQuery!')
},
/*
Returns a query, which adds an attribute to an existing table.
Parameters:
- tableName: Name of the existing table.
- attributeName: Name of the new attribute.
- options: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
*/
addColumnQuery: function(tableName, attributeName, options) {
throw new Error('Define the method addColumnQuery!')
},
/*
Returns a query, which removes an attribute from an existing table.
Parameters:
- tableName: Name of the existing table
- attributeName: Name of the obsolete attribute.
*/
removeColumnQuery: function(tableName, attributeName) {
throw new Error('Define the method removeColumnQuery!')
},
/*
Returns a query for selecting elements in the table <tableName>.
Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
......@@ -69,30 +103,6 @@ module.exports = (function() {
},
/*
Returns a rename table query.
Parameters:
- originalTableName: Name of the table before execution.
- futureTableName: Name of the table after execution.
*/
renameTableQuery: function(originalTableName, futureTableName) {
throw new Error('Define the method renameQuery!')
},
/*
Returns a query, which adds an attribute to an existing table.
Parameters:
- tableName: Name of the existing table.
- attributeName: Name of the new attribute.
- options: A hash with attribute specific options:
- type: DataType
- defaultValue: A String with the default value
- allowNull: Boolean
*/
addColumnQuery: function(tableName, attributeName, options) {
throw new Error('Define the method addColumnQuery!')
},
/*
Takes something and transforms it into values of a where condition.
*/
getWhereConditions: function(smth) {
......
......@@ -122,8 +122,19 @@ module.exports = (function() {
}).run()
}
QueryInterface.prototype.removeColumn = function() {
QueryInterface.prototype.removeColumn = function(tableName, attributeName) {
var self = this
, query = this.QueryGenerator.removeColumnQuery(tableName, attributeName)
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()
}
QueryInterface.prototype.changeColumn = function() {
......
module.exports = {
up: function(migration, DataTypes) {
migration.removeColumn('User', 'shopId')
},
down: function(migration, DataTypes) {}
}
......@@ -97,7 +97,7 @@ describe('Migrator', function() {
})
expect(err).toBeFalsy()
expect(migrations.length).toEqual(4)
expect(migrations.length).toEqual(5)
expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
......@@ -220,6 +220,38 @@ describe('Migrator', function() {
})
})
})
describe('removeColumn', function() {
it('removes the shopId column from user', function() {
setup({from: 20111205064000, to: 20111206061400})
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]
, isAdmin = data.filter(function(hash){ return hash.Field == 'isAdmin' })[0]
, shopId = data.filter(function(hash){ return hash.Field == 'shopId' })[0]
expect(signature.Field).toEqual('signature')
expect(signature.Null).toEqual('NO')
expect(isAdmin.Field).toEqual('isAdmin')
expect(isAdmin.Null).toEqual('NO')
expect(isAdmin.Default).toEqual('0')
expect(shopId).toBeFalsy()
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!