pgroll/examples/14_add_reviews_table.json
Andrew Farries 1da1d9bfec
Implement the set column NOT NULL operation (#63)
Implement the operation to make an existing column `NOT NULL`.

The migration looks like this:

```json
{
  "name": "16_set_not_null",
  "operations": [
    {
      "set_not_null": {
        "table": "reviews",
        "column": "review",
        "up": "product || ' is good'"
      }
    }
  ]
}
```
This migration adds a `NOT NULL` constraint to the `review` column in
the `reviews` table.

* On `Start`:
  *  Create a new column with a `NOT VALID` `NOT NULL` constraint
* Backfill the new column with values from the existing column using the
`up` SQL to replace `NULL` values
* Create a trigger to populate the new column when values are written to
the old column, rewriting `NULLs` with `up` SQL.
* Create a trigger to populate the old column when values are written to
the new column.
* On `Complete`
  * Validate the `NOT VALID` `NOT NULL` constraint on the new column.
  * Add `NOT NULL` to the new column.
  * Remove triggers and the `NOT VALID` `NOT NULL` constraint
  * Drop the old column
  * Rename the new column to the old column name.
* On `Rollback`
  * Remove the new column and both triggers.
2023-08-29 14:53:29 +01:00

31 lines
544 B
JSON

{
"name": "14_add_reviews_table",
"operations": [
{
"create_table": {
"name": "reviews",
"columns": [
{
"name": "id",
"type": "serial",
"pk": true
},
{
"name": "username",
"type": "text"
},
{
"name": "product",
"type": "text"
},
{
"name": "review",
"type": "text",
"nullable": true
}
]
}
}
]
}