address #415 by supporting multi-column add/alter/modify

This commit is contained in:
Sean Corfield 2022-09-12 18:17:26 -07:00
parent 5d7a3faea5
commit 061288f1c0
3 changed files with 21 additions and 6 deletions

View file

@ -959,10 +959,14 @@
(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))
[(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) " " (sql-kw :if-not-exists) " " (format-single-column (butlast 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]]
[(str (sql-kw k) " " (format-entity x) " TO " (format-entity y))])

View file

@ -138,6 +138,12 @@
(helper-merge data 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]]
(if (map? data)
(assoc data k arg)
@ -169,7 +175,7 @@
(add-column :name [:varchar 32] [:not nil])"
[& col-elems]
(generic :add-column col-elems))
(generic-grouped :add-column col-elems))
(defn drop-column
"Takes one or more column names (use with `alter-table`).
@ -188,7 +194,7 @@
(alter-column :name [:varchar 64] [:not nil])"
[& col-elems]
(generic :alter-column col-elems))
(generic-grouped :alter-column col-elems))
(defn modify-column
"Like add-column, accepts any number of SQL elements
@ -199,7 +205,7 @@
MySQL-specific, deprecated. Use `alter-column` and
specify the MySQL dialect to get `MODIFY COLUMN`."
[& col-elems]
(generic :modify-column col-elems))
(generic-grouped :modify-column col-elems))
(defn rename-column
"Accepts two column names: the original name and the

View file

@ -631,6 +631,11 @@
(is (= (sql/format (-> (alter-table :fruit)
(add-column :id :int [:not nil])))
["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
(add-column :id :int [:not nil])
(drop-column :ident)