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

Conversation

darknos
Copy link
Contributor

@darknos darknos commented Apr 27, 2017

Description

#250 for 3.x

connect to #250

Checklist

  • [+] New tests added or existing tests modified to cover all changes
  • [+] Code conforms with the style
    guide

@slnode
Copy link

slnode commented Apr 27, 2017

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@slnode
Copy link

slnode commented Apr 27, 2017

Can one of the admins verify this patch?

2 similar comments
@slnode
Copy link

slnode commented Apr 27, 2017

Can one of the admins verify this patch?

@slnode
Copy link

slnode commented Apr 27, 2017

Can one of the admins verify this patch?

@@ -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;
};

@@ -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)

@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

@slnode ok to test

@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

@slnode test please

ssh24 referenced this pull request Apr 27, 2017
* 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
@ssh24 ssh24 self-assigned this Apr 27, 2017
@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

@darknos Thank you for taking the initiative and submitting this patch.

@ssh24 ssh24 added the apex label Apr 27, 2017
@ssh24 ssh24 added this to the Sprint 34 - Apex milestone Apr 27, 2017
@ssh24 ssh24 merged commit ea33f55 into loopbackio:master Apr 27, 2017
kjdelisle pushed a commit that referenced this pull request May 2, 2017
 * Tests for datetime types (Kevin Delisle)
 * Add new type DateString to fromColumnValue (Buck Bito)
 * Remove String manipulations of Date objects (Buck Bito)
 * Properties with mysql custom "columnName" don't get autoupdated (#273) (Sergey Nosenko)
 * Revert PR #257 (#266) (Sakib Hasan)
 * Fix async.each in migration (#262) (Benjamin Schuster-Boeckler)
 * refactor date, timestamp and datetime data types handling (#257) (Sergey Nosenko)
 * Fix too many connection error (#261) (Sakib Hasan)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants