Skip to content

Commit

Permalink
Fix & Refactor BackendAPI scopes: orphans & not_used_by
Browse files Browse the repository at this point in the history
  - Use not_exists instead of not_in for the possible null values.
  - Do it through BabySqueel and unscoping the order in the subquery to make it work in
Oracle.
  - Test .orphans with disable_transactional_fixtures! to not have null
values like in the real execution.
  • Loading branch information
Martouta committed Dec 17, 2019
1 parent 5784bb7 commit a4e71ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
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))
end
}

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
}

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

0 comments on commit a4e71ca

Please sign in to comment.