Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow both up/down AND change in migrations #38376

Closed
axos88 opened this issue Feb 2, 2020 · 1 comment
Closed

Feature request: allow both up/down AND change in migrations #38376

axos88 opened this issue Feb 2, 2020 · 1 comment

Comments

@axos88
Copy link

@axos88 axos88 commented Feb 2, 2020

Allow both up/down and change in migrations.

I find myself doing this quite often:

class AddReservationAcknowledged < ActiveRecord::Migration[5.2]
  def ch
    add_column :reservations, :acknowledged, :boolean, null: false, default: false
  end

  def up
    ch
    execute "UPDATE reservations SET acknowledged = true"
  end

  def down
    revert { ch }
  end
end

For this pattern to be reusable I would expect up and down to yield when the change needs to be executed/reverted, like so:

class AddReservationAcknowledged < ActiveRecord::Migration[5.2]
  def change
    add_column :reservations, :acknowledged, :boolean, null: false, default: false
  end

  def up
    yield
    execute "UPDATE reservations SET acknowledged = true"
  end
end

If either up or down is not defined, only execute/revert the change.

@matthewd

This comment has been minimized.

Copy link
Member

@matthewd matthewd commented Feb 3, 2020

We added up_only to address this use case:

class AddReservationAcknowledged < ActiveRecord::Migration[5.2]
  def change
    add_column :reservations, :acknowledged, :boolean, null: false, default: false

    up_only do
      execute "UPDATE reservations SET acknowledged = true"
    end
  end
end

(if you need both up and down variants, look at reversible instead.)

@matthewd matthewd closed this Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.