Drop and add columns
This commit is contained in:
parent
155844eae6
commit
877e30947f
|
@ -242,9 +242,29 @@ MySQL.prototype.alterTable = function (model, actualFields, done) {
|
|||
var m = this._models[model];
|
||||
var propNames = Object.keys(m.properties);
|
||||
var sql = [];
|
||||
|
||||
// change/add new fields
|
||||
propNames.forEach(function (propName) {
|
||||
var found;
|
||||
actualFields.forEach(function (f) {
|
||||
if (f.Field === propName) {
|
||||
found = f;
|
||||
}
|
||||
});
|
||||
|
||||
if (found) {
|
||||
actualize(propName, found);
|
||||
} else {
|
||||
sql.push('ADD COLUMN `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
||||
}
|
||||
});
|
||||
|
||||
// drop columns
|
||||
actualFields.forEach(function (f) {
|
||||
if (f.Field !== 'id') {
|
||||
actualize(f.Field, f);
|
||||
var notFound = !~propNames.indexOf(f.Field);
|
||||
if (f.Field === 'id') return;
|
||||
if (notFound || !m.properties[f.Field]) {
|
||||
sql.push('DROP COLUMN `' + f.Field + '`');
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -256,9 +276,7 @@ MySQL.prototype.alterTable = function (model, actualFields, done) {
|
|||
|
||||
function actualize(propName, oldSettings) {
|
||||
var newSettings = m.properties[propName];
|
||||
if (!newSettings) {
|
||||
sql.push('ADD COLUMN `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
||||
} else if (changed(newSettings, oldSettings)) {
|
||||
if (newSettings && changed(newSettings, oldSettings)) {
|
||||
sql.push('CHANGE COLUMN `' + propName + '` `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
||||
}
|
||||
}
|
||||
|
@ -293,8 +311,7 @@ MySQL.prototype.propertiesSQL = function (model) {
|
|||
MySQL.prototype.propertySettingsSQL = function (model, prop) {
|
||||
var p = this._models[model].properties[prop];
|
||||
return datatype(p) + ' ' +
|
||||
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL') +
|
||||
''; // (p.index ? ' KEY ix' + model + '_' + prop : '');
|
||||
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
|
||||
};
|
||||
|
||||
function datatype(p) {
|
||||
|
|
|
@ -114,13 +114,21 @@ it 'should autoupgrade', (test) ->
|
|||
User.defineProperty 'email', type: String
|
||||
User.defineProperty 'name', type: String, limit: 50
|
||||
User.defineProperty 'newProperty', type: Number
|
||||
User.defineProperty 'pendingPeriod', false
|
||||
schema.autoupdate (err) ->
|
||||
getFields 'User', (err, fields) ->
|
||||
# change nullable for email
|
||||
test.equal fields.email.Null, 'YES'
|
||||
# change type of name
|
||||
test.equal fields.name.Type, 'varchar(50)'
|
||||
# add new column
|
||||
test.ok fields.newProperty
|
||||
if fields.newProperty
|
||||
test.equal fields.newProperty.Type, 'int(11)'
|
||||
# drop column
|
||||
test.ok not fields.pendingPeriod
|
||||
|
||||
# user still exists
|
||||
userExists (yep) ->
|
||||
test.ok yep
|
||||
test.done()
|
||||
|
@ -128,3 +136,4 @@ it 'should autoupgrade', (test) ->
|
|||
it 'should disconnect when done', (test) ->
|
||||
schema.disconnect()
|
||||
test.done()
|
||||
|
||||
|
|
Loading…
Reference in New Issue