class RemoveOldOrganizationSettings < PandaPal::MiscHelper::MigrationClass def current_tenant @current_tenant ||= PandaPal::Organization.find_by_name(Apartment::Tenant.current) end def up # don't rerun this if it was already run before we renamed the migration. existing_versions = execute ("SELECT * from schema_migrations where version = '30171205194657'") if (existing_versions.count > 0) execute "DELETE from schema_migrations where version = '30171205194657'" return end # 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 Apartment::Tenant.current == '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: # PandaPal::Organization.reset_column_information # 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) o = PandaPal::Organization.find_by!(name: o.name) raise "Failed to migrate PandaPal Settings" if o.settings != YAML.load(o.old_settings) end end remove_column :panda_pal_organizations, :old_settings end def down add_column :panda_pal_organizations, :old_settings, :text if Apartment::Tenant.current == 'public' PandaPal::Organization.find_each do |o| o.old_settings = o.settings.to_yaml o.save end end end end