Back

create (clj)

(source)

function

(create config & [name type])
Create a new migration with the current date

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

  (testing "Migrations are applied succesfully in PostgreSQL."
    (let [pg-container (tc/create pg-container-spec)
          initialized-pg-container (tc/start! pg-container)
          meta->table-names #(into #{} (map :pg_class/table_name %))]
      (Thread/sleep 1000)
      (let [ds (jdbc/get-datasource {:dbtype   "postgresql"
                                     :dbname   "postgres"
                                     :user     "postgres"
                                     :password "pw"
                                     :host     (:host initialized-pg-container)
                                     :port     (get (:mapped-ports initialized-pg-container) 5432)})
            config {:store :database
                    :migration-dir "migrations-postgres"
                    :init-script "init.sql"
                    :migration-table-name "foo_bar"
                    :db {:datasource ds}}]
        (is (= [] (test-sql/db-tables-and-views ds)) "db is empty before 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.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-create-and-destroy
  (let [migration      "create-user"
        migration-up   "create-user.up.sql"
        migration-down "create-user.down.sql"]
    (testing "should create two migrations"
      (create nil migration)
      (is (migration-exists? migration-up))
      (is (migration-exists? migration-down)))
    (testing "should delete two migrations"
      (destroy nil migration)
      (is (empty? (migration-exists? migration-up)))
      (is (empty? (migration-exists? migration-down))))))

(deftest test-create-and-destroy-edn
  (let [migration     "create-other-user"
        migration-edn "create-other-user.edn"]
    (testing "should create the migration"
      (create nil migration :edn)
      (is (migration-exists? migration-edn)))
    (testing "should delete the migration"
      (destroy nil migration)
      (is (empty? (migration-exists? migration-edn))))))

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