Merge pull request #190 from rlloyd2001/mysqlPropFix

Fixes issues where autoupdate ignores mysql.columnName and deletes columns
This commit is contained in:
Simon Ho 2016-09-14 13:01:01 -07:00 committed by GitHub
commit 955f5dc084
3 changed files with 79 additions and 8 deletions

View File

@ -142,9 +142,10 @@ function mixinMigration(MySQL, mysql) {
propNames.forEach(function(propName) {
if (m.properties[propName] && self.id(model, propName)) return;
var found;
var colName = expectedColName(propName);
if (actualFields) {
actualFields.forEach(function(f) {
if (f.Field === propName) {
if (f.Field === colName) {
found = f;
}
});
@ -161,9 +162,12 @@ function mixinMigration(MySQL, mysql) {
// drop columns
if (actualFields) {
actualFields.forEach(function(f) {
var notFound = !~propNames.indexOf(f.Field);
if (m.properties[f.Field] && self.id(model, f.Field)) return;
if (notFound || !m.properties[f.Field]) {
var colNames = propNames.map(expectedColName);
var index = colNames.indexOf(f.Field);
var propName = index >= 0 ? propNames[index] : f.Field;
var notFound = !~index;
if (m.properties[propName] && self.id(model, propName)) return;
if (notFound || !m.properties[propName]) {
sql.push('DROP COLUMN ' + self.client.escapeId(f.Field));
}
});
@ -329,6 +333,18 @@ function mixinMigration(MySQL, mysql) {
}
return false;
}
function expectedColName(propName) {
var mysql = m.properties[propName].mysql;
if (typeof mysql === 'undefined') {
return propName;
}
var colName = mysql.columnName;
if (typeof colName === 'undefined') {
return propName;
}
return colName;
}
};
MySQL.prototype.buildColumnDefinitions =

View File

@ -338,14 +338,13 @@ describe('migrations', function() {
});
});
it('should map zero dateTime into null', function (done) {
it('should map zero dateTime into null', function(done) {
query('INSERT INTO `DateData` ' +
'(`dateTime`, `timestamp`) ' +
'VALUES("0000-00-00 00:00:00", "0000-00-00 00:00:00") ',
function (err, ret) {
function(err, ret) {
should.not.exists(err);
DateData.findById(ret.insertId, function (err, dateData) {
DateData.findById(ret.insertId, function(err, dateData) {
should(dateData.dateTime)
.be.null();
should(dateData.timestamp)

View File

@ -13,6 +13,10 @@ before(function() {
});
describe('MySQL connector', function() {
before(function() {
setupAltColNameData();
});
it('should auto migrate/update tables', function(done) {
var schema_v1 =
{
@ -163,6 +167,37 @@ describe('MySQL connector', function() {
});
});
function setupAltColNameData() {
var schema = {
name: 'ColRenameTest',
options: {
idInjection: false,
mysql: {
schema: 'myapp_test',
table: 'col_rename_test',
},
},
properties: {
firstName: {
type: 'String',
required: false,
length: 40,
mysql: {
columnName: 'first_name',
dataType: 'varchar',
dataLength: 40,
},
},
lastName: {
type: 'String',
required: false,
length: 40,
},
},
};
ds.createModel(schema.name, schema.properties, schema.options);
}
it('should report errors for automigrate', function(done) {
ds.automigrate('XYZ', function(err) {
assert(err);
@ -176,4 +211,25 @@ describe('MySQL connector', function() {
done();
});
});
it('"mysql.columnName" is updated with correct name on create table', function(done) {
// first autoupdate call uses create table
verifyMysqlColumnNameAutoupdate(done);
});
it('"mysql.columnName" is updated without changing column name on alter table', function(done) {
// second autoupdate call uses alter table
verifyMysqlColumnNameAutoupdate(done);
});
function verifyMysqlColumnNameAutoupdate(done) {
ds.autoupdate('ColRenameTest', function(err) {
ds.discoverModelProperties('col_rename_test', function(err, props) {
assert.equal(props[0].columnName, 'first_name');
assert.equal(props[1].columnName, 'lastName');
assert.equal(props.length, 2);
done();
});
});
}
});