Back

migrate-up (clj)

(source)

protocol

(migrate-up this migration)
Run and record an up 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 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]))
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?))))