mirror of
https://github.com/xataio/pgroll.git
synced 2024-09-11 13:55:28 +03:00
9a08b6cc77
Implement `Start` for **add column** operations that add a `NOT NULL` column without a `DEFAULT`. To add such a column without forcing a exclusive lock while a full table scan is performed, these steps need to be followed: On `Start`: 1. Add the new column 2. Add a `CHECK IS NOT NULL` constraint to the new column, but with `NOT VALID`, to avoid the scan. 3. Backfill the new column with the provided `up` SQL. On `Complete` 1. Validate the constraint (with `ALTER TABLE VALIDATE CONSTRAINT`). 2. Add the `NOT NULL` attribute to the column. The presence of a valid `NOT NULL` constraint on the column means that adding `NOT NULL` to the column does not perform a full table scan. See [this post](https://medium.com/paypal-tech/postgresql-at-scale-database-schema-changes-without-downtime-20d3749ed680#00dc) for a summary of these steps.
39 lines
730 B
JSON
39 lines
730 B
JSON
{
|
|
"name": "03_add_column_to_products",
|
|
"operations": [
|
|
{
|
|
"add_column": {
|
|
"table": "products",
|
|
"up": "UPPER(name)",
|
|
"column": {
|
|
"name": "description",
|
|
"type": "varchar(255)",
|
|
"nullable": true
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"add_column": {
|
|
"table": "products",
|
|
"column": {
|
|
"name": "stock",
|
|
"type": "int",
|
|
"nullable": false,
|
|
"default": "100"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"add_column": {
|
|
"table": "products",
|
|
"up": "name || '-category'",
|
|
"column": {
|
|
"name": "category",
|
|
"type": "varchar(255)",
|
|
"nullable": false
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|