From bb3c5e2849366b9b894cb5c16f3d34768fb12c56 Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sat, 24 Feb 2024 15:36:09 +0000 Subject: [PATCH 1/2] minor: Add native-image compat to ns loader --- packages/mallard/src/k16/mallard/loaders/ns.clj | 17 ++++++++++------- .../test/k16/mallard/loaders/ns_test.clj | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 packages/mallard/test/k16/mallard/loaders/ns_test.clj 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))))) From 1534926a05a737a8d38ad07960e97d2912a6d68e Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Thu, 21 Mar 2024 11:33:39 +0200 Subject: [PATCH 2/2] patch: Allow passing operations directly to run! --- packages/mallard/src/k16/mallard/api.clj | 12 ++++++++---- packages/mallard/src/k16/mallard/dev.clj | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) 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]}]