Commit Graph

4 Commits

Author SHA1 Message Date
Andrew Farries
9a08b6cc77
Implement Start for adding columns with NOT NULL and no DEFAULT (#37)
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.
2023-07-21 07:47:42 +01:00
Andrew Farries
b4efd8ad50
Support up SQL on add column operations (#34)
Add a new field `Up` to **add column** migrations:

```json
{
  "name": "03_add_column_to_products",
  "operations": [
    {
      "add_column": {
        "table": "products",
        "up": "UPPER(name)",
        "column": {
          "name": "description",
          "type": "varchar(255)",
          "nullable": true
        }
      }
    }
  ]
}
```

The SQL specified by the `up` field will be run whenever an row is
inserted into the underlying table when the session's `search_path` is
not set to the latest version of the schema.

The `up` SQL snippet can refer to existing columns in the table by name
(as in the the above example, where the `description` field is set to
`UPPER(name)`).
2023-07-20 06:37:03 +01:00
Andrew Farries
dce42da85a
Support adding columns with UNIQUE, NOT NULL and DEFAULT constraints (#30)
Allow the **add column** operation to add columns with `NOT NULL`,
`UNIQUE` and `DEFAULT` constraints by re-using the SQL generation code
that adds columns to tables.
2023-07-13 08:38:43 +01:00
Andrew Farries
6430f05353
Implement 'add column' migrations for nullable columns (#20)
Add support for **add column** migrations in the simple case where the
new column is nullable.

Add tests for the new operation, covering start, rollback and complete,
in the cases where the add column operation is running against a table
created in an earlier migration and in the case where the the column is
added to a table created in an operation earlier in the migration.

Reshape [offers](https://github.com/fabianlindfors/reshape#add-column)
an `up` option when adding a column, to allow users to backfill the new
column. This PR does not implement this feature.
2023-07-07 11:23:19 +01:00