diff --git a/src/drift/Drift.clj b/src/drift/Drift.clj index 41e92de..c938f46 100644 --- a/src/drift/Drift.clj +++ b/src/drift/Drift.clj @@ -7,6 +7,7 @@ (:gen-class :methods [[init [java.util.List] Object] [migrate [Long java.util.List] Void] + [migrationCount [Long java.util.List] Integer] [currentVersion [] Long] [maxMigrationNumber [] Long] [addListener [drift.listener_protocol.ListenerProtocol] java.util.Collection] @@ -18,6 +19,9 @@ (defn -migrate [_ version other-args] (execute/migrate version other-args)) +(defn -migrationCount [_ version other-args] + (execute/migration-count version other-args)) + (defn -currentVersion [_] (long (version/current-db-version))) diff --git a/src/drift/execute.clj b/src/drift/execute.clj index 6b49846..e8f9832 100644 --- a/src/drift/execute.clj +++ b/src/drift/execute.clj @@ -12,6 +12,13 @@ (Long/parseLong version) version) Long/MAX_VALUE)) + +(defn migration-count + "Returns the total number of migrations to run to update the database to the given version number." + [version remaining-args] + (core/with-init-config remaining-args + (fn [] + (runner/migration-count (version-number version))))) (defn migrate [version remaining-args] diff --git a/src/drift/runner.clj b/src/drift/runner.clj index f184e3b..f533435 100644 --- a/src/drift/runner.clj +++ b/src/drift/runner.clj @@ -97,6 +97,16 @@ (logging/info "No changes were made to the database.")) (logging/error (str "Invalid version number: " from-version " or " to-version ". No changes were made to the database.")))) +(defn migration-count + "Returns the total number of migrations to run to update the database to the given version number." + [version-number] + (if version-number + (let [db-version (version/current-db-version) + version-number-min (min (max version-number 0) (core/max-migration-number))] + (if (< db-version version-number-min) + (count (core/migration-namespaces-in-range (inc db-version) version-number-min)) + (count (core/migration-namespaces-in-range db-version (inc version-number-min))))))) + (defn #^{ :doc "Updates the database to the given version number. If the version number is less than the current database version number, then this function causes a roll back." }