Skip to content

Conversation

kvch
Copy link

@kvch kvch commented Jan 22, 2025

Extend the Go API with a new function:

func DeparseParenthesizedSeqOptList(seqOptions []*Node) (output string, err error)

This function is used to deparse sequence options. Example statement for generating identity columns and setting sequence options:

ALTER TABLE table1 ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (START 5 INCREMENT 5);

the (START 5 INCREMENT 5) portion can be deparsed with this new function.

Based on #6

@kvch kvch force-pushed the feature-deparse-seq-op-list branch from d0c521d to 01d3411 Compare January 22, 2025 11:59
@kvch kvch force-pushed the feature-deparse-seq-op-list branch from 01d3411 to 9fcf9cc Compare January 22, 2025 12:10
@kvch kvch requested a review from andrew-farries January 22, 2025 12:10
@kvch kvch marked this pull request as ready for review January 22, 2025 12:10
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

Have you tried using this in xataio/pgroll#605?

We may as well hold off merging this until we know it works there.

@kvch
Copy link
Author

kvch commented Jan 22, 2025

Yes, I tested it locally. That's why there is an extra branch in the repo: https://github.com/xataio/pg_query_go/tree/feature-deparse-seq-op-list

@kvch kvch merged commit 54118c0 into xataio:main Jan 22, 2025
kvch added a commit to xataio/pgroll that referenced this pull request Jan 22, 2025
This PR adds support for generated columns in `create_table` and
`add_column` operations.

The column definition is extended with the setting named `generated`.

Define a stored column:
```json
...
"generated": {
  "expression": "upper(name)"
}
```

Define a generated identity column:
```json
...
"generated": {
  "identity": {
    "user_specified_values": "ALWAYS",
    "sequence_options": "start 5"
}
```

Identity columns are marked not null by default, so setting `nullable`
is unnecessary.

I changed the existing `fruits` table to use a generated identity column
as a primary key.

```json
{
  "name": "08_create_fruits_table",
  "operations": [
    {
      "create_table": {
        "name": "fruits",
        "columns": [
          {
            "name": "id",
            "type": "bigint",
            "pk": true,
            "generated": {
              "identity": {
                "user_specified_values": "BY DEFAULT"
              }
            }
          },
          {
            "name": "name",
            "type": "varchar(255)",
            "unique": true
          },
          {
            "name": "price",
            "type": "decimal(10,2)"
          }
        ]
      }
    }
  ]
}
```

The PR also contains the transformation required by `sql2pgroll`
project.

Requires xataio/pg_query_go#7

---------

Co-authored-by: Andrew Farries <[email protected]>
andrew-farries pushed a commit that referenced this pull request Apr 25, 2025
Add support for deparsing parenthesized sequence options list
andrew-farries pushed a commit that referenced this pull request Apr 25, 2025
add deparse sequence options list
andrew-farries pushed a commit that referenced this pull request Apr 25, 2025
add deparse sequence options list
andrew-farries pushed a commit that referenced this pull request Apr 25, 2025
add deparse sequence options list
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