Back
create (clj)
(source)function
(create config name migration-type)
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.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
(ns migratus.test.migrations
(:require
[clojure.test :refer [deftest is]]
[migratus.migration.sql :as sql-mig]
[migratus.migrations :as sut]
[migratus.properties :as props]
[migratus.utils :as utils]))
(deftest test-parse-name
(is (= ["20111202110600" "create-foo-table" ["up" "sql"]]
(sut/parse-name "20111202110600-create-foo-table.up.sql")))
(is (= ["20111202110600" "create-foo-table" ["down" "sql"]]
(sut/parse-name "20111202110600-create-foo-table.down.sql"))))
(deftest test-find-migrations
(let [create-migrations {"20111202113000"
{"create-bar-table"
{:sql
{:up "CREATE TABLE IF NOT EXISTS bar(id BIGINT);\n"
:down "DROP TABLE IF EXISTS bar;\n"}}}
"20111202110600"
{"create-foo-table"
{:sql
{:up "CREATE TABLE IF NOT EXISTS foo(id bigint);\n"
:down "DROP TABLE IF EXISTS foo;\n"}}}}
migrations (assoc create-migrations "20120827170200"
{"multiple-statements"
{:sql
{:up multi-stmt-up
:down multi-stmt-down}}})]
(is (= migrations (sut/find-migrations "migrations" ["init.sql"] nil))
"single migrations dir")
(is (= create-migrations (sut/find-migrations "migrations" ["init.sql" "*-multiple-*"] nil))
"single migrations dir with glob exclusions"))
(is (= {"20220604110500"
{"create-foo1-table"
{:sql
{:down "DROP TABLE IF EXISTS foo1;"
:up "CREATE TABLE IF NOT EXISTS foo1(id bigint);"}}}
"20220604113000"
{"create-bar1-table"
{:sql
{:down "DROP TABLE IF EXISTS bar1;"
:up "CREATE TABLE IF NOT EXISTS bar1(id BIGINT);"}}}
"20220604113500"
{"create-bar2-table"
{:sql
{:up "CREATE TABLE IF NOT EXISTS bar2(id BIGINT);"
:down "DROP TABLE IF EXISTS bar2;"}}}
"20220604111500"
{"create-foo2-table"
{:sql
{:down "DROP TABLE IF EXISTS foo2;",
:up "CREATE TABLE IF NOT EXISTS foo2(id bigint);"}}}}
(sut/find-migrations ["migrations1" "migrations2"] [] nil))
"multiple migrations dirs")
(is (= {"20111202110600" {"create-foo-table" {:sql {:up "CREATE TABLE IF NOT EXISTS foo(id bigint);\n",
:down "DROP TABLE IF EXISTS TEST_SCHEMA.foo;\n"}},
"create-schema" {:sql {:up "CREATE SCHEMA TEST_SCHEMA\n"}}}}
(sut/find-migrations "migrations-with-props" [] {"${migratus.schema}" "TEST_SCHEMA"}))))
(deftest test-list-migrations
(is (= #{(sql-mig/->SqlMigration
20111202113000
"create-bar-table"
"CREATE TABLE IF NOT EXISTS bar(id BIGINT);\n"
"DROP TABLE IF EXISTS bar;\n")
(sql-mig/->SqlMigration
20111202110600
"create-foo-table"
"CREATE TABLE IF NOT EXISTS foo(id bigint);\n"
"DROP TABLE IF EXISTS foo;\n")
(sql-mig/->SqlMigration
20120827170200
"multiple-statements"
multi-stmt-up
multi-stmt-down)}
(set (sut/list-migrations {:migration-dir "migrations"})))))