Skip to content

Commit

Permalink
add unsigned int support for postgresql/malli schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ixffxi committed Apr 7, 2023
1 parent 94c404c commit d5d411a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


(def lib 'ai.motiva/dbxray)
(def version "0.1.0")
(def version "0.1.1")


(defn deploy
Expand Down
10 changes: 8 additions & 2 deletions src/donut/dbxray.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
(filter (complement :non_unique))
seq))
:autoincrement? (fn [{:keys [is_autoincrement] :as _raw}]
(= "YES" is_autoincrement))}})
(= "YES" is_autoincrement))
:unsigned? (fn [_]
false)}})

(defmulti adapter* :dbtype)

(defmethod adapter* :postgresql
[_]
{:schema-pattern "public"
:column-types {#"serial" :integer}})
:column-types {#"serial" :integer}
:predicates {:unsigned? (fn [{:keys [type_name] :as _raw}]
(= "uint" type_name))}})

(defmethod adapter* :sqlite
[_]
Expand Down Expand Up @@ -142,6 +146,7 @@
nullable? ((:nullable? predicates) raw-column)
unique? ((:unique? predicates) raw-column)
autoincrement? ((:autoincrement? predicates) raw-column)
unsigned? ((:unsigned? predicates) raw-column)
primary-key? (get pks column_name)]
(assoc cols-map
(keyword column_name)
Expand All @@ -151,6 +156,7 @@
primary-key? (assoc :primary-key? true)
unique? (assoc :unique? true)
autoincrement? (assoc :autoincrement? true)
unsigned? (assoc :unsigned? true)
fk-ref (assoc :refers-to fk-ref)))))
{}
table-cols)))
Expand Down
6 changes: 5 additions & 1 deletion src/donut/dbxray/generate/malli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

(def column-types
{:integer 'int?
:uint 'pos-int?
:integer-pk 'pos-int?
:clob 'string?
:text 'string?
Expand All @@ -28,7 +29,7 @@
(column-spec nil xray table-name column-name))

([enums xray table-name column-name]
(let [{:keys [column-type primary-key? nullable? refers-to]} (get-in xray [table-name :columns column-name])]
(let [{:keys [column-type primary-key? nullable? unsigned? refers-to]} (get-in xray [table-name :columns column-name])]
[(column-spec-name table-name column-name)
{:optional? (boolean nullable?)}

Expand All @@ -39,6 +40,9 @@
(and (= :integer column-type) primary-key?)
(:integer-pk column-types)

(and (= :integer column-type) unsigned?)
(:uint column-types)

;; :enum type definition
(str/includes? column-type ".")
(let [column (-> column-type (name) (str/replace #"\"" ""))]
Expand Down
6 changes: 4 additions & 2 deletions test/donut/dbxray_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
:child_records {:columns
{:id {:column-type :integer
:primary-key? true
:unique? true}
:unique? true
:unsigned? false}
:fk_id {:column-type :integer
:refers-to [:parent_records :id]}}
:refers-to [:parent_records :id]
:unsigned? false}}

:column-order
[:id :fk_id]}}
Expand Down

0 comments on commit d5d411a

Please # to comment.