Sha256: f4b7cf779731a322dca05f0d75187baa5dd0bc24f4a76133072589f0d883b0ed

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 KB

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
panda_pal-5.3.9 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.3.7 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.3.5 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.3.4 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.3.2 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.3.0 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.2.5 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.2.4 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.2.3 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.2.2 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.2.1 db/migrate/20171205194657_remove_old_organization_settings.rb