Sha256: 6c5441b3a8aa768fb5ecfa0a21c5928d2ce508a2b98cfcdcb4d2830fa22fb629

Contents?: true

Size: 1.95 KB

Versions: 47

Compression:

Stored size: 1.95 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|
        next unless o.old_settings.present?

        # 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)
        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

47 entries across 47 versions & 1 rubygems

Version Path
panda_pal-5.12.7 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.6 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.5 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.4 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.3 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.2 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.1 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.12.0 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.11.0 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.10.1 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.10.0 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.8.beta1 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.7 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.6 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.5 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.4 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.3 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.1 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.9.0 db/migrate/20171205194657_remove_old_organization_settings.rb
panda_pal-5.8.5 db/migrate/20171205194657_remove_old_organization_settings.rb