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

No backfill needed for empty table #636

Open
agedemenli opened this issue Jan 29, 2025 · 0 comments · May be fixed by #642
Open

No backfill needed for empty table #636

agedemenli opened this issue Jan 29, 2025 · 0 comments · May be fixed by #642
Assignees

Comments

@agedemenli
Copy link
Contributor

agedemenli commented Jan 29, 2025

Primary key or a UNIQUE NOT NULL column is required for backfills. When running DDL on empty (probably newly created) tables, pgroll errors out because the backfill fails. Since backfill is actually not needed for an empty table, we should consider skipping the backfill step for empty tables. That would allow users to modify the tables that they just created freely.

For example, create a table without identity column, run below migration:
(just removed the primary key part from ./examples/43_create_tickets_table.json)

{
  "name": "43_create_tickets_table",
  "operations": [
    {
      "create_table": {
        "name": "tickets",
        "columns": [
          {
            "name": "ticket_id",
            "type": "serial"
          },
          {
            "name": "sellers_name",
            "type": "varchar(255)"
          },
          {
            "name": "sellers_zip",
            "type": "integer"
          },
          {
            "name": "ticket_type",
            "type": "varchar(255)",
            "default": "'paper'"
          }
        ]
      }
    }
  ]
}

And then try to add a unique constraint, by simply running migration ./examples/44_add_table_unique_constraint.json

{
  "name": "44_add_table_unique_constraint",
  "operations": [
    {
      "create_constraint": {
        "type": "unique",
        "table": "tickets",
        "name": "unique_zip_name",
        "columns": [
          "sellers_name",
          "sellers_zip"
        ],
        "up": {
          "sellers_name": "sellers_name",
          "sellers_zip": "sellers_zip"
        },
        "down": {
          "sellers_name": "sellers_name",
          "sellers_zip": "sellers_zip"
        }
      }
    }
  ]
}
./pgroll start ./examples/44_add_table_unique_constraint.json --complete
  ERROR   Failed to start migration: unable to backfill table "tickets": a backfill is required but table "tickets" doesn't have a single column primary key or a UNIQUE, NOT NULL column 
@agedemenli agedemenli self-assigned this Jan 29, 2025
@agedemenli agedemenli linked a pull request Jan 30, 2025 that will close this issue
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 a pull request may close this issue.

1 participant