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