Move all dedications into valid json
This commit is contained in:
parent
bb67341f37
commit
4550ab81cd
1 changed files with 51 additions and 0 deletions
51
db/migrate/20181002160627_correct_dedications.rb
Normal file
51
db/migrate/20181002160627_correct_dedications.rb
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
class CorrectDedications < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
execute <<-SQL
|
||||||
|
create or replace function is_valid_json(p_json text)
|
||||||
|
returns boolean
|
||||||
|
as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
return (p_json::json is not null);
|
||||||
|
exception
|
||||||
|
when others then
|
||||||
|
return false;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
|
language plpgsql
|
||||||
|
immutable;
|
||||||
|
SQL
|
||||||
|
|
||||||
|
result = Qx.select('id', 'dedication').from(:donations)
|
||||||
|
.where("dedication IS NOT NULL AND dedication != ''")
|
||||||
|
.and_where("NOT is_valid_json(dedication)").ex
|
||||||
|
result.map do |i|
|
||||||
|
output = {id: i['id']}
|
||||||
|
if i['dedication'] =~ /(((in (loving )?)?memory of|in memorium)\:? )(.+)/i
|
||||||
|
output[:dedication] = JSON.generate({type: 'memory', note: $+ })
|
||||||
|
elsif i['dedication'] =~ /((in honor of|honor of)\:? )(.+)/i
|
||||||
|
output[:dedication] = JSON.generate({type: 'honor', note: $+ })
|
||||||
|
else
|
||||||
|
output[:dedication] = JSON.generate({type: 'honor', note: i['dedication'] })
|
||||||
|
end
|
||||||
|
output
|
||||||
|
end.each do |i|
|
||||||
|
Qx.update(:donations).where('id = $id', {id: i[:id]}).set({dedication: i[:dedication]}).ex
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# result = Qx.select('id', 'dedication').from(:donations)
|
||||||
|
# .where("id IN ($ids)", ids: result.map{|i| i['id']}).ex
|
||||||
|
#
|
||||||
|
# puts result
|
||||||
|
|
||||||
|
|
||||||
|
execute <<-SQL
|
||||||
|
drop function is_valid_json(text);
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue