README.md in strong_migrations-1.6.4 vs README.md in strong_migrations-1.7.0
- old
+ new
@@ -6,11 +6,11 @@
Supports PostgreSQL, MySQL, and MariaDB
:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
-[![Build Status](https://github.com/ankane/strong_migrations/workflows/build/badge.svg?branch=master)](https://github.com/ankane/strong_migrations/actions)
+[![Build Status](https://github.com/ankane/strong_migrations/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/strong_migrations/actions)
## Installation
Add this line to your application’s Gemfile:
@@ -73,10 +73,11 @@
Postgres-specific checks:
- [adding an index non-concurrently](#adding-an-index-non-concurrently)
- [adding a reference](#adding-a-reference)
- [adding a foreign key](#adding-a-foreign-key)
+- [adding a unique constraint](#adding-a-unique-constraint)
- [adding an exclusion constraint](#adding-an-exclusion-constraint)
- [adding a json column](#adding-a-json-column)
- [setting NOT NULL on an existing column](#setting-not-null-on-an-existing-column)
Config-specific checks:
@@ -303,11 +304,11 @@
A safer approach is to:
1. Create a new table
2. Write to both tables
-3. Backfill data from the old table to new table
+3. Backfill data from the old table to the new table
4. Move reads from the old table to the new table
5. Stop writing to the old table
6. Drop the old table
### Creating a table with the force option
@@ -506,9 +507,42 @@
```ruby
class ValidateForeignKeyOnUsers < ActiveRecord::Migration[7.1]
def change
validate_foreign_key :users, :orders
+ end
+end
+```
+
+### Adding a unique constraint
+
+#### Bad
+
+In Postgres, adding a unique constraint creates a unique index, which blocks reads and writes.
+
+```ruby
+class AddUniqueContraint < ActiveRecord::Migration[7.1]
+ def change
+ add_unique_constraint :users, :some_column
+ end
+end
+```
+
+#### Good
+
+Create a unique index concurrently, then use it for the constraint.
+
+```ruby
+class AddUniqueContraint < ActiveRecord::Migration[7.1]
+ disable_ddl_transaction!
+
+ def up
+ add_index :users, :some_column, unique: true, algorithm: :concurrently
+ add_unique_constraint :users, using_index: "index_users_on_some_column"
+ end
+
+ def down
+ remove_unique_constraint :users, :some_column
end
end
```
### Adding an exclusion constraint