Skip to content

Properties with mysql custom "columnName" don't get autoupdated #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function mixinMigration(MySQL, mysql) {
});
}
if (found) {
actualize(colName, found);
actualize(propName, found);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need change this?

Copy link
Contributor Author

@darknos darknos Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we have a way how to find column name by property name but not vise versa.

I explained it here #250 (comment)

} else {
sql.push('ADD COLUMN ' + self.client.escapeId(colName) + ' ' +
self.buildColumnDefinition(model, propName));
Expand All @@ -186,7 +186,7 @@ function mixinMigration(MySQL, mysql) {
function actualize(propName, oldSettings) {
var newSettings = m.properties[propName];
if (newSettings && changed(newSettings, oldSettings)) {
var pName = self.client.escapeId(propName);
var pName = self.columnEscaped(model, propName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to var pName = (newSettings.mysql && newSettings.mysql.columnName) || self.client.escapeId(propName);

Copy link
Contributor Author

@darknos darknos Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but in this case columnName will be not escaped and we have possible issue with columnName like from because SQL will be like CHANGE COLUMN from from ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to merge this PR asap, but why do you want to replace documented and widely used self.columnEscaped to that.

... ten lines above we use var colName = expectedColNameForModel(propName, m); for the same result.

please, confirm the change and, please, explain the reason

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. But what I am not understanding is, if we specify the columnName explicitly to be something different than the property name, how would this work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

var colName = newSettings.mysql && newSettings.mysql.columnName || self.client.escapeId(propName);
var pName = self.columnEscaped(model, colName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var pName = self.client.escapeId(newSettings.mysql && newSettings.mysql.columnName || propName);

do the same as

var pName = self.columnEscaped(propName);
/**
 * Get the escaped column name for a given model property
 * @param {String} model The model name
 * @param {String} property The property name
 * @returns {String} The escaped column name
 */
SQLConnector.prototype.columnEscaped = function(model, property) {
  return this.escapeName(this.column(model, property));
};
/**
 * Get the column name for the given model property. The column name can be
 * customized at the model property definition level as `column` or
 * `columnName`. For example,
 *
 * ```json
 * "name": {
 *   "type": "string",
 *   "mysql": {
 *     "column": "NAME"
 *   }
 * }
 * ```
 *
 * @param {String} model The model name
 * @param {String} property The property name
 * @returns {String} The column name
 */
SQLConnector.prototype.column = function(model, property) {
  var prop = this.getPropertyDefinition(model, property);
  var columnName;
  if (prop && prop[this.name]) {
    columnName = prop[this.name].column || prop[this.name].columnName;
    if (columnName) {
      // Explicit column name, return as-is
      return columnName;
    }
  }
  columnName = property;
  if (typeof this.dbName === 'function') {
    columnName = this.dbName(columnName);
  }
  return columnName;
};

sql.push('CHANGE COLUMN ' + pName + ' ' + pName + ' ' +
self.buildColumnDefinition(model, propName));
}
Expand Down
43 changes: 43 additions & 0 deletions test/mysql.autoupdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,49 @@ describe('MySQL connector', function() {
// second autoupdate call uses alter table
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) {
ds.autoupdate('ColRenameTest', function(err) {
Expand Down