PostgreSQL zero-downtime migrations made easy
Go to file
Andrew Farries f2bb2f9581
Make set_unique operations respect the contract for old schema versions (#118)
When `pg-roll` makes a schema change, the contract with the user is that
it will leave the old version of schema unchanged.

When the operation to add a `UNIQUE` constraint was implemented
(https://github.com/xataio/pg-roll/pull/53), this contract was not
respected. The operation adds a unique index to the existing column,
changing the behaviour for users of the old schema.

This PR changes the operation so that it follows a similar pattern to
other operations that were implemented later:
* On `Start`:
  * Duplicate the column.
  * Add a `UNIQUE` index concurrently
* Create `up` and `down` triggers to copy values between the old and new
columns.
  * Backfill values from the old column into the new using `up` SQL
* On `Complete`
  * Create a unique constraint on the new column using the unique index.
  * Drop the old column
  * Rename the column to its old name.
  * Remove `up` and `down` triggers.

Writing correct `up` SQL for the operation is a little more difficult
than for other operations (eg set `NOT NULL`) as it is up to the user to
ensure uniqueness of values. The example migration in this PR appends a
random suffix to each value.
2023-09-22 08:52:15 +00:00
.github Update repo settings for release (#113) 2023-09-21 12:03:28 +00:00
.vscode Add linter to tests (#9) 2023-06-27 16:33:50 +01:00
cmd Add raw SQL operation (#43) 2023-08-30 11:50:59 +02:00
examples Make set_unique operations respect the contract for old schema versions (#118) 2023-09-22 08:52:15 +00:00
pkg Make set_unique operations respect the contract for old schema versions (#118) 2023-09-22 08:52:15 +00:00
.golangci.yml Add linter to tests (#9) 2023-06-27 16:33:50 +01:00
docker-compose.yml Initial commit 2023-06-22 17:30:40 +02:00
go.mod Change module name (#60) 2023-08-22 09:27:58 +01:00
go.sum Upgrade to Go 1 21 (#54) 2023-08-17 07:43:41 +01:00
LICENSE Create LICENSE (#111) 2023-09-21 13:18:39 +00:00
main.go Change module name (#60) 2023-08-22 09:27:58 +01:00
README.md Add migrations state handling (#7) 2023-06-28 11:10:03 +02:00

pg-roll

⚠️ Under development ⚠️

PostgreSQL zero-downtime migrations made easy.

Getting started (development)

  • Bring a development PostgreSQL up:

    docker compose up
    
  • Initialize pg-roll (first time only):

    go run . init
    
  • Start a migration:

    go run . start examples/01_create_tables.json
    
  • Inspect the results:

    psql postgres://localhost -U postgres
    
    \d+ public.*
    \d+ 01_create_tables.*
    
  • (Optional) Rollback the migration (undo):

    go run . rollback
    
  • Complete the migration:

    go run . complete