From cf3dcfb4f6349b091c2ac3159cb20df0baafa897 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 19 Dec 2017 17:32:26 +0100 Subject: Add `down` to 20171218174509 Hstore to JSON migration Copy Cédric's JSON to Hstore migration for the `down` here to give us the ability to roll back. I'm guessing the solution probably comes from here: https://stackoverflow.com/questions/28315948/casting-json-to-hstore-in-postgres-9-3/28319295#28319295 Refs #5316 --- ...ontrol_attributes_format_from_hstore_to_json.rb | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb b/db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb index ec5a95ec9..0576dddf2 100644 --- a/db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb +++ b/db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb @@ -1,5 +1,5 @@ class ChangeComplianceControlComplianceCheckControlAttributesFormatFromHstoreToJson < ActiveRecord::Migration - def change + def up change_column :compliance_controls, :control_attributes, 'json USING hstore_to_json(control_attributes)' @@ -7,4 +7,27 @@ class ChangeComplianceControlComplianceCheckControlAttributesFormatFromHstoreToJ :control_attributes, 'json USING hstore_to_json(control_attributes)' end + + def down + execute <<-SQL + CREATE OR REPLACE FUNCTION my_json_to_hstore(json) + RETURNS hstore + IMMUTABLE + STRICT + LANGUAGE sql + AS $func$ + SELECT hstore(array_agg(key), array_agg(value)) + FROM json_each_text($1) + $func$; + SQL + + change_column :compliance_controls, + :control_attributes, + 'hstore USING my_json_to_hstore(control_attributes)' + change_column :compliance_checks, + :control_attributes, + 'hstore USING my_json_to_hstore(control_attributes)' + + execute "DROP FUNCTION my_json_to_hstore(json)" + end end -- cgit v1.2.3