address #415 by supporting multi-column add/alter/modify
This commit is contained in:
parent
5d7a3faea5
commit
061288f1c0
3 changed files with 21 additions and 6 deletions
|
|
@ -959,10 +959,14 @@
|
||||||
(str/join ", " (map #'format-single-column xs))
|
(str/join ", " (map #'format-single-column xs))
|
||||||
")")])
|
")")])
|
||||||
|
|
||||||
(defn- format-add-item [k spec]
|
(defn- format-add-single-item [k spec]
|
||||||
(if (contains? #{:if-not-exists 'if-not-exists} (last spec))
|
(if (contains? #{:if-not-exists 'if-not-exists} (last spec))
|
||||||
[(str (sql-kw k) " " (sql-kw :if-not-exists) " " (format-single-column (butlast spec)))]
|
(str (sql-kw k) " " (sql-kw :if-not-exists) " " (format-single-column (butlast spec)))
|
||||||
[(str (sql-kw k) " " (format-single-column spec))]))
|
(str (sql-kw k) " " (format-single-column spec))))
|
||||||
|
|
||||||
|
(defn- format-add-item [k spec]
|
||||||
|
(let [items (if (and (sequential? spec) (sequential? (first spec))) spec [spec])]
|
||||||
|
[(str/join ", " (for [item items] (format-add-single-item k item)))]))
|
||||||
|
|
||||||
(defn- format-rename-item [k [x y]]
|
(defn- format-rename-item [k [x y]]
|
||||||
[(str (sql-kw k) " " (format-entity x) " TO " (format-entity y))])
|
[(str (sql-kw k) " " (format-entity x) " TO " (format-entity y))])
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,12 @@
|
||||||
(helper-merge data k args))
|
(helper-merge data k args))
|
||||||
(helper-merge {} k args)))
|
(helper-merge {} k args)))
|
||||||
|
|
||||||
|
(defn- generic-grouped [k args]
|
||||||
|
(if (map? (first args))
|
||||||
|
(let [[data & args] args]
|
||||||
|
(helper-merge data k [args]))
|
||||||
|
(helper-merge {} k [args])))
|
||||||
|
|
||||||
(defn- generic-1 [k [data arg]]
|
(defn- generic-1 [k [data arg]]
|
||||||
(if (map? data)
|
(if (map? data)
|
||||||
(assoc data k arg)
|
(assoc data k arg)
|
||||||
|
|
@ -169,7 +175,7 @@
|
||||||
|
|
||||||
(add-column :name [:varchar 32] [:not nil])"
|
(add-column :name [:varchar 32] [:not nil])"
|
||||||
[& col-elems]
|
[& col-elems]
|
||||||
(generic :add-column col-elems))
|
(generic-grouped :add-column col-elems))
|
||||||
|
|
||||||
(defn drop-column
|
(defn drop-column
|
||||||
"Takes one or more column names (use with `alter-table`).
|
"Takes one or more column names (use with `alter-table`).
|
||||||
|
|
@ -188,7 +194,7 @@
|
||||||
|
|
||||||
(alter-column :name [:varchar 64] [:not nil])"
|
(alter-column :name [:varchar 64] [:not nil])"
|
||||||
[& col-elems]
|
[& col-elems]
|
||||||
(generic :alter-column col-elems))
|
(generic-grouped :alter-column col-elems))
|
||||||
|
|
||||||
(defn modify-column
|
(defn modify-column
|
||||||
"Like add-column, accepts any number of SQL elements
|
"Like add-column, accepts any number of SQL elements
|
||||||
|
|
@ -199,7 +205,7 @@
|
||||||
MySQL-specific, deprecated. Use `alter-column` and
|
MySQL-specific, deprecated. Use `alter-column` and
|
||||||
specify the MySQL dialect to get `MODIFY COLUMN`."
|
specify the MySQL dialect to get `MODIFY COLUMN`."
|
||||||
[& col-elems]
|
[& col-elems]
|
||||||
(generic :modify-column col-elems))
|
(generic-grouped :modify-column col-elems))
|
||||||
|
|
||||||
(defn rename-column
|
(defn rename-column
|
||||||
"Accepts two column names: the original name and the
|
"Accepts two column names: the original name and the
|
||||||
|
|
|
||||||
|
|
@ -631,6 +631,11 @@
|
||||||
(is (= (sql/format (-> (alter-table :fruit)
|
(is (= (sql/format (-> (alter-table :fruit)
|
||||||
(add-column :id :int [:not nil])))
|
(add-column :id :int [:not nil])))
|
||||||
["ALTER TABLE fruit ADD COLUMN id INT NOT NULL"]))
|
["ALTER TABLE fruit ADD COLUMN id INT NOT NULL"]))
|
||||||
|
(is (= (sql/format (-> (alter-table :fruit)
|
||||||
|
(add-column :id :int [:not nil])
|
||||||
|
(add-column :a1 :int nil)
|
||||||
|
(add-column :be :text [:not nil])))
|
||||||
|
["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN a1 INT NULL, ADD COLUMN be TEXT NOT NULL"]))
|
||||||
(is (= (sql/format (alter-table :fruit
|
(is (= (sql/format (alter-table :fruit
|
||||||
(add-column :id :int [:not nil])
|
(add-column :id :int [:not nil])
|
||||||
(drop-column :ident)
|
(drop-column :ident)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue