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

[Bugfix][Oracle] path order in routing_policy working for null/empty values #1173

Merged
merged 1 commit into from
Sep 10, 2019

Conversation

Martouta
Copy link
Contributor

@Martouta Martouta commented Sep 10, 2019

Test it locally with oracle DB for the test: test/unit/proxy_test.rb:103
For Oracle the empty string is saved as NULL.

@Martouta Martouta self-assigned this Sep 10, 2019
@Martouta Martouta force-pushed the fix-oracle-path-nil-empty branch from 5d4957a to e5257d0 Compare September 10, 2019 09:32
@codecov
Copy link

codecov bot commented Sep 10, 2019

Codecov Report

Merging #1173 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1173      +/-   ##
==========================================
+ Coverage   92.99%   93.03%   +0.04%     
==========================================
  Files        2454     2455       +1     
  Lines       80951    80986      +35     
==========================================
+ Hits        75281    75348      +67     
+ Misses       5670     5638      -32
Impacted Files Coverage Δ
app/lib/backend_api_logic/routing_policy.rb 100% <100%> (ø) ⬆️
...pec/acceptance/api/line_item_variable_cost_spec.rb 100% <0%> (ø)
app/lib/three_scale/api/responder.rb 97.87% <0%> (+2.12%) ⬆️
lib/tasks/impersonation_admin_user.rake 100% <0%> (+10%) ⬆️
app/queries/new_accounts_query.rb 82.6% <0%> (+10.86%) ⬆️
lib/tasks/multitenant/tenants.rake 52.83% <0%> (+11.32%) ⬆️
app/models/application_record.rb 81.25% <0%> (+18.75%) ⬆️
config/initializers/access_token_authentication.rb 77.77% <0%> (+22.22%) ⬆️
.../initializers/backport_pg_10_support_to_rails_4.rb 80% <0%> (+50%) ⬆️
config/initializers/postgres.rb 100% <0%> (+50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76b5ed5...7c0ba70. Read the comment docs.

@Martouta Martouta force-pushed the fix-oracle-path-nil-empty branch from e5257d0 to 003009b Compare September 10, 2019 09:33
@Martouta Martouta changed the title [WIP][Bugfix][Oracle] path_to_regex NoMethodError: undefined method 'empty?' for nil:NilClass [WIP][Bugfix][Oracle] path routing_policy working for null values Sep 10, 2019
@Martouta Martouta force-pushed the fix-oracle-path-nil-empty branch from 003009b to 41e3e3f Compare September 10, 2019 09:35
@Martouta Martouta marked this pull request as ready for review September 10, 2019 09:35
@@ -17,7 +17,7 @@ def initialize(service)
end

def to_a
rules = backend_api_configs.reorder(path: :desc).each_with_object([]) do |config, rules|
rules = backend_api_configs.reorder('CASE WHEN path IS NULL THEN 1 ELSE 0 END, path DESC').each_with_object([]) do |config, rules|
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use COALESCE instead (with sift) and make it Oracle only just in case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Spoke with Gui, and we discovered that baby squeel does not offer reorder construct yet: rzane/baby_squeel#73

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Coalesce is not ordering properly for me:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

backend_api_configs.reorder('CASE WHEN path IS NULL THEN 1 ELSE 0 END, path DESC') works for the 3 DBs, however it might be a better idea for speed in the other DBs (not sure, so share your opinions) doing the specific query of Oracle only on Oracle so the others do not execute stuff they do not need, like this:

reorder = System::Database.oracle? ? 'CASE WHEN path IS NULL THEN 1 ELSE 0 END, path DESC' : {path: :desc}
backend_api_configs.reorder(reorder)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in fact then i could make it oracle specific like:

reorder = System::Database.oracle? ? 'path DESC NULLS LAST' : {path: :desc}
rules = backend_api_configs.reorder(reorder).each_with_object([]) do |config, rules|
# ...

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say let's make it specific for Oracle. Even though it works for all supported DBs to this day, a principle applies: the fix is meant for Oracle only.

One day we may have to support some weird DB to which this fix does NOT work and neither is necessary. Another thing that could happen is – who knows? – we drop support to Oracle one day. Then we'd end up with some ugly code for DBs that do need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So these are the 2 options, and both work in all DBs:

rules = backend_api_configs.reorder('CASE WHEN path IS NULL THEN 1 ELSE 0 END, path DESC').each_with_object([]) do |config, rules|
# ...
reorder = System::Database.oracle? ? 'path DESC NULLS LAST' : {path: :desc}
rules = backend_api_configs.reorder(reorder).each_with_object([]) do |config, rules|
# ...

Copy link
Contributor Author

@Martouta Martouta Sep 10, 2019

Choose a reason for hiding this comment

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

Right, good 👍 So I will push the last once 👍 And it is also definitely more understandable at first glance 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just discovered that reordering exists now in baby squeel 😄because i tried it locally after seeing https://www.bountysource.com/issues/53307207-reordering-without-explicit-direction-doesn-t-work-with-last-in-rails-4
So I will do the sifter 😄

@Martouta Martouta force-pushed the fix-oracle-path-nil-empty branch 2 times, most recently from 7c0ba70 to ed9fb32 Compare September 10, 2019 10:45
@Martouta Martouta changed the title [WIP][Bugfix][Oracle] path routing_policy working for null values [WIP][Bugfix][Oracle] path order in routing_policy working for null values Sep 10, 2019
@Martouta Martouta changed the title [WIP][Bugfix][Oracle] path order in routing_policy working for null values [WIP][Bugfix][Oracle] path order in routing_policy working for null/empty values Sep 10, 2019
@Martouta Martouta force-pushed the fix-oracle-path-nil-empty branch from ed9fb32 to 411b370 Compare September 10, 2019 10:46
Copy link
Contributor

@guicassolato guicassolato left a comment

Choose a reason for hiding this comment

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

Nice job!

As a future improvement (in case we ever need it), we could make sifter :path_desc more generic so it works with any column name and both ways :asc and :desc.

@Martouta Martouta changed the title [WIP][Bugfix][Oracle] path order in routing_policy working for null/empty values [Bugfix][Oracle] path order in routing_policy working for null/empty values Sep 10, 2019
@Martouta Martouta merged commit 9b608ea into master Sep 10, 2019
@Martouta Martouta deleted the fix-oracle-path-nil-empty branch September 10, 2019 11:35
@Martouta Martouta added APIAP bug Something isn't working Priority: Blocker This PR is a blocker. Everyone should review and help to close it ASAP. ruby Pull requests that update Ruby code labels Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
APIAP bug Something isn't working Priority: Blocker This PR is a blocker. Everyone should review and help to close it ASAP. ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants