Skip to content

Commit

Permalink
Added the migration count function.
Browse files Browse the repository at this point in the history
  • Loading branch information
macourtney committed May 29, 2013
1 parent d1835aa commit 5aa72d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/drift/Drift.clj
Expand Up @@ -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]
Expand All @@ -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)))

Expand Down
7 changes: 7 additions & 0 deletions src/drift/execute.clj
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions src/drift/runner.clj
Expand Up @@ -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." }
Expand Down

0 comments on commit 5aa72d5

Please sign in to comment.