-
Notifications
You must be signed in to change notification settings - Fork 655
Add support for annotating check constraints #868
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
Conversation
Thanks for sharing. This should get merged! |
@ctran any change we can get this merged? |
Here's a 2022 bump to this issue.
|
Hi @lovro-bikic, First of all, I love the new feature ❤️. We've started using your implementation and noticed it has issues with check constraints that go across multiple lines. For example, say I have the following constraint: CONSTRAINT foo_not_null_when_active CHECK (
CASE
WHEN ((status)::text = 'active'::text) THEN (foo IS NOT NULL)
ELSE true
END) Currently this results in the following annotation:
Not sure how we'd want this to be formatted in the annotation? Perhaps we can keep the wrapping. Maybe something like this?
Alternatively could
|
23d6d42
to
b0507b1
Compare
@dtcristo, thanks for the feedback! I would go with multiline expressions squished into a single line, to be consistent with the rest of the annotations. I pushed a commit for it: b850e9b. Side note: CI is failing, though the failures are unrelated to this branch. |
Hi @lovro-bikic 👋, I've had a play with your So it turns out there is a bug in Rails 6.1 (since fixed here for Rails 7 only). The bug means that multi-line constraints are coming through as So your line:
Will error because However, something else I noticed with the change here, is that it will introduce an extra later of parenthesis around the check constraint for normal (one line) constraints. So a constraint that was previously printing as:
Will now print
However with this change a multi-line constraint will correctly print with only one layer of parenthesis. Not sure if this is an issue, but I just wanted to point it out? Potentially if you see wrapping parenthesis you could strip them? |
Thanks for pointing out the issue regarding missing expressions, it should be fixed with this commit: 6449e7d.
I couldn't reproduce this. I tried it out on Rails versions 6.1.5.1 and 7.0.2.4, and Rails in both cases output the constraint expression without the wrapping parentheses, so the annotation would always correctly have a single pair of them. |
It would be really helpful to get this merged. Is there anything we might to to help this along @ctran ? |
I need to take a look to see why the CI is failing. |
@ctran hi there! have you been able to look at this? We found ourselves needing to annotate constraints, and found out about this pull request, it looks like what we need 😄, anything we can do to help get this merged? |
I just need help getting the CI build to pass :-) |
@ctran I rebased the branch but the CI is still failing due to this error:
It seems that the test suite is non-deterministic because when I run I'm not sure how to proceed here because these errors fall outside the scope of this PR, please advise. |
@lovro-bikic I merged #980 |
Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Thanks! I rebased the branch, but the CI failed because of Rubocop. I disabled these offenses but let me know if you want me to fix them in a different way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (we'll deal with the rubocop issues later)
commit 22ab676 Author: Cuong Tran <ctran@users.noreply.github.com> Date: Thu Mar 30 15:17:37 2023 -0700 chore: remove broken badges from README.md commit 10a7a76 Author: Takumi KAGIYAMA <694547+kg8m@users.noreply.github.com> Date: Fri Mar 31 07:15:07 2023 +0900 Support `--frozen` option for routing annotations (ctran#979) The `--frozen` option previously deal only model annotations. This change will support route annotations as well. --------- Signed-off-by: kg8m <takumi.kagiyama@gmail.com> Co-authored-by: Cuong Tran <ctran@users.noreply.github.com> commit a28fef3 Author: Jim Jowdy <jjowdy@gmail.com> Date: Wed Mar 29 15:09:11 2023 -0700 Fix retrieve_indexes_from_table when indexes is empty and base table does not exist. (ctran#849) Some tables may have a table_name_prefix but no indexes. Previous versions of the code would strip the prefix and look for indexes on the resulting table which likely would not exist. This causes DB errors, at least in MySQL. So now check if the new table exists first before trying to show its indexes. commit 13b532d Author: Cuong Tran <ctran@users.noreply.github.com> Date: Wed Mar 29 01:51:15 2023 -0700 Update codeql-analysis.yml commit ea4cd00 Author: Lovro Bikić <lovro.bikic@infinum.hr> Date: Wed Mar 29 10:31:01 2023 +0200 Add support for annotating check constraints (ctran#868) This adds annotation of check constraints with an option to disable/enable annotation. Most of the work done in this PR is based off of existing implementation for annotating indexes and foreign keys. Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr> commit 76a1804 Author: Lovro Bikić <lovro.bikic@gmail.com> Date: Wed Mar 29 10:18:24 2023 +0200 Fix flaky specs (ctran#980) Signed-off-by: Lovro Bikic <lovro.bikic@infinum.hr>
Supports annotating model check constraints, credit goes to folks who worked on ctran/annotate_models#868. Adds new option `show_check_constraints` that defaults to `false`. When enabled, it will add check constraints annotations rails/rails#31323 to the model annotations. It can be enabled also through the command line with options `-c` or `--show-check-constraints`. Resolves #104
Rails added support for check constraints (rails/rails#31323), so I thought it would be nice if we could annotate them in models.
This PR adds annotation of check constraints with an option to disable/enable annotation. Most of the work done in this PR is based off of existing implementation for annotating indexes and foreign keys.