Skip to content

Commit

Permalink
breaking: update konserve to enable multitenancy and unbundle jdbc dr…
Browse files Browse the repository at this point in the history
…ivers from jar (#25)
  • Loading branch information
alekcz authored Oct 25, 2023
1 parent 884503f commit 5427039
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pom.xml.asc
.nrepl-port
.cpcache/
temp/
datahike.mv.db
*.mv.db
*.trace.db
.idea/
datahike-jdbc.iml
.clj-kondo/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ It is also possible to pass a configuration url via `:jdbcUrl` like it is mentio

Arguments not mentioned will be passed downstream to the corresponding jdbc-driver so every configuration option available should be working.

BREAKING CHANGE: datahike-jdbc versions after 0.1.45 no longer include actual JDBC drivers. Before you upgrade please make sure your application provides the necessary dependencies.

## Prerequisites
For this backend to work you need to choose a database that is supported by JDBC. Please have a
look at the docs for [clojure.java.jdbc](https://github.com/clojure/java.jdbc/). For the sake
Expand All @@ -51,6 +53,7 @@ On your local machine your setup prerequisites could look something like this:
2. A running instance of PostgreSQL that you can connect to.
3. [A JDK to run your Clojure on](https://clojure.org/guides/getting_started)
4. [Leiningen, Boot or Clojure CLI to run your code or a REPL](https://leiningen.org/#install)
5. The JDBC drivers you wish to use added as a dependency (since `0.2.x`)

We will stick with Leiningen for this manual. If you want to use MySQL or H2 for a try, please
have a look into the tests to see how to configure these backend stores.
Expand Down Expand Up @@ -83,6 +86,7 @@ keyword `:jdbc`. If you want to use other backends than JDBC please refer to the
:port 5432
:user "alice"
:password "foo"
:table "my_first_application" ;; defaults to konserve
:dbname "config-test"}})

;; Create a database at this place, by default configuration we have a strict
Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(def org "replikativ")
(def lib 'io.replikativ/datahike-jdbc)
(def current-commit (b/git-process {:git-args "rev-parse HEAD"}))
(def version (format "0.1.%s" (b/git-count-revs nil)))
(def version (format "0.2.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def jar-file (format "target/%s-%s.jar" (name lib) version))
Expand Down
12 changes: 9 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{:deps {org.clojure/clojure {:mvn/version "1.11.1" :scope "provided"}
io.replikativ/konserve-jdbc {:mvn/version "0.1.79"}}
io.replikativ/konserve-jdbc {:mvn/version "0.2.82"}
io.replikativ/datahike {:mvn/version "0.6.1552" :scope "provided"}}
:paths ["src"]
:aliases {:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.84.1335"}
io.replikativ/datahike {:mvn/version "0.6.1541"}}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.84.1335"}
com.h2database/h2 {:mvn/version "2.1.214"}
com.microsoft.sqlserver/mssql-jdbc {:mvn/version "9.4.1.jre11"}
mysql/mysql-connector-java {:mvn/version "8.0.25"}
org.apache.derby/derby {:mvn/version "10.16.1.1"}
org.postgresql/postgresql {:mvn/version "42.6.0"}
org.xerial/sqlite-jdbc {:mvn/version "3.41.2.2"}}
:main-opts ["-m" "kaocha.runner"]}

:format {:extra-deps {cljfmt/cljfmt {:mvn/version "0.9.2"}}
Expand Down
10 changes: 6 additions & 4 deletions src/datahike_jdbc/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
[clojure.spec.alpha :as s]))

(defmethod store-identity :jdbc [store-config]
(let [{:keys [jdbcUrl dbtype host port dbname]} store-config]
(let [{:keys [jdbcUrl dbtype host port dbname table]} store-config]
(if jdbcUrl
[:jdbc jdbcUrl]
[:jdbc dbtype host port dbname])))
[:jdbc jdbcUrl table]
[:jdbc dbtype host port dbname table])))

(defmethod empty-store :jdbc [store-config]
(k/connect-store store-config))
Expand Down Expand Up @@ -36,6 +36,7 @@
(s/def :datahike.store.jdbc/classname string?)
(s/def :datahike.store.jdbc/user string?)
(s/def :datahike.store.jdbc/password string?)
(s/def :datahike.store.jdbc/table string?)
(s/def ::jdbc (s/keys :req-un [:datahike.store.jdbc/backend]
:opt-un [:datahike.store.jdbc/dbtype
:datahike.store.jdbc/jdbcUrl
Expand All @@ -46,7 +47,8 @@
:datahike.store.jdbc/port
:datahike.store.jdbc/classname
:datahike.store.jdbc/user
:datahike.store.jdbc/password]))
:datahike.store.jdbc/password
:datahike.store.jdbc/table]))

(defmethod config-spec :jdbc [_] ::jdbc)

Expand Down
40 changes: 40 additions & 0 deletions test/datahike_jdbc/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,43 @@
(is (d/database-exists?))
(d/delete-database)
(is (not (d/database-exists?))))))

(deftest ^:integration test-table
(let [config {:store {:backend :jdbc
:dbtype "mysql"
:user "alice"
:password "foo"
:dbname "config-test"
:table "wonderland"}
:schema-flexibility :write
:keep-history? false}
config2 (assoc-in config [:store :table] "tea_party")
_ (d/delete-database config)
_ (d/delete-database config2)]
(is (not (d/database-exists? config)))
(is (not (d/database-exists? config2)))
(let [_ (d/create-database config)
_ (d/create-database config2)
conn (d/connect config)
conn2 (d/connect config2)]
(d/transact conn [{:db/ident :name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :age
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}])
(d/transact conn [{:db/id 1, :name "Ivan", :age 15}
{:db/id 2, :name "Petr", :age 37}
{:db/id 3, :name "Ivan", :age 37}
{:db/id 4, :age 15}])
(is (= (d/q '[:find ?e :where [?e :name]] @conn)
#{[3] [2] [1]}))
(is (empty? (d/q '[:find ?e :where [?e :name]] @conn2)))
(d/release conn)
(d/release conn2)
(is (d/database-exists? config))
(d/delete-database config)
(is (d/database-exists? config2))
(d/delete-database config2)
(is (not (d/database-exists? config)))
(is (not (d/database-exists? config2))))))

0 comments on commit 5427039

Please sign in to comment.