Back

connect (clj)

(source)

protocol

(connect this)
Opens resources necessary to run migrations against the 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)))

(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-connect*-returns-a-connection
  (testing "connect* works with a ^java.sql.Connection"
    (let [ds (jdbc/get-datasource db-mem)]
      (with-open [connection (jdbc/get-connection ds)]
        (let [res (db/connect* {:connection connection})]
          (is (map? res) "connect* response is a map")
          (is (contains? res :connection) "connect* response contains :connection")
          (is (instance? Connection (:connection res))
              "connect* response has a ^java.sql.Connection")
          (is (= connection (:connection res))
              "connect* response contains the same connection we passed")))))

  (testing "connect* works with a ^javax.sql.DataSource"
    (let [ds (jdbc/get-datasource db-mem)
          res (db/connect* {:datasource ds})]
      (is (map? res) "connect* response is a map")
      (is (contains? res :connection) "connect* response contains :connection")
      (is (instance? Connection (:connection res))
          "connect* response has a ^java.sql.Connection")))

  (testing "connect* works with a db spec"
    (let [res (db/connect* db-mem)]
      (is (map? res) "connect* response is a map")
      (is (contains? res :connection) "connect* response contains :connection")
      (is (instance? Connection (:connection res))
          "connect* response has a ^java.sql.Connection"))))

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