diff --git a/packages/mallard/src/k16/mallard/api.clj b/packages/mallard/src/k16/mallard/api.clj index 4271a5c..4079782 100644 --- a/packages/mallard/src/k16/mallard/api.clj +++ b/packages/mallard/src/k16/mallard/api.clj @@ -33,17 +33,20 @@ (undo! props) (run-next! props)) +#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn run "A run function to be used in a Deps.edn project to execute operations using the file loader. :init-store! - Should be given a symbol that resolves to a datastore init function. - :load-dir - should be a resource path to a directory containing operation files that will - be loaded using the file loader. - :action - should be given an action to perform. One of #{:up :down :next :undo :redo}" + :load-dir - should be a resource path to a directory containing operation files that will + be loaded using the file loader. + :operations - Should be a symbol resolving to a set of operations + :action - should be given an action to perform. One of #{:up :down :next :undo :redo}" [{create-ctx-fn :create-ctx! create-store-fn :create-store! shutdown-fn :shutdown! load-dir :load-dir + operations :operations action :action}] (let [create-store! (requiring-resolve create-store-fn) @@ -52,7 +55,8 @@ (create-ctx)) store (create-store! context) props {:context context - :operations (loaders.fs/load! load-dir) + :operations (or operations + (loaders.fs/load! load-dir)) :store store}] (case action diff --git a/packages/mallard/src/k16/mallard/dev.clj b/packages/mallard/src/k16/mallard/dev.clj index 4841838..cb777db 100644 --- a/packages/mallard/src/k16/mallard/dev.clj +++ b/packages/mallard/src/k16/mallard/dev.clj @@ -1,6 +1,7 @@ (ns k16.mallard.dev "For use in development, this is a component that can be included in a gx configuration to automatically run any migrations on system start." + (:refer-clojure :exclude [run!]) (:require [k16.mallard.api :as api] [k16.mallard.datastore :as datastore.api] @@ -20,6 +21,7 @@ [:or executor/?Operations [:=> [:cat] executor/?Operations]]]]]]) +#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (def run! {:gx/start {:gx/processor (fn [{:keys [props]}] diff --git a/packages/mallard/src/k16/mallard/loaders/ns.clj b/packages/mallard/src/k16/mallard/loaders/ns.clj index 87e60dd..4033e90 100644 --- a/packages/mallard/src/k16/mallard/loaders/ns.clj +++ b/packages/mallard/src/k16/mallard/loaders/ns.clj @@ -4,12 +4,15 @@ (set! *warn-on-reflection* true) -(defn load! +(defmacro load! "Dynamically require all given namespaces as operation files." [namespaces] - (->> namespaces - (map (fn [ns'] - (require ns') - {:id (-> (name ns') (str/split #"\.") last) - :run-up! (ns-resolve ns' 'run-up!) - :run-down! (ns-resolve ns' 'run-down!)})))) + `(do + (doseq [namespace# ~namespaces] + (require namespace#)) + + (->> ~namespaces + (map (fn [namespace#] + {:id (-> namespace# str (str/split #"\.") last) + :run-up! (resolve (symbol (str namespace# "/run-up!"))) + :run-down! (resolve (symbol (str namespace# "/run-down!")))}))))) diff --git a/packages/mallard/test/k16/mallard/loaders/ns_test.clj b/packages/mallard/test/k16/mallard/loaders/ns_test.clj new file mode 100644 index 0000000..4f65832 --- /dev/null +++ b/packages/mallard/test/k16/mallard/loaders/ns_test.clj @@ -0,0 +1,17 @@ +(ns k16.mallard.loaders.ns-test + (:require + [clojure.test :refer [deftest is testing]] + [k16.mallard.loaders.ns :as loaders.ns] + [matcher-combinators.test])) + +(deftest ns-loader-test + (testing "It should load migrations from a given collection of namespaces" + (let [migrations (loaders.ns/load! '(fixtures.migrations.1-migration + fixtures.migrations.2-migration))] + (is (match? [{:id "1-migration" + :run-up! (requiring-resolve 'fixtures.migrations.1-migration/run-up!) + :run-down! (requiring-resolve 'fixtures.migrations.1-migration/run-down!)} + {:id "2-migration" + :run-up! (requiring-resolve 'fixtures.migrations.2-migration/run-up!) + :run-down! (requiring-resolve 'fixtures.migrations.2-migration/run-down!)}] + migrations)))))