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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix & Refactor BackendAPI scopes: orphans & not_used_by #1450

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion app/lib/fields/fields.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

#this is tested in unit/account_test
module Fields::Fields
extend ActiveSupport::Concern

Expand Down
27 changes: 11 additions & 16 deletions app/models/backend_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,20 @@ class BackendApi < ApplicationRecord

has_system_name(uniqueness_scope: [:account_id])

scope :orphans, -> { where.has { id.not_in(BackendApiConfig.selecting { :backend_api_id }) } }
scope :orphans, -> {
where.has do
not_exists(BackendApiConfig.except(:order).by_backend_api(BabySqueel[:backend_apis].id).select(:id))
Copy link
Contributor

Choose a reason for hiding this comment

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

This should improve efficiency a lot! Well done!

end
}
Comment on lines +40 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What generates for Oracle now for BackendApi.orphans.explain:

SELECT "BACKEND_APIS".*
FROM "BACKEND_APIS"
WHERE (
  NOT EXISTS(
    SELECT "BACKEND_API_CONFIGS"."ID"
    FROM "BACKEND_API_CONFIGS"
    WHERE "BACKEND_API_CONFIGS"."BACKEND_API_ID" = "BACKEND_APIS"."ID"
  )
)

image


scope :not_used_by, ->(service_id) {
# TODO: Baby Squeel
# It should be:
# where.has do
# not_exists BackendApiConfig.by_service(service_id).by_backend_api(BabySqueel[:backend_apis].id).select(:id)
# end
# And that works for MySQL and Postgres but not Oracle
sql_query = <<~SQL
(
NOT EXISTS (
SELECT id
FROM backend_api_configs
WHERE service_id = ? AND backend_api_configs.backend_api_id = backend_apis.id
)
where.has do
not_exists(
BackendApiConfig.except(:order).select(:id)
.by_service(service_id)
.by_backend_api(BabySqueel[:backend_apis].id)
)
SQL
where(sql_query, service_id)
end
}
Comment on lines 46 to 54
Copy link
Contributor Author

@Martouta Martouta Dec 18, 2019

Choose a reason for hiding this comment

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

What generates for Oracle now for BackendApi.not_used_by(service.id).explain :

SELECT "BACKEND_APIS".*
FROM "BACKEND_APIS"
WHERE (
  NOT EXISTS(
    SELECT "BACKEND_API_CONFIGS"."ID"
    FROM "BACKEND_API_CONFIGS"
    WHERE "BACKEND_API_CONFIGS"."SERVICE_ID" = theServiceID
      AND "BACKEND_API_CONFIGS"."BACKEND_API_ID" = "BACKEND_APIS"."ID"
  )
)

image


scope :oldest_first, -> { order(created_at: :asc) }
Expand Down
5 changes: 4 additions & 1 deletion lib/tasks/backend_api.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace :backend_api do
desc 'Destroy all orphan backend apis that does not belongs to any service when api_as_product is disabled'
task :destroy_orphans => :environment do
BackendApi.orphans.find_each { |backend_api| DeleteObjectHierarchyWorker.perform_later(backend_api) unless backend_api.account.provider_can_use?(:api_as_product) }
BackendApi.orphans.joins(:account).select(:id, :account_id).find_each do |backend_api|
next if backend_api.account.provider_can_use?(:api_as_product)
DeleteObjectHierarchyWorker.perform_later(backend_api)
end
end
end
30 changes: 16 additions & 14 deletions test/models/backend_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ def test_default_api_backend
assert @backend_api.valid?
end

test '.orphans should return backend apis that do not belongs to any service' do
account = FactoryBot.create(:account)
FactoryBot.create(:service, account: account)
service_to_delete = FactoryBot.create(:simple_service, :with_default_backend_api, system_name: 'orphan_system', account: account)
orphan_backend_api = service_to_delete.backend_api_configs.first.backend_api

assert_equal [], BackendApi.orphans

service_to_delete.destroy!

assert_equal [orphan_backend_api], BackendApi.orphans
end

test '.not_used_by returns the backend apis that are not related to that service' do
account = FactoryBot.create(:simple_provider)
backend_api_not_used_by_any_service = FactoryBot.create(:backend_api, account: account)
Expand Down Expand Up @@ -93,7 +80,7 @@ def test_default_api_backend
refute BackendApi.exists? backend_api.id
end

class ProxyConfigAffectingChangesTest < ActiveSupport::TestCase
class DisableTransactionalFixturesTest < ActiveSupport::TestCase
disable_transactional_fixtures!

test 'proxy config affecting changes on update' do
Expand All @@ -108,5 +95,20 @@ class ProxyConfigAffectingChangesTest < ActiveSupport::TestCase
backend_api.update_attributes(private_endpoint: 'http://new-endpoint')
backend_api.update_attributes(name: 'New Backend Name')
end

test '.orphans should return backend apis that do not belongs to any service' do
account = FactoryBot.create(:account)
FactoryBot.create(:service, account: account)
service_to_delete = FactoryBot.create(:simple_service, :with_default_backend_api, system_name: 'orphan_system', account: account)
orphan_backend_api = service_to_delete.backend_api_configs.first.backend_api

assert_equal [], BackendApi.orphans.pluck(:id)

service_to_delete.destroy!
assert_equal 0, orphan_backend_api.services.count
assert_equal 0, orphan_backend_api.backend_api_configs.count

assert_equal [orphan_backend_api.id], BackendApi.orphans.pluck(:id)
end
end
end