diff --git a/CHANGELOG.md b/CHANGELOG.md index 35b0fbe..f4c8605 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ * 2.1.next in progress * Fix #371 by treating the operand of `NOT` as a nested expression (so it is parenthesized unless it is a simple value). * Fix #370 by **always** parenthesizing the operand of `:nest`. + * Fix #354 by supporting `DROP COLUMN IF EXISTS` / `ADD COLUMN IF NOT EXISTS`. + * Update `build-clj` to v0.5.5. * 2.1.818 -- 2021-10-04 * Fix #367 by supporting parameters in subexpressions around `IS NULL` / `IS NOT NULL` tests. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index fa07044..16fa67e 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -43,9 +43,15 @@ If a single table name is provided, a single column user=> (sql/format {:alter-table :fruit :add-column [:id :int [:not nil]]}) ["ALTER TABLE fruit ADD COLUMN id INT NOT NULL"] +user=> (sql/format {:alter-table :fruit + :add-column [:id :int [:not nil] :if-not-exists]}) +["ALTER TABLE fruit ADD COLUMN IF NOT EXISTS id INT NOT NULL"] user=> (sql/format {:alter-table :fruit :drop-column :ident}) ["ALTER TABLE fruit DROP COLUMN ident"] +user=> (sql/format {:alter-table :fruit + :drop-column [:if-exists :ident]}) +["ALTER TABLE fruit DROP COLUMN IF EXISTS ident"] user=> (sql/format {:alter-table :fruit :modify-column [:id :int :unsigned nil]}) ["ALTER TABLE fruit MODIFY COLUMN id INT UNSIGNED NULL"] @@ -63,6 +69,16 @@ user=> (sql/format {:alter-table [:fruit {:add-column [:id :int [:not nil]]} {:drop-column :ident}]}) ["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, DROP COLUMN ident"] +user=> (sql/format {:alter-table [:fruit + {:add-column [:id :int [:not nil]]} + {:add-column [:name [:varchar 32]]} + {:drop-column :ident} + {:modify-column [:appearance :text]}]}) +["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN name VARCHAR(32), DROP COLUMN ident, MODIFY COLUMN appearance TEXT"] +user=> (sql/format {:alter-table [:fruit + {:add-column [:id :int [:not nil] :if-not-exists]} + {:drop-column [:if-exists :ident]}]}) +["ALTER TABLE fruit ADD COLUMN IF NOT EXISTS id INT NOT NULL, DROP COLUMN IF EXISTS ident"] ``` As can be seen above, `:add-column` and `:modify-column` diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index b97a295..d347387 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -813,7 +813,9 @@ ")")]) (defn- format-add-item [k spec] - [(str (sql-kw k) " " (format-single-column 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))])) (defn- format-rename-item [k [x y]] [(str (sql-kw k) " " (format-entity x) " TO " (format-entity y))]) @@ -849,7 +851,7 @@ and removed." (atom {:alter-table #'format-alter-table :add-column #'format-add-item - :drop-column #'format-selector + :drop-column #'format-drop-items :modify-column #'format-add-item :rename-column #'format-rename-item ;; so :add-index works with both [:index] and [:unique]