High Performance PostgreSQL for Rails: AddCheckConstraintTripsCompletedAt p. 75 (paper book P1.0)

Hello @andatki ,
I ran into a problem with the migration suggested p. 75:

class AddCheckConstraintTripsCompletedAt < ActiveRecord::Migration[7.0]
  def change
    add_check_constraint :trips,
      "completed_at > created_at", # Chronologic consistency
      name: "trips_completed_after_created"
  end
end

I got the following error:

StandardError: An error has occurred, this and all later migrations canceled: (StandardError)
=== Dangerous operation detected #strong_migrations ===
Adding a check constraint key blocks reads and writes while every row is checked.
Instead, add the check constraint without validating existing rows,
then validate them in a separate migration.
class AddCheckConstraintTripsCompletedAt < ActiveRecord::Migration[7.1]
def change
add_check_constraint :trips, “completed_at > created_at”, name: “trips_completed_after_created”, validate: false
end
end
class ValidateAddCheckConstraintTripsCompletedAt < ActiveRecord::Migration[7.1]
def change
validate_check_constraint :trips, name: “trips_completed_after_created”
end
end

Maybe change the migration or add a note about that in the book?

1 Like

Thanks for reporting this @cleaver-party. Indeed, I’d probably skip the safe migrations checks from Strong Migrations here, since the process for safely adding a check constraint is important, but beyond the scope of this section. We’ll get a fix in place for the next ebook version and I’ll leave this open until then, and follow up.

1 Like