From 293165f63650f602465ca23567fc18d2c7ddca45 Mon Sep 17 00:00:00 2001 From: Buck Ryan Date: Sat, 6 Apr 2019 17:47:33 -0400 Subject: [PATCH] Fix query-values priority When used with an upsert clause, query-values was not being rendered with the correct priority. The provided test fails on master and passes on this branch. It was previously rendering the SQL as: ``` INSERT INTO distributors (did, dname) ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING (SELECT ?, ?) ``` which is not valid SQL. The below snippet from psql demonstrates this not working: ``` muncher=# insert into foo (id) select 1 on conflict do nothing; INSERT 0 1 muncher=# insert into foo (id) on conflict do nothing (select 1); ERROR: syntax error at or near "on" LINE 1: insert into foo (id) on conflict do nothing (select 1); ``` --- src/honeysql_postgres/format.cljc | 2 +- test/honeysql_postgres/postgres_test.cljc | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 6d285f8..3a278fb 100644 --- a/src/honeysql_postgres/format.cljc +++ b/src/honeysql_postgres/format.cljc @@ -45,7 +45,7 @@ :offset 210 :lock 215 :values 220 - :query-values 250} + :query-values 221} custom-additions)) (defn override-default-clause-priority diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 1849cd9..321a79b 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -7,8 +7,8 @@ add-column partition-by insert-into-as create-table rename-table drop-table window create-view over with-columns]] - [honeysql.helpers :as sqlh :refer [insert-into values where select - from order-by update sset]] + [honeysql.helpers :as sqlh :refer [insert-into values where select columns + from order-by update sset query-values]] [honeysql.core :as sql] [clojure.test :as test :refer [deftest is testing]])) @@ -45,6 +45,13 @@ (values [{:did 23 :dname "Foo Distributors"}]) (on-conflict :did) (do-update-set! [:dname "EXCLUDED.dname || ' (formerly ' || d.dname || ')'"]) + sql/format))) + (is (= ["INSERT INTO distributors (did, dname) (SELECT ?, ?) ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING" 1 "whatever"] + (-> (insert-into :distributors) + (columns :did :dname) + (query-values (select 1 "whatever")) + (upsert (-> (on-conflict-constraint :distributors_pkey) + do-nothing)) sql/format)))))