Fixes #115 by providing do-commands example

This commit is contained in:
Sean Corfield 2020-06-07 10:01:12 -07:00
parent cfd403301f
commit 509e065fbf
3 changed files with 26 additions and 2 deletions

View file

@ -6,6 +6,7 @@ Changes made on master since 1.0.462:
* Add tests for `"jtds"` database driver (against MS SQL Server), making it officially supported. * Add tests for `"jtds"` database driver (against MS SQL Server), making it officially supported.
* Switch from OpenTable Embedded PostgreSQL to Zonky's version, so that testing can move forward from PostgreSQL 10.11 to 12.2.0. * Switch from OpenTable Embedded PostgreSQL to Zonky's version, so that testing can move forward from PostgreSQL 10.11 to 12.2.0.
* Address #119 by clarifying realization actions in the docstrings for `row-number`, `column-names`, and `metadata`. * Address #119 by clarifying realization actions in the docstrings for `row-number`, `column-names`, and `metadata`.
* Address #115 by adding equivalent of `db-do-commands` in the `clojure.java.jdbc` migration guide.
* Add log4j2 as a test dependency so that I have better control over logging (which makes debugging easier!). * Add log4j2 as a test dependency so that I have better control over logging (which makes debugging easier!).
## Stable Builds ## Stable Builds

View file

@ -55,6 +55,18 @@ The `next.jdbc.sql` namespace contains several functions with similarities to `c
* `update!` -- similar to `clojure.java.jdbc/update!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector), * `update!` -- similar to `clojure.java.jdbc/update!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector),
* `delete!` -- similar to `clojure.java.jdbc/delete!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector). * `delete!` -- similar to `clojure.java.jdbc/delete!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector).
If you were using `db-do-commands` in `clojure.java.jdbc` to execute DDL, the following is the equivalent in `next.jdbc`:
```clojure
(defn do-commands [connectable commands]
(if (instance? java.sql.Connection connectable)
(with-open [stmt (next.jdbc.prepare/statement connectable)]
(run! #(.addBatch stmt %) commands)
(into [] (.executeBatch stmt)))
(with-open [conn (next.jdbc/get-connection connectable)]
(do-commands conn commands))))
```
### `:identifiers` and `:qualifier` ### `:identifiers` and `:qualifier`
If you are using `:identifiers`, you will need to change to the appropriate `:builder-fn` option with one of `next.jdbc.result-set`'s `as-*` functions. If you are using `:identifiers`, you will need to change to the appropriate `:builder-fn` option with one of `next.jdbc.result-set`'s `as-*` functions.

View file

@ -4,6 +4,7 @@
"Multi-database testing fixtures." "Multi-database testing fixtures."
(:require [clojure.string :as str] (:require [clojure.string :as str]
[next.jdbc :as jdbc] [next.jdbc :as jdbc]
[next.jdbc.prepare :as prep]
[next.jdbc.sql :as sql]) [next.jdbc.sql :as sql])
(:import (io.zonky.test.db.postgres.embedded EmbeddedPostgres))) (:import (io.zonky.test.db.postgres.embedded EmbeddedPostgres)))
@ -92,6 +93,16 @@
[] []
@test-datasource) @test-datasource)
(defn- do-commands
"Example from migration docs: this serves as a test for it."
[connectable commands]
(if (instance? java.sql.Connection connectable)
(with-open [stmt (prep/statement connectable)]
(run! #(.addBatch stmt %) commands)
(into [] (.executeBatch stmt)))
(with-open [conn (jdbc/get-connection connectable)]
(do-commands conn commands))))
(defn with-test-db (defn with-test-db
"Given a test function (or suite), run it in the context of an in-memory "Given a test function (or suite), run it in the context of an in-memory
H2 database set up with a simple fruit table containing four rows of data. H2 database set up with a simple fruit table containing four rows of data.
@ -122,9 +133,9 @@
"AUTO_INCREMENT PRIMARY KEY")] "AUTO_INCREMENT PRIMARY KEY")]
(with-open [con (jdbc/get-connection (ds))] (with-open [con (jdbc/get-connection (ds))]
(try (try
(jdbc/execute-one! con [(str "DROP TABLE " fruit)]) (do-commands con [(str "DROP TABLE " fruit)])
(catch Exception _)) (catch Exception _))
(jdbc/execute-one! con [(str " (do-commands con [(str "
CREATE TABLE " fruit " ( CREATE TABLE " fruit " (
ID INTEGER " auto-inc-pk ", ID INTEGER " auto-inc-pk ",
NAME VARCHAR(32), NAME VARCHAR(32),