Back

datasource-config (clj)

(source)

function

(datasource-config datasource-options)
Create datasource config from `datasource-options`

Examples

hikari-cp
(ns hikari-cp.core-test
  (:require [hikari-cp.core :refer :all])
  (:use expectations)
  (:import (com.zaxxer.hikari.pool HikariPool$PoolInitializationException)
           (com.codahale.metrics MetricRegistry)
           (com.codahale.metrics.health HealthCheckRegistry)
           (com.zaxxer.hikari.metrics.prometheus PrometheusMetricsTrackerFactory)))

(def datasource-config-with-required-settings
  (datasource-config (apply dissoc valid-options (keys default-datasource-options))))

(def datasource-config-with-overrides
  (datasource-config valid-options))

(def datasource-config-with-overrides-alternate
  (datasource-config (-> (dissoc valid-options :adapter)
                         (merge alternate-valid-options))))

(def datasource-config-with-overrides-alternate2
  (datasource-config (-> (dissoc valid-options :adapter)
                         (merge alternate-valid-options2))))

(def mysql8-datasource-config
  (datasource-config (merge valid-options {:adapter "mysql8"})))

(def mysql-datasource-config
  (datasource-config (merge valid-options
                            {:adapter "mysql"
                             :use-legacy-datetime-code false})))

(def metric-registry-config (datasource-config (merge valid-options metric-registry-options)))

(def health-check-registry-config (datasource-config (merge valid-options health-check-registry-options)))

(def metrics-tracker-factory-config (datasource-config (merge valid-options metrics-tracker-factory-options)))

(expect false
        (get (.getDataSourceProperties mysql-datasource-config) "useLegacyDatetimeCode"))
(expect "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        (.getDataSourceClassName mysql-datasource-config))
(expect "com.mysql.cj.jdbc.MysqlDataSource"
  (.getDataSourceClassName mysql8-datasource-config))
(expect true
        (.isAutoCommit datasource-config-with-required-settings))
(expect false
        (.isReadOnly datasource-config-with-required-settings))
(expect 30000
        (.getConnectionTimeout datasource-config-with-required-settings))
(expect 5000
        (.getValidationTimeout datasource-config-with-required-settings))
(expect 600000
        (.getIdleTimeout datasource-config-with-required-settings))
(expect 1800000
        (.getMaxLifetime datasource-config-with-required-settings))
(expect 10
        (.getMinimumIdle datasource-config-with-required-settings))
(expect 10
        (.getMaximumPoolSize datasource-config-with-required-settings))
(expect "org.postgresql.ds.PGSimpleDataSource"
        (.getDataSourceClassName datasource-config-with-required-settings))
(expect "username"
        (.getUsername datasource-config-with-required-settings))
(expect "password"
        (.getPassword datasource-config-with-required-settings))
(expect 5433
        (-> datasource-config-with-required-settings
            .getDataSourceProperties
            (get "portNumber")))
(expect nil
        (.getMetricRegistry datasource-config-with-required-settings))
(expect (:metric-registry metric-registry-options)
        (.getMetricRegistry metric-registry-config))

(expect nil
  (.getHealthCheckRegistry datasource-config-with-required-settings))
(expect (:health-check-registry health-check-registry-options)
  (.getHealthCheckRegistry health-check-registry-config))

(expect nil
  (.getMetricsTrackerFactory datasource-config-with-required-settings))
(expect (:metrics-tracker-factory metrics-tracker-factory-options)
  (.getMetricsTrackerFactory metrics-tracker-factory-config))

(expect "TRANSACTION_SERIALIZABLE"
  (.getTransactionIsolation datasource-config-with-required-settings))

(expect false
        (.isAutoCommit datasource-config-with-overrides))
(expect true
        (.isReadOnly datasource-config-with-overrides))
(expect 1000
        (.getConnectionTimeout datasource-config-with-overrides))
(expect 1000
        (.getValidationTimeout datasource-config-with-overrides))
(expect 0
        (.getIdleTimeout datasource-config-with-overrides))
(expect 0
        (.getMaxLifetime datasource-config-with-overrides))
(expect 0
        (.getMinimumIdle datasource-config-with-overrides))
(expect 1
        (.getMaximumPoolSize datasource-config-with-overrides))
(expect "db-pool"
        (.getPoolName datasource-config-with-overrides))
(expect "set join_collapse_limit=4"
        (.getConnectionInitSql datasource-config-with-overrides))
(expect "select 0"
        (.getConnectionTestQuery datasource-config-with-overrides))
(expect true
        (.isRegisterMbeans datasource-config-with-overrides))

(expect "org.postgresql.ds.PGPoolingDataSource"
          (.getDriverClassName datasource-config-with-overrides-alternate))
(expect "jdbc:postgresql://localhost:5433/test"
        (.getJdbcUrl datasource-config-with-overrides-alternate))

(expect "com.sybase.jdbc3.jdbc.SybDataSource"
        (.getDataSourceClassName datasource-config-with-overrides-alternate2))

(expect IllegalArgumentException
        (datasource-config (dissoc valid-options :adapter)))
(expect #"contains\? % :adapter"
        (try
          (datasource-config (validate-options (dissoc valid-options :adapter)))
          (catch IllegalArgumentException e
            (str (.getMessage e)))))

(expect "jdbc:postgres:test"
        (.getJdbcUrl (datasource-config {:jdbc-url "jdbc:postgres:test"})))


;; -- check leak detections option
;; default should stay 0
(expect 0 (-> valid-options
              (datasource-config)
              (.getLeakDetectionThreshold)))

;; it should apply a correct value
(let [config (datasource-config (assoc valid-options :leak-detection-threshold 3000))]
  (expect 3000 (.getLeakDetectionThreshold config)))
tomekw/hikari-cp
(ns hikari-cp.core-test
  (:require [hikari-cp.core :refer :all])
  (:use expectations)
  (:import (com.zaxxer.hikari.pool HikariPool$PoolInitializationException)
           (com.codahale.metrics MetricRegistry)
           (com.codahale.metrics.health HealthCheckRegistry)
           (com.zaxxer.hikari.metrics.prometheus PrometheusMetricsTrackerFactory)))

(def datasource-config-with-required-settings
  (datasource-config (apply dissoc valid-options (keys default-datasource-options))))

(def datasource-config-with-overrides
  (datasource-config valid-options))

(def datasource-config-with-overrides-alternate
  (datasource-config (-> (dissoc valid-options :adapter)
                         (merge alternate-valid-options))))

(def datasource-config-with-overrides-alternate2
  (datasource-config (-> (dissoc valid-options :adapter)
                         (merge alternate-valid-options2))))

(def mysql8-datasource-config
  (datasource-config (merge valid-options {:adapter "mysql8"})))

(def mysql-datasource-config
  (datasource-config (merge valid-options
                            {:adapter "mysql"
                             :use-legacy-datetime-code false})))

(def metric-registry-config (datasource-config (merge valid-options metric-registry-options)))

(def health-check-registry-config (datasource-config (merge valid-options health-check-registry-options)))

(def metrics-tracker-factory-config (datasource-config (merge valid-options metrics-tracker-factory-options)))

(expect false
        (get (.getDataSourceProperties mysql-datasource-config) "useLegacyDatetimeCode"))
(expect "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        (.getDataSourceClassName mysql-datasource-config))
(expect "com.mysql.cj.jdbc.MysqlDataSource"
  (.getDataSourceClassName mysql8-datasource-config))
(expect true
        (.isAutoCommit datasource-config-with-required-settings))
(expect false
        (.isReadOnly datasource-config-with-required-settings))
(expect 30000
        (.getConnectionTimeout datasource-config-with-required-settings))
(expect 5000
        (.getValidationTimeout datasource-config-with-required-settings))
(expect 600000
        (.getIdleTimeout datasource-config-with-required-settings))
(expect 1800000
        (.getMaxLifetime datasource-config-with-required-settings))
(expect 10
        (.getMinimumIdle datasource-config-with-required-settings))
(expect 10
        (.getMaximumPoolSize datasource-config-with-required-settings))
(expect "org.postgresql.ds.PGSimpleDataSource"
        (.getDataSourceClassName datasource-config-with-required-settings))
(expect "username"
        (.getUsername datasource-config-with-required-settings))
(expect "password"
        (.getPassword datasource-config-with-required-settings))
(expect 5433
        (-> datasource-config-with-required-settings
            .getDataSourceProperties
            (get "portNumber")))
(expect nil
        (.getMetricRegistry datasource-config-with-required-settings))
(expect (:metric-registry metric-registry-options)
        (.getMetricRegistry metric-registry-config))

(expect nil
  (.getHealthCheckRegistry datasource-config-with-required-settings))
(expect (:health-check-registry health-check-registry-options)
  (.getHealthCheckRegistry health-check-registry-config))

(expect nil
  (.getMetricsTrackerFactory datasource-config-with-required-settings))
(expect (:metrics-tracker-factory metrics-tracker-factory-options)
  (.getMetricsTrackerFactory metrics-tracker-factory-config))

(expect "TRANSACTION_SERIALIZABLE"
  (.getTransactionIsolation datasource-config-with-required-settings))

(expect false
        (.isAutoCommit datasource-config-with-overrides))
(expect true
        (.isReadOnly datasource-config-with-overrides))
(expect 1000
        (.getConnectionTimeout datasource-config-with-overrides))
(expect 1000
        (.getValidationTimeout datasource-config-with-overrides))
(expect 0
        (.getIdleTimeout datasource-config-with-overrides))
(expect 0
        (.getMaxLifetime datasource-config-with-overrides))
(expect 0
        (.getMinimumIdle datasource-config-with-overrides))
(expect 1
        (.getMaximumPoolSize datasource-config-with-overrides))
(expect "db-pool"
        (.getPoolName datasource-config-with-overrides))
(expect "set join_collapse_limit=4"
        (.getConnectionInitSql datasource-config-with-overrides))
(expect "select 0"
        (.getConnectionTestQuery datasource-config-with-overrides))
(expect true
        (.isRegisterMbeans datasource-config-with-overrides))

(expect "org.postgresql.ds.PGPoolingDataSource"
          (.getDriverClassName datasource-config-with-overrides-alternate))
(expect "jdbc:postgresql://localhost:5433/test"
        (.getJdbcUrl datasource-config-with-overrides-alternate))

(expect "com.sybase.jdbc3.jdbc.SybDataSource"
        (.getDataSourceClassName datasource-config-with-overrides-alternate2))

(expect IllegalArgumentException
        (datasource-config (dissoc valid-options :adapter)))
(expect #"contains\? % :adapter"
        (try
          (datasource-config (validate-options (dissoc valid-options :adapter)))
          (catch IllegalArgumentException e
            (str (.getMessage e)))))

(expect "jdbc:postgres:test"
        (.getJdbcUrl (datasource-config {:jdbc-url "jdbc:postgres:test"})))


;; -- check leak detections option
;; default should stay 0
(expect 0 (-> valid-options
              (datasource-config)
              (.getLeakDetectionThreshold)))

;; it should apply a correct value
(let [config (datasource-config (assoc valid-options :leak-detection-threshold 3000))]
  (expect 3000 (.getLeakDetectionThreshold config)))