From f0ada59fff90460d34850c7de518ded451936132 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 29 Jul 2022 15:40:45 -0700 Subject: [PATCH] fix #406 --- CHANGELOG.md | 1 + doc/clause-reference.md | 14 ++++++++------ doc/postgresql.md | 8 ++++---- doc/special-syntax.md | 2 +- src/honey/sql.cljc | 7 ++++++- src/honey/sql/helpers.cljc | 13 ++++++++++++- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20fd96f..a399c66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Address [#414](https://github.com/seancorfield/honeysql/issues/414) by providing an example of `ORDER BY` with a `CASE` expression. * Address [#412](https://github.com/seancorfield/honeysql/issues/412) by documenting options in a separate page and reorganizing the ToC structure. * Address [#409](https://github.com/seancorfield/honeysql/issues/409) by making docstring check for public helpers conditional. + * Fix [#406](https://github.com/seancorfield/honeysql/issues/406) by adding `:alter-column` (which produces `MODIFY COLUMN` when the MySQL dialect is selected) and deprecating `:modify-column`. * Address [#401](https://github.com/seancorfield/honeysql/issues/401) by adding `register-dialect!` and `get-dialect`, and also making `add-clause-before`, `strop`, and `upper-case` public so that new dialects are easier to construct. * 2.2.891 -- 2022-04-23 diff --git a/doc/clause-reference.md b/doc/clause-reference.md index ab1d816..4b0385a 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -32,7 +32,7 @@ See [Column Descriptors in Special Syntax](special-syntax.md#column-descriptors) > Google BigQuery support: `[:bigquery/array :string]` as a column type produces `ARRAY` and `[:bigquery/struct col1-spec col2-spec]` as a column type produces `STRUCT` (where `colN-spec` is a vector specifying a named column). -## alter-table, add-column, drop-column, modify-column, rename-column +## alter-table, add-column, drop-column, alter-column, modify-column, rename-column `:alter-table` can accept either a single table name or a sequence that begins with a table name and is followed @@ -55,8 +55,8 @@ 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"] + :alter-column [:id :int :unsigned nil]}) +["ALTER TABLE fruit ALTER COLUMN id INT UNSIGNED NULL"] user=> (sql/format {:alter-table :fruit :rename-column [:look :appearance]}) ["ALTER TABLE fruit RENAME COLUMN look TO appearance"] @@ -75,20 +75,22 @@ 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"] + {:alter-column [:appearance :text]}]}) +["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN name VARCHAR(32), DROP COLUMN ident, ALTER 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` +As can be seen above, `:add-column` and `:alter-column` both accept a column description (as a sequence of simple expressions); `:drop-column` accepts a single column name, and `:rename-column` accepts a sequence with two column names: the "from" and the "to" names. +> Note: `:modify-column` is MySQL-specific and should be considered legacy and deprecated. `:alter-column` will produce `MODIFY COLUMN` when the MySQL dialect is selected. + ### add-index, drop-index Used with `:alter-table`, diff --git a/doc/postgresql.md b/doc/postgresql.md index 26b4469..d1dbd39 100644 --- a/doc/postgresql.md +++ b/doc/postgresql.md @@ -21,7 +21,7 @@ The code examples herein assume: insert-into values create-table with-columns create-view create-extension add-column alter-table add-index - modify-column rename-column rename-table + alter-column rename-column rename-table drop-table drop-column drop-index drop-extension upsert returning on-conflict on-constraint do-update-set do-nothing]]) @@ -361,11 +361,11 @@ user=> (-> (alter-table :fruit) (drop-column :skin) sql/format) ["ALTER TABLE fruit DROP COLUMN skin"] -;; alter table modify column: +;; alter table alter column: user=> (-> (alter-table :fruit) - (modify-column :name [:varchar 64] [:not nil]) + (alter-column :name [:varchar 64] [:not nil]) sql/format) -["ALTER TABLE fruit MODIFY COLUMN name VARCHAR(64) NOT NULL"] +["ALTER TABLE fruit ALTER COLUMN name VARCHAR(64) NOT NULL"] ;; alter table rename column: user=> (-> (alter-table :fruit) (rename-column :cost :price) diff --git a/doc/special-syntax.md b/doc/special-syntax.md index 3e14241..8179c59 100644 --- a/doc/special-syntax.md +++ b/doc/special-syntax.md @@ -4,7 +4,7 @@ This section lists the function-like expressions that HoneySQL supports out of the box which are formatted as special syntactic forms. -The first group are used for SQL expressions. The second (last group) are used primarily in column definitions (as part of `:with-columns` and `:add-column` / `:modify-column`). +The first group are used for SQL expressions. The second (last group) are used primarily in column definitions (as part of `:with-columns` and `:add-column` / `:alter-column`). ## array diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 79fc029..9914650 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -42,7 +42,8 @@ (def ^:private default-clause-order "The (default) order for known clauses. Can have items added and removed." [;; DDL comes first (these don't really have a precedence): - :alter-table :add-column :drop-column :modify-column :rename-column + :alter-table :add-column :drop-column + :alter-column :modify-column :rename-column :add-index :drop-index :rename-table :create-table :create-table-as :with-columns :create-view :create-materialized-view :create-extension @@ -943,6 +944,10 @@ (atom {:alter-table #'format-alter-table :add-column #'format-add-item :drop-column #'format-drop-items + :alter-column (fn [k spec] + (format-add-item + (if (mysql?) :modify-column k) + spec)) :modify-column #'format-add-item :rename-column #'format-rename-item ;; so :add-index works with both [:index] and [:unique] diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index 4bb98ab..a26e263 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -179,11 +179,22 @@ [& args] (generic-1 :drop-column args)) +(defn alter-column + "Like add-column, accepts any number of SQL elements + that describe the new column definition: + + (alter-column :name [:varchar 64] [:not nil])" + [& col-elems] + (generic :alter-column col-elems)) + (defn modify-column "Like add-column, accepts any number of SQL elements that describe the new column definition: - (modify-column :name [:varchar 64] [:not nil])" + (modify-column :name [:varchar 64] [:not nil]) + + MySQL-specific, deprecated. Use `alter-column` and + specify the MySQL dialect to get `MODIFY COLUMN`." [& col-elems] (generic :modify-column col-elems))