Skip to content

Pass all column attributes to new temporary column when adding a new one to the table #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Apr 10, 2025

The following migration was failing due to several reasons:

{
  "operations": [
    {
      "create_table": {
        "name": "users",
        "columns": [
          {
            "name": "id",
            "type": "uuid",
            "pk": true
          }
        ]
      }
    },
    {
      "add_column": {
        "table": "users",
        "column": {
          "name": "name",
          "type": "text",
          "nullable": true
        }
      }
    },
    {
      "create_constraint": {
        "table": "users",
        "columns": [
          "name"
        ],
        "type": "unique",
        "name": "my_custom_name",
        "up": {
          "name": "name"
        },
        "down": {
          "name": "name"
        }
      }
    }
  ]
}

This PR fixes the first problem with the migration. The first problem is when pgroll adds the new column with the temporary name to the table schema it lacks all column attributes. Thus, if a later operation tries to duplicate this duplicated column it fails because we do not know the type of the column, the default value, etc. The SQL statement generated has a syntax error because of the lack of information:

Failed to start migration: unable to execute start operation: failed to duplicate column: pq: syntax error at or near ","

@github-actions github-actions bot temporarily deployed to Docs Preview April 10, 2025 17:00 Inactive
@kvch kvch force-pushed the fix-column-duplication branch from 17f7082 to 7c4faa3 Compare April 10, 2025 17:10
@github-actions github-actions bot temporarily deployed to Docs Preview April 10, 2025 17:10 Inactive
@kvch kvch changed the title Fix multioperation migration: add column then add constraint Pass all column attributes to new temporary column when adding a new one to the table Apr 10, 2025
@kvch kvch marked this pull request as ready for review April 10, 2025 17:17
@kvch kvch requested a review from exekias April 10, 2025 17:17
@exekias
Copy link
Member

exekias commented Apr 11, 2025

would it be possible to add a test that fails without this change?

@kvch
Copy link
Contributor Author

kvch commented Apr 11, 2025

No. At the moment we do not have a standard way to duplicate duplicated columns. First we need to decide how we want to deal with those column, then it can be tested. But I do not see the value of adding a test for this change. Adding more info to a struct is not going to break anything for adding columns.

@exekias
Copy link
Member

exekias commented Apr 11, 2025

Do I understand correctly that you say it is not fixing anything either?

@kvch
Copy link
Contributor Author

kvch commented Apr 11, 2025

It is fixing the syntax error. If we fix the syntax error, then we get to the underlying issue which is that we cannot duplicate the column.

table.AddColumn(o.Column.Name, &schema.Column{
Name: TemporaryName(o.Column.Name),
})
tmpColumn := &schema.Column{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid mistakes if a new field is added to schema.Column we probably want to clone o.Column then change the name maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are two different Column types, I cannot really clone them. I added a function we could call to translate from migrations.Column to schema.Column.

@github-actions github-actions bot temporarily deployed to Docs Preview April 11, 2025 15:13 Inactive
@github-actions github-actions bot temporarily deployed to Docs Preview April 11, 2025 15:20 Inactive
@kvch kvch force-pushed the fix-column-duplication branch from 4785d72 to 699b86d Compare April 11, 2025 15:21
@github-actions github-actions bot temporarily deployed to Docs Preview April 11, 2025 15:21 Inactive
@kvch kvch enabled auto-merge (squash) April 11, 2025 15:21
@kvch kvch merged commit 08c7e01 into xataio:main Apr 11, 2025
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants