Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add uuid and enum support to malli schema generator #1

Merged
1 commit merged into from
Apr 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/donut/dbxray/generate/malli.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns donut.dbxray.generate.malli
(:require
[clojure.string :as str]
[camel-snake-kebab.core :as csk]
[inflections.core :as inflections]))

Expand All @@ -9,7 +10,8 @@
:clob 'string?
:text 'string?
:varchar 'string?
:timestamp 'inst?})
:timestamp 'inst?
:uuid 'uuid?})
Copy link
Author

@ghost ghost Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one-liner for uuid support


(defn- table-spec-name
[table-name]
Expand All @@ -22,28 +24,36 @@
(keyword (name table-name) (name column-name)))

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

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

(and (= :integer column-type) primary-key?)
(:integer-pk column-types)
(cond
refers-to
(last (column-spec xray (first refers-to) (second refers-to)))

:else
(column-type column-types [:TODO/column-type-not-recognized column-type]))]))
(and (= :integer column-type) primary-key?)
(:integer-pk column-types)

;; :enum type definition
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main change for enum support:

I'm assuming here that types will always be identifyable by the pattern [schema].[name] which might not be 100% accurate? I think it will for for our case in PSQL but I'm not sure about other databases. Not sure if we can really upstream this later.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this doesn't seem like the best way to identify enums, but I currently don't have any better ideas either. Maybe add a comment here to make it clear that we are saying any column type with a . is an enum.

(str/includes? column-type ".")
(let [column (-> column-type (name) (str/replace #"\"" ""))]
(get enums column))

:else
(column-type column-types [:TODO/column-type-not-recognized column-type]))])))

(defn generate
[{:keys [tables table-order]}]
[{:keys [enums tables table-order]}]
(->> table-order
(mapv (fn [table-name]
(let [{:keys [column-order]} (table-name tables)]
(list 'def
(table-spec-name table-name)
(->> column-order
(map #(column-spec tables table-name %))
(map #(column-spec enums tables table-name %))
(into [:map]))))))))