Skip to content

Commit 9a8cf11

Browse files
authored
Add create / drop extension (#55)
* Add create / drop extension * Add tests for create-extension / drop-extension * Add create-extension / drop-extension to README * Fix formatting, update drop-extension example
1 parent 6b1c479 commit 9a8cf11

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This library aims to extend the features of honeysql to support postgres specifi
2020
- [create table](#create-table)
2121
- [drop table](#drop-table)
2222
- [alter table](#alter-table)
23+
- [create extension](#create-extension)
24+
- [drop extension](#drop-extension)
2325
- [pattern matching](#pattern-matching)
2426
- [except](#except)
2527
- [SQL functions](#sql-functions)
@@ -143,6 +145,25 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level
143145
=> ["ALTER TABLE employees DROP COLUMN address"]
144146
```
145147

148+
### create-extension
149+
`create-extension` can be used to create extensions with a given keyword.
150+
```clj
151+
(-> (create-extension :uuid-ossp :if-not-exists? true)
152+
(sql/format :allow-dashed-names? true
153+
:quoting :ansi))
154+
155+
=> ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""]
156+
```
157+
158+
### drop-extension
159+
`drop-extension` is used to drop extensions.
160+
```clj
161+
(-> (drop-extension :uuid-ossp)
162+
(sql/format :allow-dashed-names? true
163+
:quoting :ansi))
164+
=> ["DROP EXTENSION \"uuid-ossp\""]
165+
```
166+
146167
### pattern matching
147168
The `ilike` and `not-ilike` operators can be used to query data using a pattern matching technique.
148169
- like

src/honeysql_postgres/format.cljc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
(def ^:private custom-additions
88
{:create-table 10
99
:drop-table 10
10+
:create-extension 10
11+
:drop-extension 10
1012
:alter-table 20
1113
:add-column 30
1214
:drop-column 40
@@ -223,3 +225,16 @@
223225

224226
(defmethod format-modifiers :distinct-on [[_ & fields]]
225227
(str "DISTINCT ON(" (sqlf/comma-join (map sqlf/to-sql fields)) ")"))
228+
229+
(defmethod sqlf/format-clause :drop-extension [[_ [extension-name]] _]
230+
(str "DROP EXTENSION "
231+
(-> extension-name
232+
util/get-first
233+
sqlf/to-sql)))
234+
235+
(defmethod format-clause :create-extension [[_ [ extension-name if-not-exists]] _]
236+
(str "CREATE EXTENSION "
237+
(when if-not-exists "IF NOT EXISTS ")
238+
(-> extension-name
239+
util/get-first
240+
sqlf/to-sql)))

src/honeysql_postgres/helpers.cljc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@
6767

6868
(defhelper insert-into-as [m fields]
6969
(assoc m :insert-into-as (sqlh/collify fields)))
70+
71+
(defhelper create-extension [m extension-name]
72+
(assoc m :create-extension (sqlh/collify extension-name)))
73+
74+
(defhelper drop-extension [m extension-name]
75+
(assoc m :drop-extension (sqlh/collify extension-name)))

test/honeysql_postgres/postgres_test.cljc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
alter-table rename-column drop-column
66
add-column partition-by insert-into-as
77
create-table rename-table drop-table
8-
window create-view over with-columns]]
8+
window create-view over with-columns
9+
create-extension drop-extension]]
910
[honeysql.helpers :as sqlh :refer [insert-into values where select columns
1011
from order-by update sset query-values
1112
modifiers]]
@@ -257,3 +258,22 @@
257258
(from :products)
258259
(modifiers :distinct-on :a :b)
259260
(sql/format :quoting :ansi))))))
261+
262+
(deftest create-extension-test
263+
(testing "create extension"
264+
(is (= ["CREATE EXTENSION \"uuid-ossp\""]
265+
(-> (create-extension :uuid-ossp)
266+
(sql/format :allow-dashed-names? true
267+
:quoting :ansi)))))
268+
(testing "create extension if not exists"
269+
(is (= ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""]
270+
(-> (create-extension :uuid-ossp :if-not-exists? true)
271+
(sql/format :allow-dashed-names? true
272+
:quoting :ansi))))))
273+
274+
(deftest drop-extension-test
275+
(testing "create extension"
276+
(is (= ["DROP EXTENSION \"uuid-ossp\""]
277+
(-> (drop-extension :uuid-ossp)
278+
(sql/format :allow-dashed-names? true
279+
:quoting :ansi))))))

0 commit comments

Comments
 (0)