Sha256: fdf41c1eab4357740e47ee6fe014a9a067b8f30bb7a0c7d27294da4d68365444

Contents?: true

Size: 1.11 KB

Versions: 34

Compression:

Stored size: 1.11 KB

Contents

class KitheModelType < ActiveRecord::Migration[5.2]
  def change
    reversible do |dir|
      dir.up do
        add_column :kithe_models, :kithe_model_type, :integer

        # Make sure all existing rows get value set. This is still kinda slow if you have
        # a big db, but well kithe is still in beta and nobody else is using it..
        say_with_time("setting values on :kithe_model_type") do
          Kithe::Model.in_batches do |rel|
            rel.pluck("id", "type").each do |id, type_name|
              type = type_name.constantize
              kithe_model_type = if type <= Kithe::Asset
                "asset"
              elsif type <= Kithe::Work
                "work"
              elsif type <= Kithe::Collection
                "collection"
              end
              Kithe::Model.where(id: id).update_all(kithe_model_type: kithe_model_type)
            end
          end
        end

        # Make it non-nullable
        change_column :kithe_models, :kithe_model_type, :integer, null: false
      end
      dir.down do
        remove_column :kithe_models, :kithe_model_type
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
kithe-2.16.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.15.1 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.15.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.14.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.13.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.12.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.11.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.10.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.9.1 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.9.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.8.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.7.1 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.7.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.6.1 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.6.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.5.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.4.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.3.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.2.0 db/migrate/20190404144551_kithe_model_type.rb
kithe-2.1.0 db/migrate/20190404144551_kithe_model_type.rb