-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert module to use Integrant expansions
- Loading branch information
1 parent
a88477a
commit 7da6397
Showing
5 changed files
with
50 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,15 @@ | ||
(ns duct.module.sql | ||
(:require [duct.core :as core] | ||
[duct.core.env :as env] | ||
[duct.core.merge :as merge] | ||
[integrant.core :as ig] | ||
[medley.core :as m])) | ||
(:require [integrant.core :as ig])) | ||
|
||
(def ^:private default-database-url | ||
(or (env/env "JDBC_DATABASE_URL") | ||
(env/env "DATABASE_URL"))) | ||
|
||
(def ^:private env-strategy | ||
{:production :raise-error | ||
:test :rebase | ||
:development :rebase}) | ||
|
||
(defn- database-config [jdbc-url] | ||
(defmethod ig/expand-key :duct.module/sql [_ _] | ||
{:duct.database.sql/hikaricp | ||
^:demote {:jdbc-url jdbc-url | ||
:logger (ig/ref :duct/logger)}}) | ||
|
||
(defn- migrator-config [environment] | ||
{:duct.migrator/ragtime | ||
^:demote {:database (ig/ref :duct.database/sql) | ||
:strategy (env-strategy environment) | ||
:logger (ig/ref :duct/logger) | ||
:migrations []}}) | ||
|
||
(defn- get-environment [config options] | ||
(:environment options (:duct.core/environment config :production))) | ||
|
||
(defmethod ig/init-key :duct.module/sql [_ options] | ||
(fn [config] | ||
(core/merge-configs | ||
config | ||
(database-config (:database-url options default-database-url)) | ||
(migrator-config (get-environment config options))))) | ||
{:logger (ig/ref :duct/logger) | ||
:jdbcUrl (ig/var 'jdbc-url)} | ||
:duct.migrator/ragtime | ||
{:logger (ig/ref :duct/logger) | ||
:database (ig/ref :duct.database/sql) | ||
:strategy (ig/profile | ||
:main :raise-error | ||
:repl :rebase | ||
:test :rebase) | ||
:migrations-file "migrations.edn"}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{:duct.module/sql | ||
{:duct/vars | ||
{jdbc-url {:arg jdbc-url, :env JDBC_DATABASE_URL}}}} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,46 @@ | ||
(ns duct.module.sql-test | ||
(:require [clojure.test :refer :all] | ||
[duct.core :as core] | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[duct.module.sql :as sql] | ||
[integrant.core :as ig])) | ||
|
||
(core/load-hierarchy) | ||
|
||
(def base-config | ||
{:duct.module/sql {:database-url "jdbc:sqlite:"}}) | ||
(ig/load-hierarchy) | ||
|
||
(deftest module-test | ||
(testing "blank config" | ||
(testing "main config" | ||
(is (= {:duct.database.sql/hikaricp | ||
{:jdbc-url "jdbc:sqlite:" | ||
{:jdbcUrl "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :raise-error | ||
:migrations []}} | ||
(core/build-config base-config)))) | ||
:migrations-file "migrations.edn"}} | ||
(-> {:duct.module/sql {}} | ||
(ig/expand (ig/deprofile [:main])) | ||
(ig/bind {'jdbc-url "jdbc:sqlite:"}))))) | ||
|
||
(testing "development config" | ||
(let [config (assoc base-config :duct.profile/base | ||
{:duct.core/environment :development})] | ||
(is (= {:duct.core/environment :development | ||
:duct.database.sql/hikaricp | ||
{:jdbc-url "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :rebase | ||
:migrations []}} | ||
(core/build-config config))))) | ||
(testing "repl config" | ||
(is (= {:duct.database.sql/hikaricp | ||
{:jdbcUrl "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :rebase | ||
:migrations-file "migrations.edn"}} | ||
(-> {:duct.module/sql {}} | ||
(ig/expand (ig/deprofile [:repl])) | ||
(ig/bind {'jdbc-url "jdbc:sqlite:"}))))) | ||
|
||
(testing "test config" | ||
(let [config (assoc base-config :duct.profile/base | ||
{:duct.core/environment :test})] | ||
(is (= {:duct.core/environment :test | ||
:duct.database.sql/hikaricp | ||
{:jdbc-url "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :rebase | ||
:migrations []}} | ||
(core/build-config config))))) | ||
|
||
(testing "config with existing data" | ||
(let [config (assoc base-config :duct.profile/base | ||
{:duct.migrator/ragtime {:strategy :rebase}})] | ||
(is (= {:duct.database.sql/hikaricp | ||
{:jdbc-url "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :rebase | ||
:migrations []}} | ||
(core/build-config config)))))) | ||
(is (= {:duct.database.sql/hikaricp | ||
{:jdbcUrl "jdbc:sqlite:" | ||
:logger (ig/ref :duct/logger)} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:logger (ig/ref :duct/logger) | ||
:strategy :rebase | ||
:migrations-file "migrations.edn"}} | ||
(-> {:duct.module/sql {}} | ||
(ig/expand (ig/deprofile [:test])) | ||
(ig/bind {'jdbc-url "jdbc:sqlite:"})))))) |