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

Add constraints format-clause with support for UNIQUE and PRIMARY KEY #60

Conversation

drew-patel
Copy link

@drew-patel drew-patel commented Jan 31, 2021

Related issue - #15

  • new constraints defhelper which adds a top-level :constraints key to the complete-sql-map
  • moved format-clause for :with-columns into its own function so that it can be reused in the constraints format-clause method as well
  • when constraints aren't present, the formatter uses :with-columns to construct the SQL string
  • when constraints are present, the formatter uses :constraints to construct the columns and constraints string wrapped in the same set of parens

@scimetfoo
Copy link
Contributor

scimetfoo commented Feb 1, 2021

@drew-patel thanks for submitting this PR!

A couple of suggestions:

  1. Use defhelper only to update the query map with the constraint clauses and not to construct the SQL function call. We want to utilise the format-clause multimethod to construct the function call so that it works with honeysql's priority-based ordering rule system. Also, add a priority to the constraint clause, which I think would be a number slightly larger than the priority assigned to with-columns. I just noticed that with-columns doesn't have a priority assigned to it either, but it will need one now. Could you please add one for it as well?
  2. Pluralise constraint so that going forward we're able to use this function for defining multiple constraints. Eg:
(constraints [:unique [:product_no :product_sku]]
             [:primary-key [:name]])
  1. Add a top-level key called :constraints to the query map instead of associng the constraints to the collection of columns. It otherwise ends up treating each constraint as a column.
  2. Support multiple constraints? It could also just be multiple unique constraints to being with.

@drew-patel
Copy link
Author

@scimetfoo Thanks for the feedback and suggestions!

All the points are clear. I'll ask questions if I'm unsure about anything while working on them. Will get back to you in a few days with the changes.

@drew-patel drew-patel force-pushed the drew-patel/constraint-helper-for-unique-columns branch from aec0ebd to fd0f47f Compare February 4, 2021 23:23
@drew-patel drew-patel changed the title Create defhelper for constraint with support for unique Add constraints format-clause with support for UNIQUE and PRIMARY KEY Feb 4, 2021
@drew-patel
Copy link
Author

@scimetfoo I have addressed comments 1-3 as described above. I've only implemented the solution for UNIQUE and PRIMARY KEY so far since they behave similarly.

  • I have no idea why I'm getting an extra space after the table name. See one of the tests.

  • Also, I'm not sure if the priority numbers chosen are ok. Happy to fix those if they aren't.

Thanks!

src/honeysql_postgres/format.cljc Outdated Show resolved Hide resolved
src/honeysql_postgres/format.cljc Outdated Show resolved Hide resolved
@drew-patel drew-patel force-pushed the drew-patel/constraint-helper-for-unique-columns branch from fd0f47f to eb8dfd1 Compare February 14, 2021 18:22
Copy link
Contributor

@scimetfoo scimetfoo 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. Please add examples to the readme.

@drew-patel drew-patel force-pushed the drew-patel/constraint-helper-for-unique-columns branch from eb8dfd1 to 405c25f Compare February 28, 2021 21:17
@drew-patel
Copy link
Author

@scimetfoo Please let me know if the examples are sufficient. Thanks again for the review!

@drew-patel drew-patel force-pushed the drew-patel/constraint-helper-for-unique-columns branch from 405c25f to bb7332f Compare February 28, 2021 21:35
Requires `:with-columns` to have its format clause pulled out
into its own function so that it can be used with `constraints`.

When `constraints` are present, the columns are constructed by
the `constraints` formatter so that columns and constraints can
be wrapped by the same set of parens.
@drew-patel drew-patel force-pushed the drew-patel/constraint-helper-for-unique-columns branch from bb7332f to ca0a76c Compare March 19, 2021 14:31
@scimetfoo scimetfoo merged commit b25aed7 into nilenso:master Mar 31, 2021
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