Skip to content

Commit

Permalink
fixes #530 by supporting :using-gin in :create-index
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Jun 13, 2024
1 parent bab4ce4 commit 2c6bf85
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions doc/clause-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
4 changes: 4 additions & 0 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
7 changes: 6 additions & 1 deletion test/honey/sql/helpers_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 2c6bf85

Please sign in to comment.