Back

init (clj)

(source)

protocol

(init this)
Initialize the data store.

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
;;;; 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-find-init-script-resource
  (testing "finds init.sql under migrations/ in a JAR file"
    (let [jar-file    (JarFile. "test/migrations-jar/init-test.jar")
          init-script (find-init-script-resource "migrations/" jar-file "init.sql")]
      (is (not (nil? init-script)))
      (is (= "CREATE SCHEMA foo;\n" (slurp init-script))))))

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

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