diff --git a/project.clj b/project.clj index 6cb3701..71219ec 100644 --- a/project.clj +++ b/project.clj @@ -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"]]) diff --git a/src/duct/module/sql.clj b/src/duct/module/sql.clj index 927cbc0..3a7e4ce 100644 --- a/src/duct/module/sql.clj +++ b/src/duct/module/sql.clj @@ -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"}}) diff --git a/src/integrant/annotations.edn b/src/integrant/annotations.edn new file mode 100644 index 0000000..c944b98 --- /dev/null +++ b/src/integrant/annotations.edn @@ -0,0 +1,3 @@ +{:duct.module/sql + {:duct/vars + {jdbc-url {:arg jdbc-url, :env JDBC_DATABASE_URL}}}} diff --git a/src/duct_hierarchy.edn b/src/integrant/hierarchy.edn similarity index 100% rename from src/duct_hierarchy.edn rename to src/integrant/hierarchy.edn diff --git a/test/duct/module/sql_test.clj b/test/duct/module/sql_test.clj index 6eda477..2850949 100644 --- a/test/duct/module/sql_test.clj +++ b/test/duct/module/sql_test.clj @@ -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:"}))))))