Shantanu Kumar  committed 8242958

let user specify ID Column name via :idcol optional argument in `create-table-withid`

  • Participants
  • Parent commits 4f4fb35
  • Branches default

Comments (0)

Files changed (2)

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

 (defn ^CreateTableChange create-table
   "Return a Change instance that creates a table (CreateTableChange).
+  NOTE! : User should call `clj-liquibase/create-table-changeset` instead.
   See also:
+    clj-liquibase/create-table-changeset"
   [table-name ^List columns
    & {:keys [schema-name schema ; String/Keyword - subject to db-iden
              table-space tspace ; String/Keyword - subject to db-iden
 (defn ^CreateTableChange create-table-withid
   "Same as `create-table`, but includes an additional auto-generated primary
-  key column. The primary key column is named <table-name>_id - e.g. if the
+  key column. The primary key column is named <table-name>_id unless overriden
+  with optional argument `:idcol` and ID column-name as the value. E.g. if the
   table name is :sample or \"sample\", then primary key will be \"sample_id\".
+  NOTE! : User should call `clj-liquibase/create-table-withid-changeset` instead.
   See also:
-    create-table"
+    create-table
+    clj-liquibase/create-table-withid-changeset"
   [table-name columns & args]
-  (let [idcol [(str (sp/db-iden table-name) "_id") :BIGINT :null false :pk true
-                                                           :autoinc true]]
-    (apply create-table table-name (cons idcol columns) args)))
+  (let [{:keys [idcol] :as opt} args
+        idcol-name (if idcol
+                     (sp/db-iden idcol)
+                     (str (sp/db-iden table-name) "_id"))
+        idcol-spec [idcol-name :BIGINT :null false :pk true :autoinc true]
+        ct-varargs (reduce into [] (dissoc opt :idcol))]
+    (apply create-table table-name (cons idcol-spec columns) ct-varargs)))
 ;; Rename Table

File src/test/clj/org/bituf/clj_liquibase/test_change.clj

 (defn test-change
   "Assert Change instance by running 'pch' partial function through arg colls.
   Each arg coll is a list where the first element is test description (string)."
-  [^Class ch-class pch & argcolls]
+  [^Class ch-class pch argcoll & argcolls]
   (let [ch? #(instance? ch-class %)]
-    (doseq [each argcolls]
+    (doseq [each (into [argcoll] argcolls)]
       (let [desc (first each)
             args (rest each)]
         (is (ch? (apply pch args)) desc)))))
          :tspace  :go-air  ; string/Keyword - s.t. clj-to-dbident
          :remarks "Notnow" ; string
-         ]))))
+         ])))
+  (testing "create-table-withid"
+           (test-change CreateTableChange
+                        (partial change/create-table-withid :emp
+                                 [[:c1 :int] [:c2 [:varchar 30]]])
+                        min-args)
+           (test-change CreateTableChange
+                        (partial change/create-table-withid :emp
+                                 [[:c1 :int] [:c2 [:varchar 30]]])
+                        ["With :idcol" :idcol "id"])))
 (deftest test-rename-table