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

Stop quoting column default values #200

Merged
merged 3 commits into from
Nov 6, 2023
Merged

Conversation

andrew-farries
Copy link
Collaborator

When adding a column to a table (either at creation time or afterwards with an alter table migration), any default value for the column is automatically single quoted.

This makes it easier to define migrations, as the user does not have to remember to add the single quotes in the migration.
For example in this migration:

{
  "name": "28_different_defaults",
  "operations": [
    {
      "create_table": {
        "name": "items",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "name",
            "type": "varchar(255)",
            "default": "unnamed"
          }
        ]
      }
    }
  ]
}

the default value for the name column (unnamed) does not need to be quoted as pgroll does that automatically.

However, this automatic quoting causes a problem when assigning a default value of now() to a timestamp column:

{
  "name": "28_different_defaults",
  "operations": [
    {
      "create_table": {
        "name": "items",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "created_at",
            "type": "timestamptz",
            "default": "now()"
          }
        ]
      }
    }
  ]
}

when run, this will result in a created_at column with a default value of the time at which the migration was run, eg:

| created_at | timestamp with time zone |  not null default '2023-11-06 08:37:01.203691+00'::timestamp with time zone | 

rather than the desired default:

| created_at | timestamp with time zone |  not null default now() |

This PR removes the automatic quoting of default values so that now(), CURRENT_TIMESTAMP etc can be used correctly as column defaults. This pushes some complexity onto users who now have to single-quote those default values that require it.

@andrew-farries andrew-farries merged commit edc78ac into main Nov 6, 2023
22 checks passed
@andrew-farries andrew-farries deleted the fix-default-literals branch November 6, 2023 11:56
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.

None yet

2 participants