Skip to content

Commit

Permalink
Generate datapotato schema (#1)
Browse files Browse the repository at this point in the history
add a generator for datapotato schema
  • Loading branch information
mpoffald authored Nov 3, 2022
1 parent 59ee170 commit 36e4e37
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 28 deletions.
42 changes: 42 additions & 0 deletions src/donut/dbxray/generate/datapotato.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns donut.dbxray.generate.datapotato
(:require
[clojure.string :as string]
[donut.dbxray.generate :as ddg]))

(defn- table-prefix
[table-name]
(let [words (string/split (name table-name) #"_")
initials (map first words)]
{:prefix (keyword (apply str initials))}))

(defn- full-column-name
[table-name col-name]
(let [table (name table-name)]
(keyword (str table "/" (name col-name)))))


(defn- table-relations
[table-name columns]
(some->> (not-empty
(into {}
(keep (fn [[column-name column-data]]
(when-let [refers-to (:refers-to column-data)]
(let [relations-key (full-column-name table-name column-name)
[ref-table ref-col] refers-to]
{relations-key [ref-table (full-column-name ref-table ref-col)]})))
columns)))
(assoc {} :relations)))

(defn- table-potato-schema
[table-name table-data]
(let [{:keys [columns]} table-data
prefix (gen-table-prefix table-name)]
{table-name (merge prefix
(table-relations table-name columns))}))

(defn generate
[xray]
(reduce (fn [generated [table-name table-data]]
(merge generated (table-potato-schema table-name table-data)))
(omap/ordered-map)
xray))
54 changes: 26 additions & 28 deletions test/donut/dbxray/fixtures.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@
(:require [flatland.ordered.map :as omap]))

(def todo-list-xray
{:users {:columns (omap/ordered-map
:id {:column-type :integer
:primary-key? true
:unique? true}
:username {:column-type :varchar
:unique? true})}
:todo_lists {:columns (omap/ordered-map
:id {:column-type :integer
:primary-key? true
:unique? true}
:created_by_id {:column-type :integer
:nullable? true
:refers-to [:users :id]})}
:todos {:columns (omap/ordered-map
:id {:column-type :integer
:primary-key? true
:unique? true}
:todo_list_id {:column-type :integer
:nullable? true
:refers-to [:todo_lists :id]}
:todo_title {:column-type :varchar}
:notes {:column-type :text
:nullable? true}
:created_by_id {:column-type :integer
:nullable? true
:refers-to [:users :id]}
:created_at {:column-type :timestamp
:nullable? true})}})
(omap/ordered-map
:users {:columns {:id {:column-type :integer
:primary-key? true
:unique? true}
:username {:column-type :varchar
:unique? true}}}
:todo_lists {:columns {:id {:column-type :integer
:primary-key? true
:unique? true}
:created_by_id {:column-type :integer
:nullable? true
:refers-to [:users :id]}}}
:todos {:columns {:id {:column-type :integer
:primary-key? true
:unique? true}
:todo_list_id {:column-type :integer
:nullable? true
:refers-to [:todo_lists :id]}
:todo_title {:column-type :varchar}
:notes {:column-type :text
:nullable? true}
:created_by_id {:column-type :integer
:nullable? true
:refers-to [:users :id]}
:created_at {:column-type :timestamp
:nullable? true}}}))
17 changes: 17 additions & 0 deletions test/donut/dbxray/generate/datapotato_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns donut.dbxray.generate.datapotato-test
(:require
[clojure.test :refer [deftest is]]
[donut.dbxray.generate.datapotato :as ddgd]
[donut.dbxray.fixtures :as ddf]
[flatland.ordered.map :as omap]))

(deftest generates-datapotato-schema
(let [datapotato-schem (omap/ordered-map)])
(is (= (into []
(omap/ordered-map :users {:prefix :u}
:todo_lists {:prefix :tl
:relations {:todo_lists/created_by_id [:users :users/id]}}
:todos {:prefix :t
:relations {:todos/todo_list_id [:todo_lists :todo_lists/id]
:todos/created_by_id [:users :users/id]}}))
(into [] (ddgd/generate ddf/todo-list-xray)))))

0 comments on commit 36e4e37

Please # to comment.