Skip to content
Snippets Groups Projects
Commit 0fd8fcb0 authored by Cam Saul's avatar Cam Saul
Browse files

:scream: Mongo support for new-style connection details

parent e2910f09
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@
(expect-expansion 0)
(expect-let 1)
(expect-when-testing-against-dataset 1)
(expect-when-testing-mongo 1)
(expect-with-all-drivers 1)
(ins 1)
(let-400 1)
......
......@@ -13,7 +13,7 @@
[metabase.driver :as driver]
[metabase.driver.interface :refer :all]
(metabase.driver.mongo [query-processor :as qp]
[util :refer [*mongo-connection* with-mongo-connection values->base-type]])))
[util :refer [*mongo-connection* with-mongo-connection values->base-type details-map->connection-string]])))
(declare driver)
......@@ -50,20 +50,8 @@
:ok)
1.0)))
(can-connect-with-details? [this {:keys [user password host port dbname]}]
(assert (and host
dbname))
(can-connect? this (str "mongodb://"
user
(when password
(assert user "Can't have a password without a user!")
(str ":" password))
(when user "@")
host
(when port
(str ":" port))
"/"
dbname)))
(can-connect-with-details? [this details]
(can-connect? this {:details details}))
;;; ### QP
(process-query [_ query]
......
......@@ -5,6 +5,28 @@
[monger.core :as mg]
[metabase.driver :as driver]))
(defn- details-map->connection-string
[{:keys [user password host port dbname]}]
{:pre [host
dbname]}
(str "mongodb://"
user
(when password
(assert user "Can't have a password without a user!")
(str ":" password))
(when user "@")
host
(when port
(str ":" port))
"/"
dbname))
(defn- is-new-style-details?
"Is DETAILS-MAP a new-style map (i.e., are its values broken out, rather than passed as a combined `conn_str`)?"
[details-map]
{:pre [(map? details-map)]}
(:dbname details-map)) ; just look for the new-style :dbname as evidence
(def ^:dynamic *mongo-connection*
"Connection to a Mongo database.
Bound by top-level `with-mongo-connection` so it may be reused within its body."
......@@ -14,8 +36,10 @@
"Run F with a new connection (bound to `*mongo-connection*`) to DATABASE.
Don't use this directly; use `with-mongo-connection`."
[f database]
(let [connection-string (if (map? database) (-> database :details :conn_str)
database)
(let [connection-string (cond
(string? database) database
(is-new-style-details? (:details database)) (details-map->connection-string (:details database))
:else (:conn_str (:details database)))
_ (assert (string? connection-string) (str "with-mongo-connection failed: connection string is must be a string, got: " connection-string))
{conn :conn mongo-connection :db} (mg/connect-via-uri connection-string)]
(log/debug (color/cyan "<< OPENED NEW MONGODB CONNECTION >>"))
......
......@@ -64,8 +64,17 @@
;; ## Tests for connection functions
;; legacy
(expect-when-testing-mongo true
(driver/can-connect? @mongo-test-db))
(driver/can-connect? {:engine :mongo
:details {:conn_str "mongodb://localhost:27017/metabase-test"}}))
;; new-style
(expect-when-testing-mongo true
(driver/can-connect? {:engine :mongo
:details {:host "localhost"
:dbname "metabase-test"
:port 27017}}))
(expect-when-testing-mongo false
(driver/can-connect? {:engine :mongo
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment