houdini/lib/delete/delete_custom_field_joins.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

39 lines
No EOL
1.6 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
module DeleteCustomFieldJoins
@columns = ['id', 'custom_field_master_id', 'supporter_id', 'value', 'created_at', 'updated_at', 'metadata']
def self.find_multiple_custom_field_joins
bad_results = Qx.select("CONCAT(custom_field_joins.supporter_id, '_', custom_field_joins.custom_field_master_id) AS our_concat, COUNT(id) AS our_count").
from(:custom_field_joins).
group_by("our_concat").
having('COUNT(id) > 1').parse
custom_field_joins_from_qx = CustomFieldJoin.
where("CONCAT(custom_field_joins.supporter_id, '_', custom_field_joins.custom_field_master_id) IN (SELECT our_concat FROM (#{bad_results}) AS ignore)").
select('id, custom_field_master_id, supporter_id, created_at, updated_at')
grouped_custom_field_joins = custom_field_joins_from_qx.group_by{|tj| "#{tj.supporter_id}_#{tj.custom_field_master_id}"}
ids_to_delete = []
grouped_custom_field_joins.each { |_, v|
sorted = v.sort_by {|a| a.updated_at }.to_a
ids_to_delete += sorted.map{|i| i.id}[0, sorted.count - 1]
}
ids_to_delete
end
def self.copy_and_delete(ids_to_delete)
if ids_to_delete.any?
Qx.insert_into(:custom_field_joins_backup, @columns).select(@columns).from(:custom_field_joins).where('id IN ($ids)', ids:ids_to_delete).execute
CustomFieldJoin.where('id IN (?)', ids_to_delete).delete_all
end
end
def self.revert
Qx.insert_into(:custom_field_joins, @columns).select(@columns).from(:custom_field_joins_backup).execute
Qx.execute_raw("DELETE FROM custom_field_joins_backup")
end
end