Skip to content
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

Do not apply default values on data from database [3.x] #1705

Merged
merged 1 commit into from
Apr 9, 2019

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Apr 4, 2019

Before this change, when a property was configured with a default value at LoopBack side and the database was returned a record with a missing value for such property, then we would supply use the configured default.

This behavior is problematic for reasons explained in #1692.

In this patch, we are introducing a new model-level setting called applyDefaultsOnReads, which is enabled by default for backwards compatibility.

When this setting is set to false, operations like find and findOrCreate will NOT apply default property values on data returned by the database (connector).

Please note that most of the other CRUD methods did not apply default values on database data as long as the connector provided native implementation of the operation, that aspect is not changing.

Also note that default values are applied only on properties with undefined values. The value null does not trigger application of default values. This is important because SQL connectors return null for properties with no value set.

Related issues

This is a back-port of #1704. Compared to #1704 (master/4.x), we are preserving backwards compatibility by introducing a new feature flag.

See also #1692.

/cc @sharathmuthu6 @snarjuna

Checklist

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

@bajtos
Copy link
Member Author

bajtos commented Apr 4, 2019

[cis-jenkins] downstream: loopback-connector-mongodb@master — Failed! (be1c365)

The failure seems unrelated, see https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-mongodb%7Emaster/35/console

[cis-jenkins] downstream: loopback-connector-mysql@master — Failed! (be1c365)

This is a valid failure. Because the new tests don't run autoupdate/automigrate, the database schema is not matching model schema. I'll investigate.

@bajtos
Copy link
Member Author

bajtos commented Apr 5, 2019

In 6e7aee4, I added automigrate calls that fixed failing tests for SQL connectors.

However, there are still few failures that look relevant to me.

@jannyHou could you please help me to investigate the Cloudant failure?

Cloudant
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-cloudant%7Emaster/40/console

3) cloudant imported features basic-querying find applies default values by default:
07:50:37      TypeError: Cannot read property 'use' of undefined
07:50:37       at Cloudant.CouchDB.selectModel (node_modules/loopback-connector-couchdb2/lib/couchdb.js:750:29)
07:50:37       at Cloudant.destroyAll (node_modules/loopback-connector-couchdb2/lib/couchdb.js:423:17)

4) cloudant imported features basic-querying find preserves empty values from the database when "applyDefaultsOnReads" is false:
07:50:37      TypeError: Cannot read property 'use' of undefined
07:50:37       at Cloudant.CouchDB.selectModel (node_modules/loopback-connector-couchdb2/lib/couchdb.js:750:29)
07:50:37       at Cloudant.destroyAll (node_modules/loopback-connector-couchdb2/lib/couchdb.js:423:17)
07:50:37       at node_modules/loopback-connector-couchdb2/lib/migrate.js:42:14

The other two test failures reported by Cloudant seem unrelated, they are reported by CouchDB connector too.

@raymondfeng could you please help me with MSSQL? It looks like a bug in the autoupdate implementation. Other SQL connectors are passing these tests.

mssql
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-mssql%7Emaster/39/console

1) mssql imported features
07:58:29        basic-querying
07:58:29          find
07:58:29            applies default values by default:
07:58:29      RequestError: Could not drop object 'dbo.Product' because it is referenced by a FOREIGN KEY constraint

2) mssql imported features
07:58:29        basic-querying
07:58:29          find
07:58:29            preserves empty values from the database when "applyDefaultsOnReads" is false:
07:58:29      RequestError: Could not drop object 'dbo.Product' because it is referenced by a FOREIGN KEY constraint.

3) mssql imported features
07:58:29        manipulation
07:58:29          findOrCreate
07:58:29            applies default values on returned data:
07:58:29      RequestError: Could not drop object 'dbo.Product' because it is referenced by a FOREIGN KEY constraint.

4) mssql imported features
07:58:29        manipulation
07:58:29          findOrCreate
07:58:29            preserves empty values from the database when "applyDefaultsOnReads" is false:
07:58:29      RequestError: Could not drop object 'dbo.Product' because it is referenced by a FOREIGN KEY constraint.

Other test failures seem unrelated:

dashdb
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-dashdb%7Emaster/42/console

Seems unrelated:

07:48:54 > node pretest.js
07:48:54 
07:48:55 > Seeding the database ...
07:49:55 
07:49:55 /home/jenkins/workspace/ds/loopback-connector-dashdb~master/pretest.js:53
07:49:55   if (err) throw err;
07:49:55            ^
07:49:55 Error: [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "9.30.218.42".  Communication function detecting the error: "selectForConnectTimeout".  Protocol specific error code(s): "115", "*", "*".  SQLSTATE=08001
07:49:55 
07:49:55     at Error (native)

Same error for "db2" .

kv-extreme-scale
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-kv-extreme-scale%7Emaster/40/console

Cannot connect to the testing database :(

mongodb@3.x
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-mongodb%7E3.x/20/console

Fails in lazyConnect and ping test cases, seems unrelated to me.

Similarly for mongodb@master.

rest
https://cis-jenkins.swg-devops.com/job/ds/job/loopback-connector-rest%7Emaster/21/console

Uncaught AssertionError: You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account
07:57:27       + expected - actual
07:57:27 
07:57:27       -REQUEST_DENIED
07:57:27       +OK
07:57:27       

@jannyHou
Copy link
Contributor

jannyHou commented Apr 5, 2019

Sure I am looking a the Cloudant failure.


Update:

The first commit in loopbackio/loopback-connector-couchdb2#59 fixes the first test case.

Still investigating the second one.

@bajtos
Copy link
Member Author

bajtos commented Apr 8, 2019

[cis-jenkins] downstream: loopback-connector-mssql@master — Failed! (6e7aee4)
RequestError: Could not drop object 'dbo.Product' because it is referenced by a FOREIGN KEY constraint.

Apparently, there are other tests (or our setup script) using Product model and setting up a foreign key constraint(s) for it. I am not sure why our mssql connector is not dropping those constraints.

Anyhow, I managed to fix the problem by renaming my test model from Product to Player.

@bajtos
Copy link
Member Author

bajtos commented Apr 8, 2019

[cis-jenkins] downstream: loopback-connector-cloudant@master — Failed! (6e7aee4)
07:50:37 TypeError: Cannot read property 'use' of undefined

I managed to fix this problem with a small tweak in Cloudant's test suite, see loopbackio/loopback-connector-cloudant#202

@bajtos
Copy link
Member Author

bajtos commented Apr 8, 2019

I consider this pull request as done, I'll clean up the git history before landing.

@jannyHou @raymondfeng please review

Copy link
Contributor

@hacksparrow hacksparrow left a comment

Choose a reason for hiding this comment

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

Overall looks good, except for the choice of the data type to test with.

@bajtos
Copy link
Member Author

bajtos commented Apr 9, 2019

@slnode test please

Copy link
Contributor

@hacksparrow hacksparrow left a comment

Choose a reason for hiding this comment

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

All good now.

@bajtos bajtos force-pushed the fix/default-value-in-response-3x branch 2 times, most recently from f91aa80 to a39a2c7 Compare April 9, 2019 10:46
@bajtos
Copy link
Member Author

bajtos commented Apr 9, 2019

I had a chat with @hacksparrow and now I understand the problem - I was using a wrong assertion for the tests checking the legacy behavior where default values are applied. I fixed the problem in f498c26.

Actually, it turns out my original test assertions were correct. When the connector returns property value null, we don't consider it to be undefined and thus the default value is not applied. How confusing! I have reverted f498c26 and updated the comments in the tests to make this more clear.

Before this change, when a property was configured with a default value
at LoopBack side and the database was returned a record with a missing
value for such property, then we would supply use the configured
default.

This behavior is problematic for reasons explained in #1692.

In this commit, we are introducing a new model-level setting called
`applyDefaultsOnReads`, which is enabled by default for backwards
compatibility.

When this setting is set to `false`, operations like `find` and
`findOrCreate` will NOT apply default property values on data returned
by the database (connector).

Please note that most of the other CRUD methods did not apply default
values on database data as long as the connector provided native
implementation of the operation, that aspect is not changing.

Also note that default values are applied only on properties with
`undefined` values. The value `null` does not trigger application of
default values. This is important because SQL connectors return
`null` for properties with no value set.
@bajtos bajtos force-pushed the fix/default-value-in-response-3x branch from a39a2c7 to fd03302 Compare April 9, 2019 11:03
Copy link
Contributor

@hacksparrow hacksparrow left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

🚢 LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants