xataio / pgroll

PostgreSQL zero-downtime migrations made easy
https://www.xata.io
Apache License 2.0
2.97k stars 54 forks source link

Down SQL in 'alter column' operation that renames a column should use the new column name #350

Closed andrew-farries closed 3 months ago

andrew-farries commented 4 months ago

When a column is renamed and modified in some other way that requires an up and down SQL (eg changing its type), migration authors should be able to refer to the column in the down SQL by the new name of the column, as in this example:

{
  "name": "02_alter_column",
  "operations": [
    {
      "alter_column": {
        "table": "products",
        "column": "age",
        "name": "product_age",
        "type": "integer",
        "up": "CAST(age AS integer)",
        "down": "CAST(product_age AS text)"
      }
    }
  ]
}

Currently, the above migration will fail because the down SQL in the above example needs to be written as though the column were still called age, ie the column name before the rename.

For alter column operations involving a rename, the generated down trigger should change in such a way that the name of column in the user supplied down SQL can use the renamed column name.