aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-12-19 17:32:26 +0100
committerTeddy Wing2017-12-19 17:32:26 +0100
commit908058cca1cf7340e2b6b201dd03e8896a277e28 (patch)
tree4f5753490175c81bd89b2c95df69ed1ec9d46c95
parent98c669d8f69757751df9048f29ae02439724b7a3 (diff)
downloadchouette-core-908058cca1cf7340e2b6b201dd03e8896a277e28.tar.bz2
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
-rw-r--r--db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb25
1 files changed, 24 insertions, 1 deletions
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