Skip to content
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

Adding a serial column #193

Open
wister opened this issue Oct 28, 2023 · 1 comment
Open

Adding a serial column #193

wister opened this issue Oct 28, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@wister
Copy link

wister commented Oct 28, 2023

I tried adding a serial column.

{
  "name": "01_add_test_id",
  "operations": [
    {
      "add_column": {
        "table": "tests",
        "column": {
          "name": "test_id",
          "type": "serial",
          "nullable": false
        }
      }
    }
  ]
}

Which I thought would be equivalent to this sql query:

ALTER TABLE IF EXISTS tests ADD COLUMN test_id serial;

The error message is:
Failed to start migration: migration is invalid: field "up" is required

In this case, what would be the SQL expression for the up property? I have tried with:

...
"up": "",
...
Failed to start migration: unable to execute start operation: failed to create trigger: pq: missing expression at or near ";"
...
"up": "ROW_NUMBER() OVER(ORDER BY created_at ASC)",
...
Failed to start migration: unable to execute start operation: failed to create trigger: pq: syntax error at or near "TRIGGER"

Any suggestions? Thanks!

@exekias exekias added the bug Something isn't working label Oct 30, 2023
@exekias
Copy link
Member

exekias commented Oct 30, 2023

Thank you for reporting!

I gave this a try and to me, this is a bug. We need an up function in order to backfill values for the column, but in the case of SERIAL columns, the default is implicit in the column definition. Providing a default up function that does proper backfilling should be possible. This is true for any column definition that has a default.

I tested this and it worked, but this is only a workaround that relies on pgroll internal names (subject to a change at any time):

  {
    "name": "01_add_test_id",
    "operations": [
      {
        "add_column": {
          "table": "tests",
          "column": {
            "name": "test_id",
            "type": "serial",
            "nullable": false
          },
          "up": "nextval('public.customers__pgroll_new_test_id_seq)"
        }
      }
    ]
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants