2021-10-21 11:12:54 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module LooseForeignKeys
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
class CleanerService
|
|
|
|
DELETE_LIMIT = 1000
|
|
|
|
UPDATE_LIMIT = 500
|
|
|
|
|
2021-11-24 07:10:21 -05:00
|
|
|
def initialize(loose_foreign_key_definition:, connection:, deleted_parent_records:, with_skip_locked: false)
|
|
|
|
@loose_foreign_key_definition = loose_foreign_key_definition
|
|
|
|
@connection = connection
|
2021-10-21 11:12:54 -04:00
|
|
|
@deleted_parent_records = deleted_parent_records
|
|
|
|
@with_skip_locked = with_skip_locked
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
result = connection.execute(build_query)
|
|
|
|
|
2021-12-01 10:13:55 -05:00
|
|
|
{ affected_rows: result.cmd_tuples, table: loose_foreign_key_definition.from_table }
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def async_delete?
|
2021-11-24 07:10:21 -05:00
|
|
|
loose_foreign_key_definition.on_delete == :async_delete
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def async_nullify?
|
2021-11-24 07:10:21 -05:00
|
|
|
loose_foreign_key_definition.on_delete == :async_nullify
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-24 07:10:21 -05:00
|
|
|
attr_reader :loose_foreign_key_definition, :connection, :deleted_parent_records, :with_skip_locked
|
2021-10-21 11:12:54 -04:00
|
|
|
|
|
|
|
def build_query
|
|
|
|
query = if async_delete?
|
|
|
|
delete_query
|
|
|
|
elsif async_nullify?
|
|
|
|
update_query
|
|
|
|
else
|
2021-11-24 07:10:21 -05:00
|
|
|
raise "Invalid on_delete argument: #{loose_foreign_key_definition.on_delete}"
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
2021-11-24 07:10:21 -05:00
|
|
|
unless query.include?(%{"#{loose_foreign_key_definition.column}" IN (})
|
2021-10-21 11:12:54 -04:00
|
|
|
raise("FATAL: foreign key condition is missing from the generated query: #{query}")
|
|
|
|
end
|
|
|
|
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
def arel_table
|
2021-12-01 10:13:55 -05:00
|
|
|
@arel_table ||= Arel::Table.new(loose_foreign_key_definition.from_table)
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def primary_keys
|
2021-12-01 10:13:55 -05:00
|
|
|
@primary_keys ||= connection.primary_keys(loose_foreign_key_definition.from_table).map { |key| arel_table[key] }
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_table_name
|
2021-12-01 10:13:55 -05:00
|
|
|
@quoted_table_name ||= Arel.sql(connection.quote_table_name(loose_foreign_key_definition.from_table))
|
2021-10-21 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_query
|
|
|
|
query = Arel::DeleteManager.new
|
|
|
|
query.from(quoted_table_name)
|
|
|
|
|
|
|
|
add_in_query_with_limit(query, DELETE_LIMIT)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_query
|
|
|
|
query = Arel::UpdateManager.new
|
|
|
|
query.table(quoted_table_name)
|
2021-11-24 07:10:21 -05:00
|
|
|
query.set([[arel_table[loose_foreign_key_definition.column], nil]])
|
2021-10-21 11:12:54 -04:00
|
|
|
|
|
|
|
add_in_query_with_limit(query, UPDATE_LIMIT)
|
|
|
|
end
|
|
|
|
|
|
|
|
# IN query with one or composite primary key
|
|
|
|
# WHERE (primary_key1, primary_key2) IN (subselect)
|
|
|
|
def add_in_query_with_limit(query, limit)
|
|
|
|
columns = Arel::Nodes::Grouping.new(primary_keys)
|
|
|
|
query.where(columns.in(in_query_with_limit(limit))).to_sql
|
|
|
|
end
|
|
|
|
|
|
|
|
# Builds the following sub-query
|
|
|
|
# SELECT primary_keys FROM table WHERE foreign_key IN (1, 2, 3) LIMIT N
|
|
|
|
def in_query_with_limit(limit)
|
|
|
|
in_query = Arel::SelectManager.new
|
|
|
|
in_query.from(quoted_table_name)
|
2021-11-24 07:10:21 -05:00
|
|
|
in_query.where(arel_table[loose_foreign_key_definition.column].in(deleted_parent_records.map(&:primary_key_value)))
|
2021-10-21 11:12:54 -04:00
|
|
|
in_query.projections = primary_keys
|
|
|
|
in_query.take(limit)
|
|
|
|
in_query.lock(Arel.sql('FOR UPDATE SKIP LOCKED')) if with_skip_locked
|
|
|
|
in_query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
end
|