forked from donut-party/dbxray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a generator for datapotato schema
- Loading branch information
Showing
3 changed files
with
85 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |