Skip to content

Improve regex parsing of CHECK constraints.#5044

Closed
gordthompson wants to merge 6 commits into
sqlalchemy:masterfrom
gordthompson:aaa-pg-check-warning
Closed

Improve regex parsing of CHECK constraints.#5044
gordthompson wants to merge 6 commits into
sqlalchemy:masterfrom
gordthompson:aaa-pg-check-warning

Conversation

@gordthompson
Copy link
Copy Markdown
Member

Fixes: #5039

Description

Avoid parse warnings for PostgreSQL CHECK constraints that are a simple function call.

Checklist

This pull request is:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!

Copy link
Copy Markdown
Member

@zzzeek zzzeek left a comment

Choose a reason for hiding this comment

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

looks good, lets use fixtures for the test case. thanks!

Comment thread test/dialect/postgresql/test_reflection.py
Comment thread test/dialect/postgresql/test_reflection.py Outdated
Comment thread test/dialect/postgresql/test_reflection.py Outdated
Comment thread test/dialect/postgresql/test_reflection.py Outdated
Comment thread test/dialect/postgresql/test_reflection.py Outdated
Comment thread test/dialect/postgresql/test_reflection.py Outdated
Comment thread test/dialect/postgresql/test_reflection.py Outdated
@gordthompson
Copy link
Copy Markdown
Member Author

The beginning of the test now looks like this:

    @testing.provide_metadata
    def test_reflect_check_constraint(self):
        meta = self.metadata

        udf_create = """\
            CREATE OR REPLACE FUNCTION is_positive(
                x integer DEFAULT '-1'::integer)
                RETURNS boolean
                LANGUAGE 'plpgsql'
                COST 100
                VOLATILE 
            AS $BODY$BEGIN
                RETURN x > 0;
            END;$BODY$;
        """
        sa.event.listen(meta, "before_create",
                        sa.DDL(udf_create))
        sa.event.listen(meta, "after_drop",
                        sa.DDL("DROP FUNCTION is_positive"))

        cc_table = Table(
            "pgsql_cc",
            meta,
            Column("a", Integer()),
            CheckConstraint("a > 1 AND a < 5", name="cc1"),
            CheckConstraint("a = 1 OR (a > 2 AND a < 5)", name="cc2"),
            CheckConstraint("is_positive(a)", name="cc3"),
        )

        cc_table.create()

Unfortunately, the CREATE FUNCTION is not getting sent (at all; see attached log), so the CREATE TABLE fails. However, the DROP FUNCTION is getting sent, so that fails, too.

postgresql.log

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Dec 19, 2019

yes call metadata.create_all() instead of table.create()

@gordthompson
Copy link
Copy Markdown
Member Author

Yup, that did the trick. Thanks.

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Dec 19, 2019

are you interested in writing changelog files ?

@gordthompson
Copy link
Copy Markdown
Member Author

are you interested in writing changelog files ?

Sure, I could give it a bash. Just create a 5039.rst file? Right now it looks like they all live in the unreleased_14 folder.

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Dec 19, 2019

OK so, this is a bug that we are targeting at 1.3.x, so it goes into unreleased_13. but yes thanks!

Comment thread doc/build/changelog/unreleased_13/5039.rst Outdated
@zzzeek zzzeek requested a review from sqla-tester December 19, 2019 17:20
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

OK, this is sqla-tester setting up my work to try to get revision b6903c6 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

New Gerrit review created for change b6903c6: https://gerrit.sqlalchemy.org/#/c/sqlalchemy/sqlalchemy/+/1623

@sqla-tester
Copy link
Copy Markdown
Collaborator

mike bayer (zzzeek) wrote:

so on older Posgresql 9.6, we're getting this error:

psycopg2.errors.SyntaxError: syntax error at end of input
LINE 1: DROP FUNCTION is_positive

does CREATE FUNCTION / DROP FUNCTION not exist on PG 9? we can add an exclusion rule to the test if this is the case.

@gordthompson
Copy link
Copy Markdown
Member Author

Ah, it appears that DROP FUNCTION without the argument type(s) is only supported in 10+.

DROP FUNCTION is_positive(integer)

works in 12.1 and it should work in 9.6 as well.

@sqla-tester
Copy link
Copy Markdown
Collaborator

Gord Thompson (gordthompson) wrote:

The pep8/1843 failure "F841 local variable 'cc_table' is assigned to but never used" looks like a new rule. (I have already found the one bloody space that triggered the "trailing whitespace" failure, which also appears to be a new rule.) The sqlalchemy_gerrit/3577 failure looks like a transient.

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Jan 3, 2020

Gord Thompson (gordthompson) wrote:

The pep8/1843 failure "F841 local variable 'cc_table' is assigned to but never used" looks like a new rule. (I have already found the one bloody space that triggered the "trailing whitespace" failure, which also appears to be a new rule.) The sqlalchemy_gerrit/3577 failure looks like a transient.

F841 has been in for awhile. I'm working on F821 in https://gerrit.sqlalchemy.org/#/c/sqlalchemy/sqlalchemy/+/1649/ but F841 was in since 190e013

@gordthompson
Copy link
Copy Markdown
Member Author

Yup, you're right. We had to change cc_table.create() to metadata.create_all() and that got rid of the usage. Anyway, we're passing now.

@sqla-tester
Copy link
Copy Markdown
Collaborator

mike bayer (zzzeek) wrote:

Code-Review+2 Workflow+1

@sqla-tester
Copy link
Copy Markdown
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/1623 has been merged. Congratulations! :)

@sqla-tester
Copy link
Copy Markdown
Collaborator

mike bayer (zzzeek) wrote:

Code-Review+2 Workflow+1

@sqla-tester
Copy link
Copy Markdown
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/1672 has been merged. Congratulations! :)

sqlalchemy-bot pushed a commit that referenced this pull request Jan 18, 2020
Fixed issue where the PostgreSQL dialect would fail to parse a reflected
CHECK constraint that was a boolean-valued function (as opposed to a
boolean-valued expression).

Fixes: #5039
Closes: #5044
Pull-request: #5044
Pull-request-sha: b6903c6

Change-Id: I7d39b104a8ce346cb593d541c1b4e5eab88867f9
(cherry picked from commit d8ac1e9)
@gordthompson gordthompson deleted the aaa-pg-check-warning branch January 19, 2020 17:34
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.

Could not parse CHECK constraint text

3 participants