Skip to content

Commit

Permalink
Fix create table default value
Browse files Browse the repository at this point in the history
  • Loading branch information
kenichsberg committed Dec 6, 2023
1 parent da1f153 commit d4ade21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
29 changes: 11 additions & 18 deletions src/dsql/pg.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ")"))))
Expand Down
13 changes: 7 additions & 6 deletions test/dsql/pg_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit d4ade21

Please sign in to comment.