Skip to content
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

Followed advice; rollback still won't run #284

Open
gisborne opened this issue Oct 30, 2024 · 1 comment
Open

Followed advice; rollback still won't run #284

gisborne opened this issue Oct 30, 2024 · 1 comment

Comments

@gisborne
Copy link

Trying to rollback this:

  def change
    safety_assured do
      add_column :tournaments, :livestream_required, :boolean, default: false, null: false
      add_column :tournament_templates, :livestream_required, :boolean, default: false, null: false
    end
  end

I am advised:

Active Record caches attributes, which causes problems
when removing columns. Be sure to ignore the column:

class TournamentTemplate < ApplicationRecord
  self.ignored_columns = ["livestream_required"]
end

Deploy the code, then wrap this step in a safety_assured { ... } block.

class AddLivestreamRequired < ActiveRecord::Migration[7.0]
  def change
    safety_assured { remove_column :tournament_templates, :livestream_required, :boolean, default: false, null: false }
  end
end
/Users/e86037/.asdf/installs/ruby/3.2.2/bin/bundle:25:in `load'
/Users/e86037/.asdf/installs/ruby/3.2.2/bin/bundle:25:in `<main>'

Caused by:
StrongMigrations::UnsafeMigration: 
=== Dangerous operation detected #strong_migrations ===

Active Record caches attributes, which causes problems
when removing columns. Be sure to ignore the column:

class TournamentTemplate < ApplicationRecord
  self.ignored_columns = ["livestream_required"]
end

Deploy the code, then wrap this step in a safety_assured { ... } block.

class AddLivestreamRequired < ActiveRecord::Migration[7.0]
  def change
    safety_assured { remove_column :tournament_templates, :livestream_required, :boolean, default: false, null: false }
  end
end

Note that I got the advice twice for some reason. Perhaps because there are two tables involved, but both advice only mentions one of them.

I have these lines in my models:

class TournamentTemplate < ApplicationRecord
  self.ignored_columns = ["livestream_required"]
…
class Tournament < ApplicationRecord
  self.ignored_columns = ["livestream_required"]

yet the migration still refuses to run.

@ankane
Copy link
Owner

ankane commented Nov 1, 2024

Hi @gisborne, you can get it to run by breaking it into up and down methods.

class AddLivestreamRequired < ActiveRecord::Migration[7.0]
  def up
    add_column :tournaments, :livestream_required, :boolean, default: false, null: false
    add_column :tournament_templates, :livestream_required, :boolean, default: false, null: false
  end

  def down
    safety_assured do
      remove_column :tournaments, :livestream_required
      remove_column :tournament_templates, :livestream_required
    end
  end
end

Will try to improve the error message for rollbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants