Fixed the different column name in datasource (#255)

* Fixed the different column name in datasource

Issue #250

* Added test to change the nullable property

* moved the code from the function into test block
This commit is contained in:
Raul Stelescu 2017-04-24 20:47:28 +02:00 committed by Sakib Hasan
parent b5ab229cb4
commit de7803f841
2 changed files with 131 additions and 89 deletions

View File

@ -307,7 +307,8 @@ function mixinMigration(MySQL, mysql) {
function actualize(propName, oldSettings) { function actualize(propName, oldSettings) {
var newSettings = m.properties[propName]; var newSettings = m.properties[propName];
if (newSettings && changed(newSettings, oldSettings)) { if (newSettings && changed(newSettings, oldSettings)) {
var pName = self.client.escapeId(propName); // Check if the property has a different column name in the database (other than property name)
var pName = (newSettings.mysql && newSettings.mysql.columnName) || self.client.escapeId(propName);
sql.push('CHANGE COLUMN ' + pName + ' ' + pName + ' ' + sql.push('CHANGE COLUMN ' + pName + ' ' + pName + ' ' +
self.buildColumnDefinition(model, propName)); self.buildColumnDefinition(model, propName));
} }

View File

@ -18,8 +18,7 @@ describe('MySQL connector', function() {
}); });
it('should auto migrate/update tables', function(done) { it('should auto migrate/update tables', function(done) {
var schema_v1 = var schema_v1 = {
{
'name': 'CustomerTest', 'name': 'CustomerTest',
'options': { 'options': {
'idInjection': false, 'idInjection': false,
@ -61,8 +60,7 @@ describe('MySQL connector', function() {
}, },
}; };
var schema_v2 = var schema_v2 = {
{
'name': 'CustomerTest', 'name': 'CustomerTest',
'options': { 'options': {
'idInjection': false, 'idInjection': false,
@ -222,6 +220,49 @@ describe('MySQL connector', function() {
verifyMysqlColumnNameAutoupdate(done); verifyMysqlColumnNameAutoupdate(done);
}); });
it('should update the nullable property of "first_name" to false', function(done) {
// update the model "required" property
var schema = {
name: 'ColRenameTest',
options: {
idInjection: false,
mysql: {
schema: 'myapp_test',
table: 'col_rename_test',
},
},
properties: {
firstName: {
type: 'String',
required: true,
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);
// nullable should be updated to false
ds.autoupdate('ColRenameTest', function(err) {
assert.ifError(err);
ds.discoverModelProperties('col_rename_test', function(err, props) {
assert.equal(props[0].nullable, 'N');
assert.equal(props[0].columnName, 'first_name');
done();
});
});
});
function verifyMysqlColumnNameAutoupdate(done) { function verifyMysqlColumnNameAutoupdate(done) {
ds.autoupdate('ColRenameTest', function(err) { ds.autoupdate('ColRenameTest', function(err) {
ds.discoverModelProperties('col_rename_test', function(err, props) { ds.discoverModelProperties('col_rename_test', function(err, props) {