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

Support deferrable unique constraint for PostgreSQL #186

Merged
merged 4 commits into from
Jan 22, 2022

Conversation

yuku
Copy link
Contributor

@yuku yuku commented Jan 21, 2022

This PR will make psqldef handle DDL like:

alter table dummy add constraint dummy_uniq unique (column_a, column_b) deferrable initially deferred;

PostgreSQL: Documentation: 14: ALTER TABLE

@yuku
Copy link
Contributor Author

yuku commented Jan 21, 2022

@k0kubun In 52f4809 I tried to fix the sqlparser to accept the expected DDL, but when I run the tests I added, I get syntax errors. I spent several hours trying to debug it, but I still haven't been able to, partly because I'm new to goyacc.

Can you see what I'm missing?

By the way, if you think it is much easier for you to write another patch by yourself from scratch rather than reviewing my code, please feel free to close this PR 🙏

cd cmd/psqldef && PGHOST=127.0.0.1 PGSSLMODE=disable go test -v -run TestPsqldefAddConstraintUnique
=== RUN   TestPsqldefAddConstraintUnique
    psqldef_test.go:668: failed to execute './psqldef -Upostgres psqldef_test --file schema.sql' (error: 'exit status 1'): `found syntax error when parsing DDL "alter table dummy add constraint dummy_uniq unique (column_a, column_b) not deferrable initially immediate": syntax error at position 87 near 'deferrable'
        `
    psqldef_test.go:668: expected '-- Nothing is modified --
        ' but got 'found syntax error when parsing DDL "alter table dummy add constraint dummy_uniq unique (column_a, column_b) not deferrable initially immediate": syntax error at position 87 near 'deferrable'
        '
...

@k0kubun k0kubun marked this pull request as ready for review January 22, 2022 05:11
@k0kubun
Copy link
Collaborator

k0kubun commented Jan 22, 2022

I tried to fix the sqlparser to accept the expected DDL, but when I run the tests I added, I get syntax errors. I spent several hours trying to debug it, but I still haven't been able to, partly because I'm new to goyacc.

You declared new tokens on the parser side, but you didn't let the lexer generate the tokens. So those keywords were recognized as if they were normal identifiers instead of keywords. Fixing it is as simple as 16aa629.

You also need to touch schema/ast.go and schema/parser.go to complete the generator implementation. However, it's just specific to the current codebase that's a bit messed up. So it's not your fault. I plan to change the architecture of how schema package works, but the work has not been completed yet. Anyway it's fixed in 53ef6c4. I needed to delete a test case because:

2022/01/21 21:09:40 pq: constraint declared INITIALLY DEFERRED must be DEFERRABLE

Copy link
Collaborator

@k0kubun k0kubun left a comment

Choose a reason for hiding this comment

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

Thanks!

@k0kubun k0kubun merged commit 5702cb7 into sqldef:master Jan 22, 2022
@k0kubun
Copy link
Collaborator

k0kubun commented Jan 22, 2022

Released as v0.11.24.

@yuku yuku deleted the deferrable branch January 22, 2022 09:01
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