Back
up (clj)
(source)function
(up config & ids)
Bring up the migrations identified by ids.
Any migrations that are already complete will be skipped.
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]))
(defn migrations [ups downs]
(for [n (range 4)]
(mock/make-migration
{:id (inc n) :name (str "id-" (inc n)) :ups ups :downs downs})))
(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-create-and-destroy
(let [migration "create-user"
migration-up "create-user.up.sql"
migration-down "create-user.down.sql"]
(testing "should create two migrations"
(create nil migration)
(is (migration-exists? migration-up))
(is (migration-exists? migration-down)))
(testing "should delete two migrations"
(destroy nil migration)
(is (empty? (migration-exists? migration-up)))
(is (empty? (migration-exists? migration-down))))))
(deftest test-create-missing-directory
(let [migration-dir "doesnt_exist"
config {:parent-migration-dir "test"
:migration-dir migration-dir}
migration "create-user"
migration-up "create-user.up.sql"
migration-down "create-user.down.sql"]
;; Make sure the directory doesn't exist before we start the test
(when (.exists (io/file "test" migration-dir))
(io/delete-file (io/file "test" migration-dir)))
(testing "when migration dir doesn't exist, it is created"
(is (nil? (utils/find-migration-dir migration-dir)))
(create config migration)
(is (not (nil? (utils/find-migration-dir migration-dir))))
(is (migration-exists? migration-up migration-dir))
(is (migration-exists? migration-down migration-dir)))
;; Clean up after ourselves
(when (.exists (io/file "test" migration-dir))
(destroy config migration)
(io/delete-file (io/file "test" migration-dir)))))
(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)))))))
(deftest supported-extensions
(testing "All supported extensions show up.
NOTE: when you add a protocol, to migratus core, update this test")
(is (= '("sql" "edn")
(proto/get-all-supported-extensions))))
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.database
(:require [clojure.java.io :as io]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]
[next.jdbc.quoted :as q]
[next.jdbc.sql :as sql]
[migratus.protocols :as proto]
[migratus.core :as core]
[clojure.test :refer :all]
[migratus.database :refer :all :as db]
[clojure.tools.logging :as log]
[migratus.test.migration.edn :as test-edn]
[migratus.test.migration.sql :as test-sql]
[migratus.utils :as utils])
(:import java.io.File
java.sql.Connection
java.util.jar.JarFile
(java.util.concurrent CancellationException)))
(use-fixtures :each test-sql/setup-test-db)
(deftest test-migrate
(is (not (test-sql/verify-table-exists? config "foo")))
(is (not (test-sql/verify-table-exists? config "bar")))
(is (not (test-sql/verify-table-exists? config "quux")))
(is (not (test-sql/verify-table-exists? config "quux2")))
(core/migrate config)
(is (test-sql/verify-table-exists? config "foo"))
(is (test-sql/verify-table-exists? config "bar"))
(is (test-sql/verify-table-exists? config "quux"))
(is (test-sql/verify-table-exists? config "quux2"))
(core/down config 20111202110600)
(is (not (test-sql/verify-table-exists? config "foo")))
(is (test-sql/verify-table-exists? config "bar"))
(is (test-sql/verify-table-exists? config "quux"))
(is (test-sql/verify-table-exists? config "quux2"))
(core/migrate config)
(is (test-sql/verify-table-exists? config "foo"))
(is (test-sql/verify-table-exists? config "bar"))
(is (test-sql/verify-table-exists? config "quux"))
(is (test-sql/verify-table-exists? config "quux2"))
(core/down config 20111202110600 20120827170200)
(is (not (test-sql/verify-table-exists? config "foo")))
(is (test-sql/verify-table-exists? config "bar"))
(is (not (test-sql/verify-table-exists? config "quux")))
(is (not (test-sql/verify-table-exists? config "quux2")))
(core/up config 20111202110600 20120827170200)
(is (test-sql/verify-table-exists? config "foo"))
(is (test-sql/verify-table-exists? config "bar"))
(is (test-sql/verify-table-exists? config "quux"))
(is (test-sql/verify-table-exists? config "quux2")))
migratus
(ns migratus.test.migration.sql
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[migratus.core :as core]
[migratus.database :as db]
[migratus.migration.sql :refer :all]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]))
(defn setup-test-db [f]
(reset-db)
(f))
(use-fixtures :each setup-test-db)
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-resolve-fn
(require test-namespace)
(is (var? (resolve-fn "test-mig" test-namespace "migrate-up")))
(is (var? (resolve-fn "test-mig" test-namespace 'migrate-up)))
(is (var? (resolve-fn "test-mig" test-namespace :migrate-up)))
(is (thrown-with-msg?
IllegalArgumentException
#"Unable to resolve"
(resolve-fn "test-mig" test-namespace "not-a-fn")))
(is (thrown-with-msg?
IllegalArgumentException
#"Namespaced symbol not allowed"
(resolve-fn "test-mig" test-namespace "clojure.core/map"))))
(deftest test-invalid-migration
(testing "namespace is required"
(is (thrown-with-msg?
IllegalArgumentException
#"Invalid migration .* no namespace"
(edn-mig {}))))
(testing "namespace must exist"
(is (thrown?
Exception
(edn-mig {:ns 'foo.bar.baz}))))
(testing "fn must exist"
(is (thrown-with-msg?
IllegalArgumentException
#"Unable to resolve"
(edn-mig {:ns test-namespace
:up-fn 'not-a-real-fn
:down-fn 'not-a-real-fn})))))
(deftest test-edn-migration
(let [mig (edn-mig {:ns test-namespace
:up-fn 'migrate-up
:down-fn 'migrate-down})]
(is (not (test-file-exists?)))
(proto/up mig test-config)
(is (test-file-exists?))
(proto/down mig test-config)
(is (not (test-file-exists?)))))
(deftest test-edn-down-optional
(let [mig (edn-mig {:ns test-namespace
:up-fn 'migrate-up
:down-fn nil})]
(is (not (test-file-exists?)))
(proto/up mig test-config)
(is (test-file-exists?))
(proto/down mig test-config)
(is (test-file-exists?))))