mirror of
https://github.com/xataio/pgroll.git
synced 2024-10-06 01:57:32 +03:00
f2bb2f9581
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. |
||
---|---|---|
.. | ||
01_create_tables.json | ||
02_create_another_table.json | ||
03_add_column.json | ||
04_rename_table.json | ||
05_sql.json | ||
06_add_column_to_sql_table.json | ||
07_drop_table.json | ||
08_create_fruits_table.json | ||
09_drop_column.json | ||
10_create_index.json | ||
11_drop_index.json | ||
12_create_employees_table.json | ||
13_rename_column.json | ||
14_add_reviews_table.json | ||
15_set_column_unique.json | ||
16_set_not_null.json | ||
17_add_rating_column.json | ||
18_change_column_type.json | ||
19_create_orders_table.json | ||
20_create_posts_table.json | ||
21_add_foreign_key_constraint.json | ||
22_add_check_constraint.json | ||
23_drop_check_constraint.json | ||
24_drop_foreign_key_constraint.json | ||
25_add_table_with_check_constraint.json | ||
26_add_column_with_check_constraint.json | ||
27_drop_unique_constraint.json |