honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
snorremd 2021-05-01T17:17:12.215700Z

Hi! I'm struggling to combine the with and insert-into functions to build a query. Any help is appreciated. EDIT: Found the answer using the pure clojure map specification:

{:with [[:workday {:insert-into :workdays
                   :values [{:work-date (t/today)
                             :work-interval (t/new-duration 7 :hours)
                             :account-id #uuid "5d90856c-5a48-46ae-8f6e-a06295ea0dfb"}]
                   :on-conflict [:work-date :account-id]
                   :do-update-set [:work-interval]
                   :returning [:id]}]]
 :insert-into [[:work-intervals [:workday-id :interval-start]]
               {:select [:workday.id (t/now)]
                :from :workday}]}
Where I want to end up:
WITH workday_insert AS (INSERT INTO workdays (work_date, work_interval, account_id) VALUES (?, ?, ?) ON CONFLICT (work_date, account_id) DO UPDATE SET work_interval = EXCLUDED.work_interval RETURNING id)
INSERT INTO work_intervals (workday_id, interval_start) INSERT INTO work_intervals (workday_id, interval_start) SELECT id, ? FROM workday_insert
What I have so far:
(-> (hsqlh/with [:workday-insert (-> (hsqlh/insert-into :workdays)
                                     (hsqlh/values [{:work-date (t/today)
                                                     :work-interval (t/new-duration 7 :hours)
                                                     :account-id #uuid "5d90856c-5a48-46ae-8f6e-a06295ea0dfb"}])
                                     (hsqlh/on-conflict :work-date :account-id)
                                     (hsqlh/do-update-set :work-interval)
                                     (hsqlh/returning :id))])
    (hsqlh/insert-into :work-intervals
                       [:workday-id :interval-start]
                       (-> (hsqlh/select :id (t/now))
                           (hsqlh/from :workday-insert))))
Producing:
["WITH workday_insert AS (INSERT INTO workdays (work_date, work_interval, account_id) VALUES (?, ?, ?) ON CONFLICT (work_date, account_id) DO UPDATE SET work_interval = EXCLUDED.work_interval RETURNING id) INSERT INTO work_intervals (workday_id, interval_start)"
 #time/date "2021-05-01"
 #time/duration "PT7H"
 #uuid "5d90856c-5a48-46ae-8f6e-a06295ea0dfb"]
It seems the select and from portion of the insert-into is simply dropped from the query. Not sure why. When running the insert-into function on its own and formatting I get:
INSERT INTO work_intervals (workday_id, interval_start) SELECT id, ? FROM workday_insert
For reference I'm using the latest code of the v2-branch.
com.github.seancorfield/honeysql {:git/url "<https://github.com/seancorfield/honeysql.git>"
                                  :sha "20cba15da22946d60f53dd6913caba990f7cddb7"

seancorfield 2021-05-01T18:26:54.216800Z

@snorremd Can you open an issue on GitHub and I'll take a look at this next week when I'm back at work?

seancorfield 2021-05-01T18:27:59.217900Z

In the meantime, have a look in the v2 tests for insert-into / :insert-into -- pretty sure there's an INSERT INTO .. SELECT .. example there somewhere.

snorremd 2021-05-01T18:40:44.223100Z

I'll try to make a smallest possible reproduction of the working and non-working code in a repo and file an issue as soon as possible. This should make it easier for you to verify/test the issue. The INSERT INTO .. SELECT .. standalone case works well both with utility functions and the clojure map syntax, so there might be something with the utility functions and combining the insert-into and with statements. Many thanks for the quick reply. 👍

seancorfield 2021-05-01T19:28:42.223600Z

@snorremd It's a bug in the insert-into helper.

seancorfield 2021-05-01T19:30:12.224500Z

It doesn't expect to have a statement to already merge into.

seancorfield 2021-05-01T19:49:16.224700Z

https://github.com/seancorfield/honeysql/issues/324

snorremd 2021-05-01T19:57:15.225500Z

Ah, I see. Thanks for looking into that.

seancorfield 2021-05-01T19:57:15.225600Z

@snorremd The fix is on the v2 branch if you want to test it?

snorremd 2021-05-01T19:57:28.226100Z

Yeah, definitely.

seancorfield 2021-05-01T19:57:58.226300Z

New sha 46b3c1773b57ad17efda63f5ceee4f24c221ad0a

snorremd 2021-05-01T19:59:58.227200Z

I can verify that it now works as expected.

seancorfield 2021-05-01T20:00:21.227600Z

Thanks for finding that bug! This is why we have Beta releases 🙂

snorremd 2021-05-01T20:01:51.228600Z

Yeah, no problem! In the future, would you prefer if I just open issues for problems like these? I was not 100% sure if it was a bug in honeysql or some problem with my code, so I used the slack channel.

seancorfield 2021-05-01T20:03:17.229900Z

Posting the example here is good. Others may see it and have input, in case there are workarounds or it isn't a bug.

👍 2
seancorfield 2021-05-01T20:04:55.230800Z

And my exposure to more "exotic" SQL is somewhat limited so I'm not always sure whether a given syntax is legal, especially for PostgreSQL 🙂