Commits

Shantanu Kumar committed fda1a6e

implement `create-table-changeset` and `create-table-withid-changeset`

Comments (0)

Files changed (3)

src/main/clj/org/bituf/clj_liquibase.clj

     [org.bituf.clj-dbspec   :as sp]
     [org.bituf.clj-miscutil :as mu]
     [org.bituf.clj-liquibase.internal     :as in]
+    [org.bituf.clj-liquibase.change       :as ch]
     [org.bituf.clj-liquibase.precondition :as pc]
     [org.bituf.clj-liquibase.sql-visitor  :as vis]))
 
     c-set))
 
 
+(defn create-table-changeset-internal
+  "Common internal implementation for `create-table-changeset` and
+  `create-table-withid-changeset`."
+  [f ct-vkeys ^String id ^String author table-name ^List columns
+   & {:keys [dummy] :as opt}]
+  (let [map2vec  (partial reduce into [])
+        ct-vargs (map2vec (select-keys opt ct-vkeys))
+        ;; mc = make-changeset
+        mc-visit (vis/make-visitors
+                   :include (map (partial vis/for-dbms! :mysql)
+                                 (vis/make-visitors :append "engine=InnoDB")))
+        mc-vargs (map2vec (apply dissoc (merge {:visitors mc-visit} opt)
+                                 ct-vkeys))]
+    (apply make-changeset id author
+           [(apply f table-name columns ct-vargs)]
+           mc-vargs)))
+
+
+(defn create-table-changeset
+  "Convenience function to build a changeset for `create-table` change with
+  sensible defaults, e.g. create a table on MySQL with InnoDB storage engine.
+  Optional arguments are a union of those in `make-changeset` and
+  `change/create-table`.
+  See also:
+    make-changeset
+    change/create-table"
+  [^String id ^String author table-name ^List columns & args]
+  (apply create-table-changeset-internal
+         ch/create-table
+         [:schema-name :schema :table-space :tspace :remarks]
+         id author table-name columns args))
+
+
+(defn create-table-withid-changeset
+  "Convenience function to build a changeset for `create-table-withid` with
+  sensible defaults, e.g. create a table on MySQL with InnoDB storage engine.
+  Optional arguments are a union of those in `make-changeset` and
+  `change/create-table-withid`.
+  See also:
+    make-changeset
+    change/create-table-withid"
+  [^String id ^String author table-name ^List columns & args]
+  (apply create-table-changeset-internal
+         ch/create-table-withid
+         [:schema-name :schema :table-space :tspace :remarks :idcol]
+         id author table-name columns args))
+
+
 ;; ===== DatabaseChangeLog helpers =====
 
 

src/test/clj/org/bituf/test_clj_liquibase.clj

                            )) "Optional arguments with shortname"))))
 
 
+(deftest test-create-table-changeset
+  (let [args ["id=1" "author=shantanu" :sample-table
+              [[:first-name [:varchar 40] :null false]
+               [:last-name  [:varchar 40]]
+               [:date-born  :date]]
+              :filepath "dummy"]]
+    (doseq [[f n] [[lb/create-table-changeset        "create-table"]
+                   [lb/create-table-withid-changeset "create-table-withid"]]]
+      (is (lb/changeset? (apply f args))
+          (str n " with no optional arg"))
+      (is (lb/changeset? (apply f (into args [:schema "some"])))
+          (str n " with one optional arg for " n))
+      (is (lb/changeset? (apply f (into args [:always true :in-txn true])))
+          (str n " with 2 optional args for make-changeset")))
+    (is (lb/changeset? (apply lb/create-table-withid-changeset
+                              (into args [:always true :idcol :id])))
+        (format "%s with an optional arg for make-changeset and :idcol for %s"
+                "create-table-withid" "create-table-withid"))))
+
+
 ;; ===== DatabaseChangeLog =====
 
 
 (defn test-ns-hook []
   ;; ===== ChangeSet =====
   (test-make-changeset)
+  (test-create-table-changeset)
   ;; ===== DatabaseChangeLog =====
   (test-make-changelog)
   (test-defchangelog)

src/test/script/runtests.clj

   (:require org.bituf.clj-liquibase.test-internal)
   (:require org.bituf.clj-liquibase.test-change)
   (:require org.bituf.clj-liquibase.test-precondition)
+  (:require org.bituf.clj-liquibase.test-sql-visitor)
   (:require org.bituf.test-clj-liquibase)
   (:use clojure.test))
 
   'org.bituf.clj-liquibase.test-internal
   'org.bituf.clj-liquibase.test-change
   'org.bituf.clj-liquibase.test-precondition
+  'org.bituf.clj-liquibase.test-sql-visitor
   'org.bituf.test-clj-liquibase)