Back

migrate (clj)

(source)

function

(migrate config)
Bring up any migrations that are not completed. Returns nil if successful, :ignore if the table is reserved, :failure otherwise. Supports thread cancellation.

Examples

migratus
(ns migratus.testcontainers.postgres
  "Integration tests for postgresql using testcontainers.org"
  {:authors ["Eugen Stan"]}
  (:require [clj-test-containers.core :as tc]
            [clojure.tools.logging :as log]
            [clojure.set :as set]
            [clojure.test :refer [deftest is testing]]
            [migratus.test.migration.sql :as test-sql]
            [migratus.core :as migratus]
            [next.jdbc :as jdbc]
            [next.jdbc.result-set :as rs]))

        ;; migrate
        (migratus/migrate config)
        (let [db-meta (test-sql/db-tables-and-views ds)
              table-names (meta->table-names db-meta) 
              expected-tables #{"quux" "foo" "foo_bar"}]
          (log/info "Tables are" table-names)
          (is (= (count expected-tables) (count db-meta))
              (str "expected table count is ok."))
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))))
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-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")


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

      (core/migrate test-config)


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

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

    (core/migrate config)
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-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?)))))
yogthos/migratus
(ns migratus.testcontainers.postgres
  "Integration tests for postgresql using testcontainers.org"
  {:authors ["Eugen Stan"]}
  (:require [clj-test-containers.core :as tc]
            [clojure.tools.logging :as log]
            [clojure.set :as set]
            [clojure.test :refer [deftest is testing]]
            [migratus.test.migration.sql :as test-sql]
            [migratus.core :as migratus]
            [next.jdbc :as jdbc]
            [next.jdbc.result-set :as rs]))

        ;; migrate
        (migratus/migrate config)
        (let [db-meta (test-sql/db-tables-and-views ds)
              table-names (meta->table-names db-meta) 
              expected-tables #{"quux" "foo" "foo_bar"}]
          (log/info "Tables are" table-names)
          (is (= (count expected-tables) (count db-meta))
              (str "expected table count is ok."))
kit-clj/kit
(ns kit.edge.db.sql.migratus
  (:require
    [integrant.core :as ig]
    [migratus.core :as migratus]))

(defmethod ig/init-key :db.sql/migrations
  [_ {:keys [migrate-on-init?]
      :or   {migrate-on-init? true}
      :as   component}]
  (when migrate-on-init?
    (migratus/migrate component))
  component)
emil0r/reverie
(ns reverie.test.database.migrations
  (:require [migratus.core :as migratus]
            [midje.sweet :refer :all]
            [reverie.test.database.sql-helpers :refer [db-spec]]))

  (let [mmap {:store :database
              :migration-dir "migrations/reverie/postgresql"
              :migration-table-name "migrations"
              :db db-spec}]
    (migratus/down mmap 2 1)
    (migratus/migrate mmap))
  )
clojure-garden/clojure-garden
(ns platform.system.database-migrator
  (:require
    [integrant.core :as ig]
    [migratus.core :as migratus]))


(defmethod ig/init-key :platform.system/database-migrator [_ config]
  (migratus/migrate config))