Back
name (clj)
(source)protocol
(name this)
Name of this migration
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 MockMigration [db id name ups downs]
proto/Migration
(id [this]
id)
(name [this]
name)
(up [this config]
(swap! ups conj id)
:success)
(down [this config]
(swap! downs conj id)
:success))
(defn make-migration [{:keys [id name ups downs]}]
(MockMigration. nil id name ups downs))
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})))
(defn- migration-exists? [name & [dir]]
(when-let [migrations-dir (utils/find-migration-dir (or dir "migrations"))]
(->> (file-seq migrations-dir)
(map #(.getName %))
(filter #(.contains % name))
(not-empty))))
(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
;;;; 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)))
(def config (merge test-sql/test-config
{:store :database
:command-separator ";"
:migration-table-name "foo_bar"}))
(defn verify-data [config table-name]
(let [db (connect* (:db config))
conn (:connection db)
result (sql/query conn
[(str "SELECT * from " table-name)]
{:builder-fn rs/as-unqualified-lower-maps})]
(.close conn)
result))
(def db-mem {:dbtype "h2:mem"
:name "mem-db"})
(deftest test-make-store
(testing "should create default table name"
(is (not (test-sql/verify-table-exists?
(dissoc config :migration-table-name) default-migrations-table)))
(test-with-store
(proto/make-store (dissoc config :migration-table-name))
(fn [config]
(is (test-sql/verify-table-exists? config default-migrations-table)))))
(test-sql/reset-db)
(testing "should create schema_migrations table"
(is (not (test-sql/verify-table-exists? config "foo_bar")))
(test-with-store
(proto/make-store config)
(fn [config]
(is (test-sql/verify-table-exists? config "foo_bar")))))
(test-sql/reset-db)
(testing "should use complex table name"
(let [table-name "U&\"\\00d6ffnungszeit\""
config (assoc config :migration-table-name table-name)]
(is (not (test-sql/verify-table-exists? config table-name)))
(test-with-store
(proto/make-store config)
(fn [config]
(is (test-sql/verify-table-exists? config table-name)))))))
(deftest test-make-store-pass-conn
(testing "should create default table name"
(is (not (test-sql/verify-table-exists?
(dissoc config :migration-table-name) default-migrations-table)))
(test-with-store
(proto/make-store (-> (dissoc config :migration-table-name)
(assoc :db {:connection (jdbc/get-connection (:db config))})))
(fn [_]
(test-sql/verify-table-exists? (dissoc config :migration-table-name)
default-migrations-table))))
(test-sql/reset-db))
(deftest test-migration-table-creation-is-hooked
(let [hook-called (atom false)]
(core/migrate
(assoc config
:migration-table-name "schema_migrations"
:modify-sql-fn (fn [sql]
(when (re-find #"CREATE TABLE schema_migrations" sql)
(reset! hook-called true))
sql)))
(is @hook-called)))
(deftest test-migration-ignored-when-already-reserved
(test-with-store
(proto/make-store config)
(fn [{:keys [db migration-table-name] :as config}]
(testing "can only reserve once"
(is (mark-reserved db migration-table-name))
(is (not (mark-reserved db migration-table-name))))
(testing "migrations don't run when locked"
(is (not (test-sql/verify-table-exists? config "foo")))
(is (= :ignore (core/migrate config)))
(is (not (test-sql/verify-table-exists? config "foo"))))
(testing "migrations run once lock is freed"
(mark-unreserved db migration-table-name)
(is (nil? (core/migrate config)))
(is (test-sql/verify-table-exists? config "foo")))
(testing "rollback migration isn't run when locked"
(is (mark-reserved db migration-table-name))
(core/down config 20111202110600)
(is (test-sql/verify-table-exists? config "foo")))
(testing "rollback migration run once lock is freed"
(mark-unreserved db migration-table-name)
(core/down config 20111202110600)
(is (not (test-sql/verify-table-exists? config "foo")))))))
(deftest test-description-and-applied-fields
(core/migrate config)
(let [from-db (verify-data config (:migration-table-name config))]
(testing "descriptions match")
(is (= (map #(dissoc % :applied) from-db)
'({:id 20111202110600,
:description "create-foo-table"}
{:id 20111202113000,
:description "create-bar-table"}
{:id 20120827170200,
:description "multiple-statements"})))
(testing "applied are timestamps")
(is (every? identity (map #(-> %
:applied
type
(= java.sql.Timestamp))
from-db)))))
(defn- test-backing-out* [test-config]
(let [{:keys [db migration-table-name]} test-config]
(testing "should fail")
(is (thrown? Throwable (core/migrate test-config)))
(testing "first statement in migration was backed out because second one failed")
(is (not (test-sql/verify-table-exists? test-config "quux2")))
(testing "third statement in migration was backed out because second one failed")
(is (not (test-sql/verify-table-exists? test-config "quux3")))
(testing "migration was not applied")
(is (not (complete? db migration-table-name 20120827170200)))
#_#_(testing "table should be unreserved after migration failure")
(is (false? (mark-reserved db migration-table-name)))))
(deftest test-no-tx-migration
(let [{:keys [db migration-table-name] :as test-config} (assoc config :migration-dir "migrations-no-tx")]
(is (not (test-sql/verify-table-exists? test-config "foo")))
(core/migrate test-config)
(is (test-sql/verify-table-exists? test-config "foo"))
(core/down test-config 20111202110600)
(is (not (test-sql/verify-table-exists? test-config "foo")))))
(deftest test-cancellation-observed
(let [lines-processed (atom 0)
future-instance (atom nil)
future-instance-set (promise)
migration-in-future (future (core/migrate
(assoc config
:migration-table-name "schema_migrations"
:modify-sql-fn (fn [sql]
(when (re-find #"CREATE TABLE schema_migrations" sql)
(deref future-instance-set)
(future-cancel @future-instance))
(swap! lines-processed inc)
sql))))]
(reset! future-instance migration-in-future)
(deliver future-instance-set true)
(is (thrown? CancellationException @migration-in-future))
(Thread/sleep 100)
(is (= 1 @lines-processed))))
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))
(def test-namespace 'migratus.test.migration.edn.test-script-args)
(def test-dir "target/edn-args-test")
(def test-config {:output-dir test-dir})
(use-fixtures :once
(fn [f]
(f)
;; `lein test` thinks it needs to test this namespace, so make sure
;; that it exists when we're done
(require test-namespace)))
(use-fixtures :each
(fn [f]
;; unload the namespace before each test to ensure that it's loaded
;; appropriately by the edn-migration code.
(unload test-namespace)
(utils/recursive-delete (io/file test-dir))
(f)))
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))
(def test-namespace 'migratus.test.migration.edn.test-script)
(def test-dir "target/edn-test")
(def test-config {:output-dir test-dir})
(use-fixtures :once
(fn [f]
(f)
;; `lein test` thinks it needs to test this namespace, so make sure
;; that it exists when we're done
(require test-namespace)))
(use-fixtures :each
(fn [f]
;; unload the namespace before each test to ensure that it's loaded
;; appropriately by the edn-migration code.
(unload test-namespace)
(utils/recursive-delete (io/file test-dir))
(f)))
(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?))))