-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor codebase to work with new 'Duct main' design. Update dependecies and change migrator to work with a single edn file.
- Loading branch information
1 parent
2ed75a7
commit 7313608
Showing
11 changed files
with
83 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[[:create-table foo (id "INTEGER")] | ||
[:create-table bar (id "INTEGER")]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[[:create-table foo (id "INTEGER")] | ||
[:create-table baz (id "INTEGER")]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[[:create-table foo (id "INTEGER")]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,178 +1,68 @@ | ||
(ns duct.migrator.ragtime-test | ||
(:require [clojure.java.jdbc :as jdbc] | ||
[clojure.test :refer :all] | ||
[duct.core :as duct] | ||
[duct.database.sql :as sql] | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[duct.logger :as logger] | ||
[duct.migrator.ragtime :as ragtime] | ||
[integrant.core :as ig] | ||
[clojure.java.io :as io])) | ||
|
||
(duct/load-hierarchy) | ||
[next.jdbc :as jdbc])) | ||
|
||
(defrecord TestLogger [logs] | ||
logger/Logger | ||
(-log [_ level ns-str file line id event data] | ||
(-log [_ _level _ns-str _file _line _id event data] | ||
(swap! logs conj [event data]))) | ||
|
||
(def logs | ||
(atom [])) | ||
|
||
(def db-spec | ||
{:connection (jdbc/get-connection {:connection-uri "jdbc:sqlite:"})}) | ||
|
||
(def config | ||
{:duct.database/sql db-spec | ||
|
||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:strategy :rebase | ||
:logger (->TestLogger logs) | ||
:migrations [(ig/ref ::create-foo) | ||
(ig/ref ::create-bar)]} | ||
|
||
[:duct.migrator.ragtime/sql ::create-foo] | ||
{:up ["CREATE TABLE foo (id int);"] | ||
:down ["DROP TABLE foo;"]} | ||
|
||
[:duct.migrator.ragtime/sql ::create-bar] | ||
{:up ["CREATE TABLE bar (id int);"] | ||
:down ["DROP TABLE bar;"]}}) | ||
|
||
(def config-resources | ||
{:duct.database/sql db-spec | ||
|
||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:strategy :rebase | ||
:logger (->TestLogger logs) | ||
:migrations (ig/ref :duct.migrator.ragtime/resources)} | ||
|
||
:duct.migrator.ragtime/resources | ||
{:path "duct/migrator/migrations"}}) | ||
|
||
(def config-directory | ||
{:duct.database/sql db-spec | ||
|
||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:strategy :rebase | ||
:logger (->TestLogger logs) | ||
:migrations (ig/ref :duct.migrator.ragtime/directory)} | ||
|
||
:duct.migrator.ragtime/directory | ||
{:path "test/duct/migrator/migrations"}}) | ||
|
||
(def config-mixed | ||
{:duct.database/sql db-spec | ||
(defmethod ig/init-key ::logger [_ _] | ||
(->TestLogger (atom []))) | ||
|
||
(def base-config | ||
{::logger {} | ||
:duct.database/sql {} | ||
:duct.migrator/ragtime | ||
{:database (ig/ref :duct.database/sql) | ||
:strategy :rebase | ||
:logger (->TestLogger logs) | ||
:migrations [(ig/ref :duct.migrator.ragtime/resources) | ||
(ig/ref ::create-baz)]} | ||
|
||
:duct.migrator.ragtime/resources | ||
{:path "duct/migrator/migrations"} | ||
|
||
[:duct.migrator.ragtime/sql ::create-baz] | ||
{:up ["CREATE TABLE baz (id int);"] | ||
:down ["DROP TABLE baz;"]}}) | ||
|
||
(defn- find-tables [] | ||
(jdbc/query db-spec ["SELECT name FROM sqlite_master WHERE type='table'"])) | ||
|
||
(defn- drop-all-tables [] | ||
(doseq [t (find-tables)] | ||
(jdbc/execute! db-spec [(str "DROP TABLE " (:name t))]))) | ||
{:logger (ig/ref ::logger) | ||
:database (ig/ref :duct.database/sql) | ||
:strategy :rebase | ||
:migrations-file "test/duct/migrator/migrations1.edn"}}) | ||
|
||
(defn reset-test-state [] | ||
(reset! logs []) | ||
(drop-all-tables)) | ||
(defn- find-tables [db] | ||
(jdbc/execute! db ["SELECT name FROM sqlite_master WHERE type='table'"])) | ||
|
||
(deftest migration-test | ||
(testing "default migrations table name" | ||
(reset-test-state) | ||
(let [system (ig/init config)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} {:name "foo"} {:name "bar"}])) | ||
(is (= @logs | ||
[[::ragtime/applying ":duct.migrator.ragtime-test/create-foo#f1480e44"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-bar#6d969ce8"]])))) | ||
|
||
(testing "custom migrations table name" | ||
(reset-test-state) | ||
(let [migrations-table "custom_migrations" | ||
system (-> config | ||
(assoc-in [:duct.migrator/ragtime :migrations-table] migrations-table) | ||
ig/init)] | ||
(is (= (find-tables) | ||
[{:name migrations-table} {:name "foo"} {:name "bar"}])) | ||
(is (= @logs | ||
[[::ragtime/applying ":duct.migrator.ragtime-test/create-foo#f1480e44"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-bar#6d969ce8"]])))) | ||
|
||
(testing "migrations from resource directory" | ||
(reset-test-state) | ||
(let [system (ig/init config-resources)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} {:name "foo"} {:name "bar"}])) | ||
(is (= @logs | ||
[[::ragtime/applying "01-create-foo#f1480e44"] | ||
[::ragtime/applying "02-create-bar#6d969ce8"]])))) | ||
|
||
(testing "migrations from filesystem directory" | ||
(reset-test-state) | ||
(let [system (ig/init config-directory)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} {:name "foo"} {:name "bar"}])) | ||
(is (= @logs | ||
[[::ragtime/applying "01-create-foo#f1480e44"] | ||
[::ragtime/applying "02-create-bar#6d969ce8"]])))) | ||
|
||
(testing "migrations from multiple sources" | ||
(reset-test-state) | ||
(let [system (ig/init config-mixed)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} {:name "foo"} {:name "bar"} {:name "baz"}])) | ||
(is (= @logs | ||
[[::ragtime/applying "01-create-foo#f1480e44"] | ||
[::ragtime/applying "02-create-bar#6d969ce8"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-baz#01f7277d"]]))))) | ||
|
||
(deftest remove-and-resume-test | ||
(reset-test-state) | ||
(let [system (ig/init config) | ||
config' (update-in config [:duct.migrator/ragtime :migrations] pop) | ||
system' (ig/resume config' system)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} | ||
{:name "foo"}])) | ||
(is (= @logs | ||
[[::ragtime/applying ":duct.migrator.ragtime-test/create-foo#f1480e44"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-bar#6d969ce8"] | ||
[::ragtime/rolling-back ":duct.migrator.ragtime-test/create-bar#6d969ce8"]])))) | ||
|
||
(deftest change-and-resume-test | ||
(reset-test-state) | ||
(let [system (ig/init config) | ||
config' (assoc config | ||
[:duct.migrator.ragtime/sql ::create-bar] | ||
{:up ["CREATE TABLE barbaz (id int)"] | ||
:down ["DROP TABLE barbaz"]}) | ||
system' (ig/resume config' system)] | ||
(is (= (find-tables) | ||
[{:name "ragtime_migrations"} | ||
{:name "foo"} | ||
{:name "barbaz"}])) | ||
(is (= @logs | ||
[[::ragtime/applying ":duct.migrator.ragtime-test/create-foo#f1480e44"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-bar#6d969ce8"] | ||
[::ragtime/rolling-back ":duct.migrator.ragtime-test/create-bar#6d969ce8"] | ||
[::ragtime/applying ":duct.migrator.ragtime-test/create-bar#66068fd2"]])))) | ||
(let [tempfile (java.io.File/createTempFile "duct" "db") | ||
jdbc-url (str "jdbc:sqlite:" tempfile) | ||
config (-> base-config | ||
(doto (ig/load-namespaces)) | ||
(assoc-in [:duct.database/sql :jdbcUrl] jdbc-url)) | ||
system (atom (ig/init config))] | ||
|
||
(testing "initial migrations" | ||
(let [logs (-> @system ::logger :logs) | ||
db (-> @system :duct.database/sql :datasource)] | ||
(is (= ["ragtime_migrations" "foo" "bar"] | ||
(map :sqlite_master/name (find-tables db)))) | ||
(is (= [[:duct.migrator.ragtime/applying {:id "create-table-foo#52bfa531"}] | ||
[:duct.migrator.ragtime/applying {:id "create-table-bar#3e718b28"}]] | ||
@logs)))) | ||
|
||
(testing "change migration" | ||
(let [config (assoc-in config [:duct.migrator/ragtime :migrations-file] | ||
"test/duct/migrator/migrations2.edn") | ||
system (swap! system (fn [sys] (ig/suspend! sys) (ig/resume config sys))) | ||
logs (-> system ::logger :logs) | ||
db (-> system :duct.database/sql :datasource)] | ||
(is (= ["ragtime_migrations" "foo" "baz"] | ||
(map :sqlite_master/name (find-tables db)))) | ||
(is (= [[:duct.migrator.ragtime/rolling-back {:id "create-table-bar#3e718b28"}] | ||
[:duct.migrator.ragtime/applying {:id "create-table-baz#055a605e"}]] | ||
@logs)))) | ||
|
||
(testing "remove migration" | ||
(let [config (assoc-in config [:duct.migrator/ragtime :migrations-file] | ||
"test/duct/migrator/migrations3.edn") | ||
system (swap! system (fn [sys] (ig/suspend! sys) (ig/resume config sys))) | ||
logs (-> system ::logger :logs) | ||
db (-> system :duct.database/sql :datasource)] | ||
(is (= ["ragtime_migrations" "foo"] | ||
(map :sqlite_master/name (find-tables db)))) | ||
(is (= [[:duct.migrator.ragtime/rolling-back {:id "create-table-baz#055a605e"}]] | ||
@logs)))) | ||
|
||
(.delete tempfile))) | ||
|
||
(deftest string-source-test | ||
(is (= "foo\n" (ragtime/get-string "foo\n"))) | ||
(is (= "foo\n" (ragtime/get-string (io/resource "duct/migrator/example.txt")))) | ||
(is (= "foo\n" (ragtime/get-string (duct/resource "duct/migrator/example.txt"))))) |