This commit is contained in:
Sean Corfield 2023-01-14 14:58:13 -08:00
parent 83ad5cff74
commit 6b015400ed
3 changed files with 60 additions and 1 deletions

View file

@ -2,6 +2,7 @@
* 2.4.next in progress
* Address [#447](https://github.com/seancorfield/honeysql/issues/447) by updating GitHub Actions and dependencies.
* Address [#445](https://github.com/seancorfield/honeysql/issues/445) and [#453](https://github.com/seancorfield/honeysql/issues/453) by adding key/constraint examples to `CREATE TABLE` docs.
* 2.4.962 -- 2022-12-17
* Fix `set-options!` (only `:checking` worked in 2.4.947).

View file

@ -168,7 +168,7 @@ was specified so nothing is parameterized. In addition,
everything except the first element of a column description
will be uppercased (mostly to give the appearance of separating
the column name from the SQL keywords) -- except for keywords
that with `'` which will be transcribed into the SQL exactly
that start with `'` which will be transcribed into the SQL exactly
as-is, with no case or character conversion at all. This
"escape hatch" is intended to allow for SQL dialects that are
case sensitive and/or have other unusual syntax constraints.
@ -178,6 +178,46 @@ in the example above, that allow things like `CHECK` for a
constraint, `FOREIGN KEY` (with a column name), `REFERENCES`
(with a pair of column names). See [Column Descriptors in Special Syntax](special-syntax.md#column-descriptors) for more details.
For example:
```clojure
user=> (-> {:create-table :foo
:with-columns
[[:a :int]
[:b :int]
[[:primary-key :a :b]]]}
(sql/format))
["CREATE TABLE foo (a INT, b INT, PRIMARY KEY(a, b))"]
```
or:
```clojure
user=> (-> {:create-table [:bar]
:with-columns
[[:a :integer]
[:b :integer]
[[:constraint :foo_natural_key] :unique [:composite :a :b]]]}
(sql/format))
["CREATE TABLE bar (a INTEGER, b INTEGER, CONSTRAINT foo_natural_key UNIQUE (a, b))"]
```
or a mix of column constraints and table constraints:
```clojure
user=> (-> '{create-table quux
with-columns
((a integer (constraint a_pos) (check (> a 0)))
(b integer)
((constraint a_bigger) (check (< b a))))}
(sql/format {:pretty true}))
["
CREATE TABLE quux
(a INTEGER CONSTRAINT a_pos CHECK(a > 0), b INTEGER, CONSTRAINT a_bigger CHECK(b < a))
"]
```
## create-table-as
`:create-table-as` can accept a single table name or a sequence

View file

@ -405,3 +405,21 @@
(is (= ["DROP EXTENSION \"uuid-ossp\""]
(-> (drop-extension :uuid-ossp)
(sql/format {:quoted true}))))))
(deftest issue-453-constraint
(testing "standalone constraint"
(is (= ["CREATE TABLE bar (a INTEGER, b INTEGER, CONSTRAINT foo_natural_key UNIQUE (a, b))"]
(-> {:create-table [:bar]
:with-columns
[[:a :integer]
[:b :integer]
[[:constraint :foo_natural_key] :unique [:composite :a :b]]]}
(sql/format)))))
(testing "inline constraint"
(is (= ["CREATE TABLE foo (a INTEGER CONSTRAINT a_pos CHECK(a > 0), b INTEGER, CONSTRAINT a_bigger CHECK(b < a))"]
(-> '{create-table foo
with-columns
((a integer (constraint a_pos) (check (> a 0)))
(b integer)
((constraint a_bigger) (check (< b a))))}
(sql/format))))))