From d4ade213388bb2cc6ffda16c8cb1ad5f7e76079e Mon Sep 17 00:00:00 2001 From: kenichsberg Date: Wed, 6 Dec 2023 04:21:28 +0200 Subject: [PATCH] Fix create table default value --- src/dsql/pg.clj | 29 +++++++++++------------------ test/dsql/pg_test.clj | 13 +++++++------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/dsql/pg.clj b/src/dsql/pg.clj index 28088b6..6d94696 100644 --- a/src/dsql/pg.clj +++ b/src/dsql/pg.clj @@ -1113,29 +1113,22 @@ (conj ")")))))) ["end )"]))) -(defn mk-columns [columns] +(defn mk-columns [opts columns] (->> columns (reduce-kv - (fn [acc column val] + (fn [acc column {:keys [type primary-key not-null default] :as val}] (cond (vector? val) - (conj acc [(str "\"" (name column) "\"") (ql/fast-join " " (map name val))]) + (conj acc (str "\"" (name column) "\" " (str/join " " (map name val)))) (map? val) - (->> [(str "\"" (name column) "\"") - (name (:type val)) - (when (:not-null val) "NOT NULL") - (when (:primary-key val) "PRIMARY KEY") - (when (:default val) - ;; this is a workaround to make :current_timestamp work - ;; TODO come up with a more clever design - (if (keyword? (:default val)) - (str "DEFAULT " (name (:default val))) - ["DEFAULT ?" (:default val)]))] - (filter some?) - (conj acc)))) + (conj acc + (cond-> (str "\"" (name column) "\" " type) + primary-key (str " PRIMARY KEY") + not-null (str " NOT NULL") + default (str " DEFAULT " (first (ql/to-sql [] opts default))))))) []) - (join-vec ","))) + (str/join " , "))) (defn mk-options [options] (->> options @@ -1156,8 +1149,8 @@ (conj "TABLE") (cond-> not-ex (conj "IF NOT EXISTS")) (identifier opts table-name) - (cond-> columns (into (concat ["("] (mk-columns columns) [")"]))) - (cond-> partition-of (conj "partition of" (name partition-of) )) + (cond-> columns (conj (str "( " (mk-columns opts columns) " )"))) + (cond-> partition-of (conj "partition of" (name partition-of))) (cond-> for (conj "for values" (when-let [f (:from for)] (str "from (" f ")")) (when-let [t (:to for)] (str "to (" t ")")))) diff --git a/test/dsql/pg_test.clj b/test/dsql/pg_test.clj index a233994..9a02ec9 100644 --- a/test/dsql/pg_test.clj +++ b/test/dsql/pg_test.clj @@ -610,6 +610,7 @@ [:jsonb/#>> :d.resource [:partOf :id]]] ["( case ( resource->>'date' ) when ( '123' ) then ( e.resource ->> 'processed' ) when ( '456' ) then ( e.resource ->> 'order_id' ) else ( d.resource #>> '{partOf,id}' ) end )"]) + (testing "CREATE TABLE" (format= {:ql/type :pg/create-table @@ -643,25 +644,25 @@ (testing "default value" - - (format= {:ql/type :pg/create-table :table-name :mytable :columns {:a {:type "integer" :not-null true :default 8}}} - ["CREATE TABLE mytable ( \"a\" integer NOT NULL DEFAULT ? )" 8]) + ["CREATE TABLE mytable ( \"a\" integer NOT NULL DEFAULT 8 )"]) (format= {:ql/type :pg/create-table :table-name "mytable" :columns {:a {:type "integer" :not-null true :default 8}}} - ["CREATE TABLE mytable ( \"a\" integer NOT NULL DEFAULT ? )" 8]) + ["CREATE TABLE mytable ( \"a\" integer NOT NULL DEFAULT 8 )"]) (format= {:ql/type :pg/create-table :table-name "mytable" - :columns {:a {:type "timestamp" :not-null true :default :current_timestamp}}} - ["CREATE TABLE mytable ( \"a\" timestamp NOT NULL DEFAULT current_timestamp )"])) + :columns {:ts {:type "timestamp" :default :current_timestamp :not-null true} + :meta_status {:type "resource_status" :default "created" :not-null true}}} + ["CREATE TABLE mytable ( \"ts\" timestamp NOT NULL DEFAULT current_timestamp , \"meta_status\" resource_status NOT NULL DEFAULT 'created' )"]) + ) (testing "without columns" (format=