Back

list-migrations (clj)

(source)

function

(list-migrations config)

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.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.migrations
  (:require
    [clojure.test :refer [deftest is]]
    [migratus.migration.sql :as sql-mig]
    [migratus.migrations :as sut]
    [migratus.properties :as props]
    [migratus.utils :as utils]))

(deftest test-list-migrations
  (is (= #{(sql-mig/->SqlMigration
             20111202113000
             "create-bar-table"
             "CREATE TABLE IF NOT EXISTS bar(id BIGINT);\n"
             "DROP TABLE IF EXISTS bar;\n")
           (sql-mig/->SqlMigration
             20111202110600
             "create-foo-table"
             "CREATE TABLE IF NOT EXISTS foo(id bigint);\n"
             "DROP TABLE IF EXISTS foo;\n")
           (sql-mig/->SqlMigration
             20120827170200
             "multiple-statements"
             multi-stmt-up
             multi-stmt-down)}
        (set (sut/list-migrations {:migration-dir "migrations"})))))

(deftest test-list-migrations-bad-type
  (is (empty?
        (sut/list-migrations {:migration-dir "migrations-bad-type"}))))

(deftest test-list-migrations-duplicate-type
  (is (thrown-with-msg?
        Exception
        #"Multiple migration types"
        (sut/list-migrations {:migration-dir "migrations-duplicate-type"}))))

(deftest test-list-migrations-duplicate-name
  (is (thrown-with-msg?
        Exception
        #"Multiple migrations with id"
        (sut/list-migrations {:migration-dir "migrations-duplicate-name"}))))
yogthos/migratus
(ns migratus.test.migrations
  (:require
    [clojure.test :refer [deftest is]]
    [migratus.migration.sql :as sql-mig]
    [migratus.migrations :as sut]
    [migratus.properties :as props]
    [migratus.utils :as utils]))

(deftest test-list-migrations
  (is (= #{(sql-mig/->SqlMigration
             20111202113000
             "create-bar-table"
             "CREATE TABLE IF NOT EXISTS bar(id BIGINT);\n"
             "DROP TABLE IF EXISTS bar;\n")
           (sql-mig/->SqlMigration
             20111202110600
             "create-foo-table"
             "CREATE TABLE IF NOT EXISTS foo(id bigint);\n"
             "DROP TABLE IF EXISTS foo;\n")
           (sql-mig/->SqlMigration
             20120827170200
             "multiple-statements"
             multi-stmt-up
             multi-stmt-down)}
        (set (sut/list-migrations {:migration-dir "migrations"})))))

(deftest test-list-migrations-bad-type
  (is (empty?
        (sut/list-migrations {:migration-dir "migrations-bad-type"}))))

(deftest test-list-migrations-duplicate-type
  (is (thrown-with-msg?
        Exception
        #"Multiple migration types"
        (sut/list-migrations {:migration-dir "migrations-duplicate-type"}))))

(deftest test-list-migrations-duplicate-name
  (is (thrown-with-msg?
        Exception
        #"Multiple migrations with id"
        (sut/list-migrations {:migration-dir "migrations-duplicate-name"}))))