class RemoveOldOrganizationSettings < ActiveRecord::Migration[5.1] def up # migrations run for public and local tenants. However, PandaPal::Organization # is going to always go to public tenant. So don't do this active record # stuff unless we are on the public tenant. if current_tenant == 'public' #PandaPal::Organization.connection.schema_cache.clear! #PandaPal::Organization.reset_column_information PandaPal::Organization.find_each do |o| # Would like to just be able to do this: # o.settings = YAML.load(o.old_settings) # o.save! # but for some reason that is always making the settings null. Instead we will encrypt the settings manually. iv = SecureRandom.random_bytes(12) key = o.encryption_key encrypted_settings = PandaPal::Organization.encrypt_settings(YAML.load(o.old_settings), iv: iv, key: key) o.update_columns(encrypted_settings_iv: [iv].pack("m"), encrypted_settings: encrypted_settings) end end remove_column :panda_pal_organizations, :old_settings end def down add_column :panda_pal_organizations, :old_settings, :text if current_tenant == 'public' PandaPal::Organization.find_each do |o| o.old_settings = o.settings.to_yaml o.save end end end end