Sha256: e7d6852e5583f3af1206aa48aa4282cafee365219106d0a38ebc8d48d505f804

Contents?: true

Size: 1.86 KB

Versions: 15

Compression:

Stored size: 1.86 KB

Contents

class MigrateMartyRolesToEnum < ActiveRecord::Migration[5.1]
  include Marty::Migrations

  def up
    new_enum(Marty::RoleType, 'keep_marty_prefix_here')

    add_column :marty_user_roles, :role, :marty_role_types
    add_column :marty_import_types, :role, :marty_role_types

    execute <<-SQL
      UPDATE marty_user_roles AS ur
      SET role = roles.name::marty_role_types
      FROM marty_roles AS roles
      WHERE ur.role_id = roles.id
    SQL

    execute <<-SQL
      UPDATE marty_import_types AS it
      SET role = roles.name::marty_role_types
      FROM marty_roles AS roles
      WHERE it.role_id = roles.id
    SQL

    remove_column :marty_user_roles, :role_id
    remove_column :marty_import_types, :role_id

    drop_table :marty_roles

    change_column_null :marty_user_roles, :role, false
    change_column_null :marty_import_types, :role, false
  end

  def down
    create_table :marty_roles do |t|
      t.string :name, null: false, limit: 255
    end

    add_column :marty_user_roles, :role_id, :integer
    add_column :marty_import_types, :role_id, :integer

    Marty::RoleType.values.each do |role|
      Marty::Role.create!(name: role)
    end

    execute <<-SQL
      UPDATE marty_user_roles AS ur
      SET role_id = roles.id
      FROM marty_roles AS roles
      WHERE ur.role::text = roles.name
    SQL

    execute <<-SQL
      UPDATE marty_import_types AS ur
      SET role_id = roles.id
      FROM marty_roles AS roles
      WHERE ur.role::text = roles.name
    SQL

    remove_column :marty_user_roles, :role
    remove_column :marty_import_types, :role

    execute <<-SQL
      DROP TYPE marty_role_types
    SQL

    change_column_null :marty_user_roles, :role_id, false
    change_column_null :marty_import_types, :role_id, false

    add_fk :marty_user_roles, :marty_roles, column: :role_id
    add_fk :marty_import_types, :marty_roles, column: :role_id
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
marty-14.3.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-14.0.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-13.0.2 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-11.0.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-10.0.3 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-10.0.2 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-10.0.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-9.5.1 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-9.5.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-9.3.3 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-9.3.2 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-9.3.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-8.5.0 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-8.4.1 db/migrate/507_migrate_marty_roles_to_enum.rb
marty-8.3.1 db/migrate/507_migrate_marty_roles_to_enum.rb