Skip to content

Commit

Permalink
Convert module to use Integrant expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Nov 10, 2024
1 parent a88477a commit 7da6397
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 88 deletions.
10 changes: 4 additions & 6 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
:url "https://github.com/duct-framework/module.sql"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.1"]
[duct/core "0.8.0"]
[duct/database.sql.hikaricp "0.4.0"]
[duct/migrator.ragtime "0.3.2"]
[integrant "0.8.0"]
[medley "1.3.0"]])
:dependencies [[org.clojure/clojure "1.11.4"]
[org.duct-framework/database.sql.hikaricp "0.7.0"]
[org.duct-framework/migrator.ragtime "0.4.0"]
[integrant "0.13.1"]])
46 changes: 12 additions & 34 deletions src/duct/module/sql.clj
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"}})
3 changes: 3 additions & 0 deletions src/integrant/annotations.edn
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.
79 changes: 31 additions & 48 deletions test/duct/module/sql_test.clj
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:"}))))))

0 comments on commit 7da6397

Please sign in to comment.