diff options
| author | Teddy Wing | 2017-12-19 17:32:26 +0100 |
|---|---|---|
| committer | Luc Donnet | 2017-12-21 15:32:42 +0100 |
| commit | cf3dcfb4f6349b091c2ac3159cb20df0baafa897 (patch) | |
| tree | d9dd34bb3a29848a3d4c121e737e18401d36408e /db/migrate | |
| parent | bb79de10b08161586dec9118354b1a153e2c67fd (diff) | |
| download | chouette-core-cf3dcfb4f6349b091c2ac3159cb20df0baafa897.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
Diffstat (limited to 'db/migrate')
| -rw-r--r-- | db/migrate/20171218174509_change_compliance_control_compliance_check_control_attributes_format_from_hstore_to_json.rb | 25 |
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 |
