sql

Ernesto Garcia 2023-08-16T10:59:21.035489Z

Would be nice to include postgres "CREATE INDEX" in HoneySQL. I'm for now running with this for the simplest case:

(sql/register-clause! ::create-index-on
  (fn [clause x]
    [(str (sql/sql-kw clause) " " (sql/format-entity x))])
  :columns)
and using it like
{::rdb/create-index-on :bank-transaction
 :columns [:bank-transaction/case-id
           :bank-transaction/date]}

igrishaev 2023-08-16T11:05:01.842699Z

I did this once before, let me find the code...

igrishaev 2023-08-16T11:06:16.131649Z

(sql/register-clause!
 :create-index
 (fn [_ {idx-name :name
         :keys [unique?
                if-not-exists?
                on-table
                on-field
                using]}]

   [(clojure.string/join
     " "
     ["CREATE"
      (when unique? "UNIQUE")
      "INDEX"
      (when if-not-exists? "IF NOT EXISTS")
      (name idx-name)
      "ON"
      (name on-table)
      "(" (name on-field) ")"
      (when using "USING")
      (when using using)])])

(sql/format {:create-index
              {:if-not-exists? true
               :name "idx_user_lname"
               :on-table :users
               :on-field :lname}})

["CREATE INDEX IF NOT EXISTS idx_user_lname
  ON users ( lname )"]

igrishaev 2023-08-16T11:07:36.860349Z

A bit ugly but works, you're welcome to refactor it

1
Raghav 2023-08-16T15:02:55.130909Z

curious about the use case of writing ddl via honeysql. how do you use it?

Ernesto Garcia 2023-08-16T15:04:52.569749Z

I use it in the same way as SQL. I just introduced migratus for managing the versioning.

seancorfield 2023-08-16T16:49:44.994799Z

DDL is mostly non-ANSI Standard in practice so each statement tends to vary a lot between dialects. HoneySQL supports the most common "core" DDL -- so there's basic ALTER TABLE / ADD INDEX stuff -- but a lot of each database's DDL is very irregular in structure and hard to model in a data-driven DSL.

seancorfield 2023-08-16T16:52:13.722239Z

Feel free to create an issue on GitHub and I'll see about putting in basic CREATE INDEX (based on the machinery of alter/add I expect).