From 2c6bf85f7ffddc2a5405e7a53b33d070af1b2622 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 12 Jun 2024 22:53:40 -0700 Subject: [PATCH] fixes #530 by supporting :using-gin in :create-index Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + doc/clause-reference.md | 8 ++++++++ src/honey/sql.cljc | 4 ++++ test/honey/sql/helpers_test.cljc | 7 ++++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3444a1a..6a84929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * 2.6.next in progress * Address [#531](https://github.com/seancorfield/honeysql/issues/531) and [#527](https://github.com/seancorfield/honeysql/issues/527) by adding tests and more documentation for `:composite`; fix bug in `set-dialect!` where clause order is not restored. + * Address [#530](https://github.com/seancorfield/honeysql/issues/530) by adding support for `:using-gin` to `:create-index`. * Address [#529](https://github.com/seancorfield/honeysql/issues/529) by fixing `:join` special syntax to support aliases and to handle expressions the same way `select` / `from` etc handle them (extra `[...]` nesting). * Add example of mixed `DO UPDATE SET` with `EXCLUDED` and regular SQL expressions. * Improve exception message when un-`lift`-ed JSON expressions are used in the DSL. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 49ad7c0..00dbcf5 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -141,6 +141,14 @@ user=> (sql/format (h/create-index [:unique :another-idx :if-not-exists] [:fruit ["CREATE UNIQUE INDEX IF NOT EXISTS another_idx ON fruit (color, LOWER(appearance))"] ``` +`USING GIN` index creation is also possible using the keyword `:using-gin` after +the table name (or the symbol `using-gin`): + +```clojure +user=> (sql/format {:create-index [:my-idx [:fruit :using-gin :appearance]]}) +["CREATE INDEX my_idx ON fruit USING GIN (appearance)"] +``` + ### rename-table Used with `:alter-table`, diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index e0288c2..f5ec8d3 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1267,10 +1267,14 @@ (defn- format-create-index [k clauses] (let [[index-spec [table & exprs]] clauses [pre entity ine & more] (destructure-ddl-item index-spec (str (sql-kw k) " options")) + [using & exprs] (if (= :using-gin (first exprs)) + exprs + (cons nil exprs)) [sqls params] (format-expr-list exprs)] (into [(str/join " " (remove empty? (-> ["CREATE" pre "INDEX" ine entity "ON" (format-entity table) + (when using (sql-kw using)) (str "(" (str/join ", " sqls) ")")] (into more))))] params))) diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index b48d153..932be21 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -980,7 +980,12 @@ (is (= ["CREATE UNIQUE INDEX IF NOT EXISTS my_column_idx ON my_table (my_column)"] (sql/format (create-index [:unique :my-column-idx :if-not-exists] [:my-table :my-column])))) (is (= ["CREATE INDEX my_column_idx ON my_table (LOWER(my_column))"] - (sql/format (create-index :my-column-idx [:my-table :%lower.my-column])))))) + (sql/format (create-index :my-column-idx [:my-table :%lower.my-column]))))) + (testing "PostgreSQL extensions (USING GIN)" + (is (= ["CREATE INDEX my_column_idx ON my_table USING GIN (my_column)"] + (sql/format {:create-index [:my-column-idx [:my-table :using-gin :my-column]]}))) + (is (= ["CREATE INDEX my_column_idx ON my_table USING GIN (my_column)"] + (sql/format (create-index :my-column-idx [:my-table :using-gin :my-column])))))) (deftest join-with-alias (is (= ["SELECT * FROM foo LEFT JOIN (populatons AS pm INNER JOIN customers AS pc ON (pm.id = pc.id) AND (pm.other_id = pc.other_id)) ON foo.fk_id = pm.id"]