Fix do update set; add on conflict tests
This commit is contained in:
parent
61cf6eda5a
commit
6db2426046
2 changed files with 42 additions and 5 deletions
|
|
@ -308,8 +308,7 @@
|
|||
(if (keyword? x)
|
||||
(let [e (format-entity x {:drop-ns? true})]
|
||||
[(str (sql-kw k) " " e " = EXCLUDED." e)])
|
||||
(let [[sql & params] (format-set-exprs :set x)]
|
||||
(into [(str (sql-kw k) " " sql)] params))))
|
||||
(format-set-exprs k x)))
|
||||
|
||||
(def ^:private base-clause-order
|
||||
"The (base) order for known clauses. Can have items added and removed.
|
||||
|
|
|
|||
|
|
@ -399,6 +399,44 @@
|
|||
:from [[:foo :f]]
|
||||
:cross-join [[:bar :b]]}))))
|
||||
|
||||
(comment ; make on conflict tests, based on nilenso repo and PG docs
|
||||
(format {:insert-into :foo, :values [[1 2 3]], :on-conflict :e :do-nothing true})
|
||||
(format {:insert-into :foo, :values [[1 2 3]], :on-conflict {:on-constraint :foo_key} :do-update-set {:a 42 :b :excluded.b}}))
|
||||
(deftest on-conflict-tests
|
||||
;; these examples are taken from https://www.postgresqltutorial.com/postgresql-upsert/
|
||||
(is (= ["
|
||||
INSERT INTO customers
|
||||
(name, email)
|
||||
VALUES ('Microsoft', 'hotline@microsoft.com')
|
||||
ON CONFLICT ON CONSTRAINT customers_name_key
|
||||
DO NOTHING
|
||||
"]
|
||||
(format {:insert-into :customers
|
||||
:columns [:name :email]
|
||||
:values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]]
|
||||
:on-conflict {:on-constraint :customers_name_key}
|
||||
:do-nothing true}
|
||||
{:pretty? true})))
|
||||
(is (= ["
|
||||
INSERT INTO customers
|
||||
(name, email)
|
||||
VALUES ('Microsoft', 'hotline@microsoft.com')
|
||||
ON CONFLICT (name)
|
||||
DO NOTHING
|
||||
"]
|
||||
(format {:insert-into :customers
|
||||
:columns [:name :email]
|
||||
:values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]]
|
||||
:on-conflict :name
|
||||
:do-nothing true}
|
||||
{:pretty? true})))
|
||||
(is (= ["
|
||||
INSERT INTO customers
|
||||
(name, email)
|
||||
VALUES ('Microsoft', 'hotline@microsoft.com')
|
||||
ON CONFLICT (name)
|
||||
DO UPDATE SET email = EXCLUDED.email || ';' || customers.email
|
||||
"]
|
||||
(format {:insert-into :customers
|
||||
:columns [:name :email]
|
||||
:values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]]
|
||||
:on-conflict :name
|
||||
:do-update-set {:email [:|| :EXCLUDED.email [:inline ";"] :customers.email]}}
|
||||
{:pretty? true}))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue