pgroll/examples
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
..
01_create_tables.json Format the examples with jq (#21) 2023-07-06 15:26:29 +01:00
02_create_another_table.json Format the examples with jq (#21) 2023-07-06 15:26:29 +01:00
03_add_column.json Implement Start for adding columns with NOT NULL and no DEFAULT (#37) 2023-07-21 07:47:42 +01:00
04_rename_table.json Rename table op (#23) 2023-07-11 08:01:05 +00:00
05_sql.json Add raw SQL operation (#43) 2023-08-30 11:50:59 +02:00
06_add_column_to_sql_table.json Add raw SQL operation (#43) 2023-08-30 11:50:59 +02:00
07_drop_table.json Implement the drop table operation (#45) 2023-08-17 06:43:42 +01:00
08_create_fruits_table.json Implement 'drop column' migrations (#48) 2023-08-17 07:37:48 +01:00
09_drop_column.json Implement 'drop column' migrations (#48) 2023-08-17 07:37:48 +01:00
10_create_index.json Make index name mandatory on create index operation (#59) 2023-08-18 08:55:25 +01:00
11_drop_index.json Make index name mandatory on create index operation (#59) 2023-08-18 08:55:25 +01:00
12_create_employees_table.json Implement 'rename column' migrations (#52) 2023-08-18 06:49:27 +01:00
13_rename_column.json Add 'alter column' operation to combine some existing operations (#91) 2023-09-12 12:10:13 +01:00
14_add_reviews_table.json Implement the set column NOT NULL operation (#63) 2023-08-29 14:53:29 +01:00
15_set_column_unique.json Make set_unique operations respect the contract for old schema versions (#118) 2023-09-22 08:52:15 +00:00
16_set_not_null.json Move set_not_null operation into alter_column (#93) 2023-09-14 05:47:16 +01:00
17_add_rating_column.json Implement the 'change column type' operation (#74) 2023-09-01 13:37:43 +01:00
18_change_column_type.json Add 'alter column' operation to combine some existing operations (#91) 2023-09-12 12:10:13 +01:00
19_create_orders_table.json Make naming FOREIGN KEY constraints mandatory (#100) 2023-09-15 11:39:17 +01:00
20_create_posts_table.json Add support for adding a foreign key constraint to an existing column (#82) 2023-09-11 06:12:59 +01:00
21_add_foreign_key_constraint.json Make naming FOREIGN KEY constraints mandatory (#100) 2023-09-15 11:39:17 +01:00
22_add_check_constraint.json Move constraint name and expression into a new CheckConstraint struct (#107) 2023-09-19 10:49:40 +01:00
23_drop_check_constraint.json Implement the drop_constraint operation (#103) 2023-09-19 05:32:53 +01:00
24_drop_foreign_key_constraint.json Implement the drop_constraint operation (#103) 2023-09-19 05:32:53 +01:00
25_add_table_with_check_constraint.json Allow columns with CHECK constraints in create table operations (#108) 2023-09-19 10:00:55 +00:00
26_add_column_with_check_constraint.json Allow columns with CHECK constraints on add column operations (#109) 2023-09-20 09:52:22 +01:00
27_drop_unique_constraint.json Add a testcase for dropping unique constraints (#117) 2023-09-21 08:27:20 +00:00