honeysql

Jakub HolΓ½ (HolyJak) 2023-06-20T08:24:54.386839Z

Hello again! I am at a loss, again πŸ˜… How do I generate

... ON CONFLICT (date_trunc('month', "my_column")) DO  ...
? https://cljdoc.org/d/com.github.seancorfield/honeysql/2.4.1033/api/honey.sql.helpers?q=on-conf#on-conflict does not help me… πŸ™ πŸ™ πŸ™

p-himik 2023-06-20T08:32:10.789859Z

Doesn't seem like there's a way to do it without putting the whole ON CONFLICT into :raw, so might be something that warrants HoneySQL's DSL extension.

πŸ‘ 1
πŸ™ 1
seancorfield 2023-06-20T18:47:56.463129Z

@holyjak I'm fixing this so that this works:

user=> (sql/format {:on-conflict [[:date_trunc [:inline "month"] :my_column]]} {:quoted true})
["ON CONFLICT DATE_TRUNC('month', \"my_column\")"]

seancorfield 2023-06-20T18:48:55.606939Z

Does it specifically need those ( .. ) around the expression to be valid syntax?

seancorfield 2023-06-20T19:09:29.499759Z

https://github.com/seancorfield/honeysql/issues/494 (I'm going to assume it does need the parens).

p-himik 2023-06-20T19:13:12.141959Z

It does, yeah.

seancorfield 2023-06-20T19:15:46.058109Z

Here's how it works now on the latest develop or the latest SNAPSHOT on Clojars:

user=> (sql/format {:on-conflict [:a [:date_trunc [:inline "month"] :my_column] :b]} {:quoted true})
["ON CONFLICT (\"a\", DATE_TRUNC('month', \"my_column\"), \"b\")"]

seancorfield 2023-06-20T19:16:44.470669Z

I'll cut a new release before the end of the month. Not sure what else I'll end up fixing in that release right now.

p-himik 2023-06-20T19:35:01.426559Z

Not sure how to test it properly given that I can't use date_trunc in ON CONFLICT without also creating an index on it, and I can't create an index on it because the function is not marked as immutable. But given the syntax description of INSERT, it seems that you need an extra pair of parents around expressions in ON CONFLICT. Just tried it with col + 1 expression instead of date_trunc - yep, needs extra parens, so the above should be formatted as ON CONFLICT ("a", (DATE_TRUNC('month', "my_column")), "b"). If it's any help, at least in the case of PostgreSQL the whole part after ON CONFLICT follows the CREATE INDEX format as per the docs.

seancorfield 2023-06-20T19:51:32.663859Z

Oh, interesting... it needs parens inside the group too??

seancorfield 2023-06-20T19:51:50.418629Z

(and, no, matching create index doesn't help at all πŸ™‚ )

p-himik 2023-06-20T19:55:16.441629Z

Yeah, otherwise stuff like INSERT INTO t VALUES (1) ON CONFLICT (d + 1) DO NOTHING; fails with ERROR: syntax error at or near "+" Position: 41. Using ((d + 1)) works.

seancorfield 2023-06-20T20:03:48.886999Z

Fix pushed to GH. A new SNAPSHOT will be available "soon".

Jakub HolΓ½ (HolyJak) 2023-06-21T06:08:35.889359Z

Sorry for the late reply. I see you figured everything out. Many thanks to both of you! I'll try the snapshot ASAP

Jakub HolΓ½ (HolyJak) 2023-06-21T12:57:40.707849Z

It works like a charm! thank you!

πŸŽ‰ 1