Back

config (clj)

(source)

protocol

(config this)

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))

(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-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)))))))
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))

(defn test-with-store [store & commands]
  (try
    (proto/connect store)
    (doseq [cmd commands]
      (cmd (proto/config store)))
    (finally
      (proto/disconnect store))))

(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-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")))

(deftest test-migrate-with-modify-sql-fn
  (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 (assoc config :modify-sql-fn comment-out-bar-statements))
  (is (test-sql/verify-table-exists? config "foo"))
  (is (not (test-sql/verify-table-exists? config "bar")))
  (is (test-sql/verify-table-exists? config "quux"))
  (is (test-sql/verify-table-exists? config "quux2")))

(deftest test-migrate-with-multi-statement-modify-sql-fn
  (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 (assoc config :modify-sql-fn multi-bar-statements))
  (is (test-sql/verify-table-exists? config "foo"))
  (is (not (test-sql/verify-table-exists? config "bar")))
  (is (test-sql/verify-table-exists? config "bar1"))
  (is (test-sql/verify-table-exists? config "bar2"))
  (is (test-sql/verify-table-exists? config "quux"))
  (is (test-sql/verify-table-exists? config "quux2")))

(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-migrate-until-just-before
  (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-until-just-before config 20120827170200)
  (is (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/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")))

(deftest test-rollback-until-just-after
  (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/rollback-until-just-after config 20111202110600)
  (is (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)
  (db/mark-unreserved (:db config) "foo_bar")
  (db/mark-reserved (:db config) "foo_bar")

  (jdbc/execute! (:db config) ["select * from foo_bar"])
  (next.jdbc.sql/insert! (:db config) "foo_bar" {:id -1})
  (jdbc/execute-one! (:db config) ["insert into foo_bar(id) values (?)" -1] {:return-keys false})

  (jdbc/execute! (:db config)
                 [(str "CREATE TABLE " (q/ansi "table")
                       "(id BIGINT UNIQUE NOT NULL, applied TIMESTAMP,
                        description VARCHAR(1024) )")])
  (run-test test-init)
  (run-test test-rollback-until-just-after)
  (run-test test-backing-out-bad-migration-no-tx)


(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-migration-sql-edn-mixed
  (let [migration-dir (io/file "test/migrations-mixed")
        test-config   (merge config
                             test-edn/test-config
                             {:parent-migration-dir "test"
                              :migration-dir "migrations-mixed"})]
    (try
      (utils/recursive-delete (io/file test-edn/test-dir))
      (utils/recursive-delete migration-dir)
      (copy-dir (io/file "test/migrations") migration-dir)
      (copy-dir (io/file "test/migrations-edn") migration-dir)

      (is (not (test-sql/verify-table-exists? test-config "foo")))
      (is (not (test-sql/verify-table-exists? test-config "bar")))
      (is (not (test-sql/verify-table-exists? test-config "quux")))
      (is (not (test-sql/verify-table-exists? test-config "quux2")))
      (is (not (test-edn/test-file-exists?)))

      (core/migrate test-config)

      (is (test-sql/verify-table-exists? test-config "foo"))
      (is (test-sql/verify-table-exists? test-config "bar"))
      (is (test-sql/verify-table-exists? test-config "quux"))
      (is (test-sql/verify-table-exists? test-config "quux2"))
      (is (test-edn/test-file-exists?))


(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-backing-out-bad-migration
  (log/debug "running backout tests")
  (test-backing-out* (assoc config :migration-dir "migrations-intentionally-broken")))

(deftest test-backing-out-bad-migration-no-tx
  (log/debug "running backout tests without tx")
  (test-backing-out* (assoc config :migration-dir "migrations-intentionally-broken-no-tx")))


(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-no-tx-migration-pass-conn
  (with-open [conn (jdbc/get-connection (:db config))]
    (let [test-config (assoc config
                                   :migration-dir "migrations-no-tx"
                                   :db {:connection conn :managed-connection? true})]
      (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})

(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))

(def test-namespace 'migratus.test.migration.edn.test-script)
(def test-dir "target/edn-test")
(def test-config {:output-dir test-dir})

(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?))))

(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?)))))