Back

completed-ids (clj)

(source)

protocol

(completed-ids this)
Seq of ids of completed migrations in descending order of applied date.

Examples

migratus
;;;; Copyright © 2011 Paul Stadig
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License"); you may not
;;;; use this file except in compliance with the License.  You may obtain a copy
;;;; of the License at
;;;;
;;;;   http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
;;;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
;;;; License for the specific language governing permissions and limitations
;;;; under the License.
(ns migratus.mock
  (:require [migratus.protocols :as proto]))

(defrecord MockStore [completed-ids config]
  proto/Store
  (init [this])
  (completed-ids [this]
    @completed-ids)
  (migrate-up [this migration]
    (proto/up migration config)
    (swap! completed-ids conj (proto/id migration))
    :success)
  (migrate-down [this migration]
    (proto/down migration config)
    (swap! completed-ids disj (proto/id migration)))
  (connect [this])
  (disconnect [this]))

(defmethod proto/make-store :mock
  [{:keys [completed-ids] :as config}]
  (MockStore. completed-ids config))
migratus
;;;; Copyright © 2011 Paul Stadig
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License"); you may not
;;;; use this file except in compliance with the License.  You may obtain a copy
;;;; of the License at
;;;;
;;;;   http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
;;;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
;;;; License for the specific language governing permissions and limitations
;;;; under the License.
(ns migratus.test.core
  (:require [migratus.protocols :as proto]
            [migratus.mock :as mock]
            [clojure.test :refer :all]
            [migratus.core :refer :all]
            migratus.logger
            [migratus.migrations :as mig]
            [migratus.utils :as utils]
            [clojure.java.io :as io])
  (:import [migratus.mock MockStore MockMigration]))

(deftest test-migrate
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1 3})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (migrate config))
    (is (= [2 4] @ups))
    (is (empty? @downs))))

(deftest test-up
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1 3})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (testing "should bring up an uncompleted migration"
        (up config 4 2)
        (is (= [2 4] @ups))
        (is (empty? @downs)))
      (reset! ups [])
      (reset! downs [])
      (testing "should do nothing for a completed migration"
        (up config 1)
        (is (empty? @ups))
        (is (empty? @downs))))))

(deftest test-down
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1 3})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (testing "should bring down a completed migration"
        (down config 1 3)
        (is (empty? @ups))
        (is (= [3 1] @downs)))
      (reset! ups [])
      (reset! downs [])
      (testing "should do nothing for an uncompleted migration"
        (down config 2)
        (is (empty? @ups))
        (is (empty? @downs))))))

(deftest test-completed-list
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1 2 3})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (testing "should return the list of completed migrations"
        (is (= ["id-1" "id-2" "id-3"]
               (migratus.core/completed-list config)))))))

(deftest test-pending-list
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (testing "should return the list of pending migrations"
        (is (= ["id-2" "id-3" "id-4"]
               (migratus.core/pending-list config)))))))

(deftest test-select-migrations
  (let [ups    (atom [])
        downs  (atom [])
        config {:store         :mock
                :completed-ids (atom #{1 3})}]
    (with-redefs [mig/list-migrations (constantly (migrations ups downs))]
      (testing "should return the list of [id name] selected migrations"
        (is (= [[1 "id-1"] [3 "id-3"]]
               (migratus.core/select-migrations config migratus.core/completed-migrations)))
        (is (= [[2 "id-2"] [4 "id-4"]]
               (migratus.core/select-migrations config migratus.core/uncompleted-migrations)))))))
migratus
(ns migratus.test.migration.edn-with-args
  (:require [clojure.java.io :as io]
            [clojure.test :refer :all]
            [migratus.core :as core]
            [migratus.migration.edn :refer :all]
            migratus.mock
            [migratus.protocols :as proto]
            [migratus.utils :as utils])
  (:import java.io.File))

(deftest test-run-edn-migrations
  (let [config (merge test-config
                      {:store :mock
                       :completed-ids (atom #{})
                       :migration-dir "migrations-edn-args"})]
    (is (not (test-file-exists?)))
    (core/migrate config)
    (is (test-file-exists?))
    (core/rollback config)
    (is (not (test-file-exists?)))))
migratus
(ns migratus.test.migration.edn
  (:require [clojure.java.io :as io]
            [clojure.test :refer :all]
            [migratus.core :as core]
            [migratus.migration.edn :refer :all]
            migratus.mock
            [migratus.protocols :as proto]
            [migratus.utils :as utils])
  (:import java.io.File))

(deftest test-run-edn-migrations
  (let [config (merge test-config
                      {:store :mock
                       :completed-ids (atom #{})
                       :migration-dir "migrations-edn"})]
    (is (not (test-file-exists?)))
    (core/migrate config)
    (is (test-file-exists?))
    (core/rollback config)
    (is (not (test-file-exists?)))))