Back
reset (clj)
(source)function
(reset config)
Reset the database by down-ing all migrations successfully
applied, then up-ing all migratinos.
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-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))))))
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)))
(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-init
(testing "db init"
(let [config (assoc config :init-script "init.sql")]
(test-sql/reset-db)
(let [store (proto/make-store config)]
(proto/init store)
(is (test-sql/verify-table-exists? config "foo")))
(test-sql/reset-db)
(let [store (proto/make-store (assoc config :init-in-transaction? false))]
(proto/init store)
(is (test-sql/verify-table-exists? config "foo"))))))
(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-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.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 reset-db []
(letfn [(delete [f]
(when (.exists f)
(.delete f)))]
(delete (io/file "site.db.trace.db"))
(delete (io/file "site.db.mv.db"))
(delete (io/file "site.db"))))
(defn setup-test-db [f]
(reset-db)
(f))