-
-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some sort of support for CREATE INDEX #348
Comments
MySQL https://dev.mysql.com/doc/refman/5.7/en/create-index.html -- yet more differences. |
Since there is already |
PostgreSQL has But if I were to implement Postgres-specific opt-in support similarly to how |
Sure, if you want to write up a PR for a PG-specific I'd prefer something generic enough to be part of the HoneySQL core and to be flexible enough for MySQL and SQL Server but I'm not likely to use it so that's not a requirement (I don't use HoneySQL for any DDL because it's just too irregular). |
For my use case, this would be enough: (ns honey.sql.pg-ddl
(:require [honey.sql :as sql]))
(sql/register-clause! :create-index
(fn [_ x] (sql/format-create :create :index x nil))
:raw)
(sql/register-clause! :create-unique-index
(fn [_ x] (sql/format-create :create :unique-index x nil))
:raw)
(sql/register-clause! :on
(fn [_ [table column]]
[(str "ON " (sql/format-entity table)
" (" (sql/format-entity column) ")")])
nil) which works like this: (ns honey.sql.pg-ddl-test
(:require [clojure.test :refer [deftest is testing]]
[honey.sql :as sql]
[honey.sql.pg-ddl]))
(deftest pg-ddl-tests
(testing "create index"
(is (= ["CREATE INDEX my_column_idx ON my_table (my_column)"]
(sql/format {:create-index :my-column-idx
:on [:my-table :my-column]})))
(testing "if not exists"
(is (= ["CREATE INDEX IF NOT EXISTS my_column_idx ON my_table (my_column)"]
(sql/format {:create-index [:my-column-idx :if-not-exists]
:on [:my-table :my-column]})))))
(testing "create unique index"
(is (= ["CREATE UNIQUE INDEX my_column_idx ON my_table (my_column)"]
(sql/format {:create-unique-index :my-column-idx
:on [:my-table :my-column]}))))) As long as you don't use Is something like this worth including, if I add docs and polish and make a PR? Does adding the |
I wouldn't want to add {:create-index [:my-column-idx [:my-table :my-column]]}
{:create-index [[:my-column-idx :if-not-exists] [:my-table :my-column]]} I would prefer not to introduce a separate {:create-index [[:unique :my-column-idx] [:my-table :my-column]]}
{:create-index [[:unique :my-column-idx :if-not-exists] [:my-table :my-column]]} So the single new clause formatter would destructure the args into If this satisfies your use case, I'm happy to add that to core and document it. Then I'll deal with any additional cases as they come up later 🙂 |
Yeah, that looks good. I'll add support for multiple columns and expressions instead of columns (PostgreSQL specific) and make a PR of that. |
There's no standard for this aspect of DDL it seems, but here's:
They're different (of course) but have some common elements. It's not clear what HoneySQL syntax should be for this because there are some words that come between
CREATE
andINDEX
, some that come betweenINDEX
and the name (which is optional in PG but not in MS! Argh!) andWITH
/WHERE
are in different orders between PG and MS too. Nasty.The text was updated successfully, but these errors were encountered: