diff options
| -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 | 
