Commit Graph

4 Commits

Author SHA1 Message Date
Andrew Farries
4d3faebffd
Make down SQL in rename column operations use the new name of the column (#354)
Ensure that 'alter column' operations that rename a column and also
specify `down` SQL (such as those that alter some other column attribute
at the time of the rename) must use the new name of the column in the
`down` SQL.

Without this change, the `down` SQL would require the use of the old
column name.

Fixes #350
2024-05-16 11:57:53 +01:00
Andrew Farries
4fbfdf7b7e
Add a 'set comment' sub-operation to 'alter column' (#344)
Allow 'alter column' operations to set the comment on a column:

```json
{
  "name": "02_change_comment",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "comment": "The full name of the event"
      }
    }
  ]
}
```

This is a versioned migration so the column to which the comment is
applied is duplicated and backfilled according to the `up` and `down`
SQL supplied with the 'alter column' operation. `up` and `down` default
to a simple copy of the field between new and old columns.

The intention is that this can be combined with a 'change type'
sub-operation if there is some column metadata that should be updated as
part of the type change:

```json
{
  "name": "35_alter_column_multiple",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "name": "event_name",
        "type": "text",
        "comment": "{type: some-metadata}",
        "up": "name",
        "down": "name"
      }
    }
  ]
}
```

Fixes #328
2024-04-29 10:40:10 +01:00
Andrew Farries
afad3d88cd
Add a 'set default' sub-operation to 'alter column' (#346)
Allow 'alter column' operations to set the default value on a column:

```json
{
  "name": "02_change_default",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "default": "'new default value'"
      }
    }
  ]
}
```

This is a versioned migration so the column to which the default is
applied is duplicated and backfilled according to the `up` and `down`
SQL supplied with the 'alter column' operation. `up` and `down` default
to a simple copy of the field between new and old columns.

Fixes #327
2024-04-25 11:47:44 +01:00
Andrew Farries
a4222d8f8c
Support multiple 'alter column' sub operations (#338)
Allow 'alter column' operations to include multiple sub-operations. This
means migrations like this one are now possible:

```json
{
  "name": "35_alter_column_multiple",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "name": "event_name",
        "type": "text",
        "nullable": false,
        "unique": {
          "name": "events_event_name_unique"
        },
        "check": {
          "name": "event_name_length",
          "constraint": "length(name) > 3"
        },
        "up": "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)",
        "down": "name"
      }
    }
  ]
}
```

This 'alter column' operation:
* Renames a column
* Changes its type
* Sets it `NOT NULL`
* Adds a unique constraint
* Adds a check constraint

Previously, this would have required 5 different operations.

Builds on https://github.com/xataio/pgroll/pull/337. Fixes
https://github.com/xataio/pgroll/issues/336
2024-04-22 12:11:15 +01:00