We've moved discussions to Discord

#strong_migrations error - ValidatePlanAndAccountNotNull & CreatePayV3Models

Running initial migrations on a local server running Postgres 13.4, getting caught with these errors.

ValidatePlanAndAccountNotNull:
=== Dangerous operation detected #strong_migrations ===

Setting NOT NULL on an existing column blocks reads and writes while every row is checked.
Instead, add a check constraint and validate it in a separate migration.

class ValidatePlanAndAccountNotNull < ActiveRecord::Migration[6.1]
  def change
    add_check_constraint :plans, "name IS NOT NULL", name: "plans_name_null", validate: false
  end
end

class ValidateValidatePlanAndAccountNotNull < ActiveRecord::Migration[6.1]
  def change
    validate_check_constraint :plans, name: "plans_name_null"
  end
end

/jumpstart/db/migrate/20210115211740_validate_plan_and_account_not_null.rb:8:in `change'

(Skipping the above migration and re-running shows another strong migration error)

CreatePayV3Models:
=== Dangerous operation detected #strong_migrations ===

Adding a column with a non-null default blocks reads and writes while the entire table is rewritten.
Instead, add the column without a default value, then change the default.

class CreatePayV3Models < ActiveRecord::Migration[6.1]
  def up
    add_column :plans, :interval_count, :integer
    change_column_default :plans, :interval_count, 1
  end

  def down
    remove_column :plans, :interval_count
  end
end

Then backfill the existing rows in the Rails console or a separate migration with disable_ddl_transaction!.

class BackfillCreatePayV3Models < ActiveRecord::Migration[6.1]
  disable_ddl_transaction!

  def up
    Plan.unscoped.in_batches do |relation| 
      relation.update_all interval_count: 1
      sleep(0.01)
    end
  end
end

/jumpstart/db/migrate/20210804000959_create_pay_v3_models.rb:4:in `change'

Will edit migrations with suggested changes, let me know if there's a better way around this.
Chris Oliver
We've dropped strong migrations for the base template. It's not needed for new apps, mostly important once you're in production. If you update to the latest, this is all fixed. 
That's perfect, thanks Chris!
Notifications
You’re not receiving notifications from this thread.