README.md in strong_migrations-0.2.2 vs README.md in strong_migrations-0.2.3

- old
+ new

@@ -14,11 +14,11 @@ gem 'strong_migrations' ``` ## How It Works -Strong Migrations detects potentially dangerous operations in migrations, prevents them from running, and gives instructions on safer ways to do what you want. +Strong Migrations detects potentially dangerous operations in migrations, prevents them from running by default, and provides instructions on safer ways to do what you want. ``` __ __ _____ _______ _ \ \ / /\ |_ _|__ __| | \ \ /\ / / \ | | | | | | @@ -43,10 +43,11 @@ The following operations can cause downtime or errors: - adding a column with a non-null default value to an existing table - removing a column - changing the type of a column +- setting a `NOT NULL` constraint with a default value - renaming a column - renaming a table - adding an index non-concurrently (Postgres only) - adding a `json` column to an existing table (Postgres only) @@ -73,10 +74,12 @@ remove_column :users, :some_column end end ``` +Don’t backfill existing rows in this migration, as it can cause downtime. See the next section for how to do it safely. + ### Backfilling data To backfill data, use the Rails console or a separate migration with `disable_ddl_transaction!`. Avoid backfilling in a transaction, especially one that alters a table. See [this great article](https://wework.github.io/data/2015/11/05/add-columns-with-default-values-to-large-tables-in-rails-postgres/) on why. ```ruby @@ -164,13 +167,13 @@ add_index :users, :some_index, algorithm: :concurrently end end ``` -If you forget `disable_ddl_transaction!`, the migration will fail. +If you forget `disable_ddl_transaction!`, the migration will fail. Also, note that indexes on new tables (those created in the same migration) don’t require this. -Also, note that indexes on new tables (those created in the same migration) don’t require this. +Check out [this gem](https://github.com/ankane/gindex) to quickly generate index migrations without memorizing the syntax. ### Adding a json column (Postgres) There’s no equality operator for the `json` column type, which causes issues for `SELECT DISTINCT` queries. @@ -259,20 +262,24 @@ StrongMigrations.auto_analyze = true ``` ## Lock Timeout (Postgres) -It’s a good idea to set a lock timeout for the database user that runs migrations. This way, if migrations can’t acquire a lock in a timely manner, other statements won’t be stuck behind it. +It’s a good idea to set a lock timeout for the database user that runs migrations. This way, if migrations can’t acquire a lock in a timely manner, other statements won’t be stuck behind it. Here’s a great explanation of [how lock queues work](https://www.citusdata.com/blog/2018/02/15/when-postgresql-blocks/). ```sql ALTER ROLE myuser SET lock_timeout = '10s'; ``` There’s also [a gem](https://github.com/gocardless/activerecord-safer_migrations) you can use for this. +## Bigint Primary Keys (Postgres & MySQL) + +Rails 5.1+ uses `bigint` for primary keys to keep you from running out of ids. To get this in earlier versions of Rails, check out [this gem](https://github.com/Shopify/rails-bigint-primarykey). + ## Additional Reading -- [Rails Migrations with No Downtime](http://pedro.herokuapp.com/past/2011/7/13/rails_migrations_with_no_downtime/) +- [Rails Migrations with No Downtime](https://pedro.herokuapp.com/past/2011/7/13/rails_migrations_with_no_downtime/) - [Safe Operations For High Volume PostgreSQL](https://www.braintreepayments.com/blog/safe-operations-for-high-volume-postgresql/) ## Credits Thanks to Bob Remeika and David Waller for the [original code](https://github.com/foobarfighter/safe-migrations).