Dan Burton
Dan Burton

Reputation: 53665

Rollback all migrations with ragtime

I'm dealing with some code that assumed that the rollback function below would roll back all migrations. However, it only seems to roll back the latest migration.

(defn create-migrator
  [spec]
  {:datastore  (ragtime.jdbc/sql-database spec)
   :migrations (ragtime.jdbc/load-resources "migrations")})

(defn rollback
  [env]
  (-> (create-db-spec env)
      (create-migrator)
      (ragtime.repl/rollback)))

How can I alter rollback to roll back all migrations?

Upvotes: 1

Views: 411

Answers (1)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

Ragtime rollback function accepts multiple options. Among them there is number of migrations to rollback or ID of migration you want to rollback to (amount-or-id).

As ragtime.jdbc/load-resources returns a seq of all migrations sorted by their names (and by convention they will be sorted by their order of application) you can query the first one and get its ID:

(-> (ragtime.jdbc/load-resources "migrations")
    (first)
    (:id))

If your database is at the latest migrations I guess using count of your migrations seq as amount should also work.


For the given example:

(defn rollback-all
  [env]
  (let [spec (create-db-spec env)
        migrator (create-migrator spec)
        count-migrations (-> migrator :migrations count)]
    (ragtime.repl/rollback migrator count-migrations)))

Upvotes: 2

Related Questions