diff --git a/jdbc/src/ragtime/jdbc.clj b/jdbc/src/ragtime/jdbc.clj index 03c48c8..82242ec 100644 --- a/jdbc/src/ragtime/jdbc.clj +++ b/jdbc/src/ragtime/jdbc.clj @@ -94,17 +94,20 @@ (doseq [s statements] (jdbc/execute! db-spec [s] {:transaction? transaction?}))) +(defn- execute! [action db transactions] + (if (var? action) + (action (:db-spec db)) + (execute-sql! (:db-spec db) + action + (contains? #{:up :both true} transactions)))) + (defrecord SqlMigration [id up down transactions] p/Migration (id [_] id) (run-up! [_ db] - (execute-sql! (:db-spec db) - up - (contains? #{:up :both true} transactions))) + (execute! up db transactions)) (run-down! [_ db] - (execute-sql! (:db-spec db) - down - (contains? #{:down :both true} transactions)))) + (execute! down db transactions))) (defn sql-migration "Create a Ragtime migration from a map with a unique :id, and :up and :down diff --git a/jdbc/test/migrations/008-test.edn b/jdbc/test/migrations/008-test.edn new file mode 100644 index 0000000..f44467c --- /dev/null +++ b/jdbc/test/migrations/008-test.edn @@ -0,0 +1,2 @@ +{:up migrations.008-jdbc-test/up + :down migrations.008-jdbc-test/down} diff --git a/jdbc/test/migrations/008_jdbc_test.clj b/jdbc/test/migrations/008_jdbc_test.clj new file mode 100644 index 0000000..79f7b05 --- /dev/null +++ b/jdbc/test/migrations/008_jdbc_test.clj @@ -0,0 +1,8 @@ +(ns migrations.008-jdbc-test + (:require [clojure.java.jdbc :as jdbc])) + +(defn up [db-spec] + (jdbc/execute! db-spec ["CREATE TABLE clj (id int)"])) + +(defn down [db-spec] + (jdbc/execute! db-spec ["DROP TABLE clj"])) diff --git a/jdbc/test/migrations/009-test.edn b/jdbc/test/migrations/009-test.edn new file mode 100644 index 0000000..9a9dcf7 --- /dev/null +++ b/jdbc/test/migrations/009-test.edn @@ -0,0 +1,2 @@ +{:up migrations.009-jdbc-test/up + :down ["DROP TABLE clj_mixed1"]} diff --git a/jdbc/test/migrations/009_jdbc_test.clj b/jdbc/test/migrations/009_jdbc_test.clj new file mode 100644 index 0000000..d9c872f --- /dev/null +++ b/jdbc/test/migrations/009_jdbc_test.clj @@ -0,0 +1,5 @@ +(ns migrations.009-jdbc-test + (:require [clojure.java.jdbc :as jdbc])) + +(defn up [db-spec] + (jdbc/execute! db-spec ["CREATE TABLE clj_mixed1 (id int)"])) diff --git a/jdbc/test/migrations/010-test.edn b/jdbc/test/migrations/010-test.edn new file mode 100644 index 0000000..c23dcce --- /dev/null +++ b/jdbc/test/migrations/010-test.edn @@ -0,0 +1,2 @@ +{:up ["CREATE TABLE clj_mixed2 (id int)"] + :down migrations.010-jdbc-test/down} diff --git a/jdbc/test/migrations/010_jdbc_test.clj b/jdbc/test/migrations/010_jdbc_test.clj new file mode 100644 index 0000000..b9bda57 --- /dev/null +++ b/jdbc/test/migrations/010_jdbc_test.clj @@ -0,0 +1,5 @@ +(ns migrations.010-jdbc-test + (:require [clojure.java.jdbc :as jdbc])) + +(defn down [db-spec] + (jdbc/execute! db-spec ["DROP TABLE clj_mixed2"])) diff --git a/jdbc/test/ragtime/jdbc_test.clj b/jdbc/test/ragtime/jdbc_test.clj index 3f23b1e..1c9c8d3 100644 --- a/jdbc/test/ragtime/jdbc_test.clj +++ b/jdbc/test/ragtime/jdbc_test.clj @@ -91,9 +91,11 @@ ms (jdbc/load-directory "test/migrations") idx (core/into-index ms)] (core/migrate-all db idx ms) - (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD"} + (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" "QUZA" "QUZB" "QUXA" + "QUXB" "LAST_TABLE" "QUXC" "QUXD" "CLJ" "CLJ_MIXED1" "CLJ_MIXED2"} (table-names db))) - (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" "007-test"] + (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" + "007-test" "008-test" "009-test" "010-test"] (p/applied-migration-ids db))) (core/rollback-last db idx (count ms)) (is (= #{"RAGTIME_MIGRATIONS"} (table-names db))) @@ -104,9 +106,11 @@ ms (jdbc/load-resources "migrations") idx (core/into-index ms)] (core/migrate-all db idx ms) - (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD"} + (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" "QUZA" "QUZB" "QUXA" + "QUXB" "LAST_TABLE" "QUXC" "QUXD" "CLJ" "CLJ_MIXED1" "CLJ_MIXED2"} (table-names db))) - (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" "007-test"] + (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" + "007-test" "008-test" "009-test" "010-test"] (p/applied-migration-ids db))) (core/rollback-last db idx (count ms)) (is (= #{"RAGTIME_MIGRATIONS"} (table-names db))) diff --git a/next-jdbc/src/ragtime/next_jdbc.clj b/next-jdbc/src/ragtime/next_jdbc.clj index 63d89d5..0023894 100644 --- a/next-jdbc/src/ragtime/next_jdbc.clj +++ b/next-jdbc/src/ragtime/next_jdbc.clj @@ -131,17 +131,20 @@ (doseq [s statements] (jdbc/execute! datasource [s])))) +(defn- execute! [action db transactions] + (if (var? action) + (action (:datasource db)) + (execute-sql! (:datasource db) + action + (contains? #{:up :both true} transactions)))) + (defrecord SqlMigration [id up down transactions] p/Migration (id [_] id) (run-up! [_ db] - (execute-sql! (:datasource db) - up - (contains? #{:up :both true} transactions))) + (execute! up db transactions)) (run-down! [_ db] - (execute-sql! (:datasource db) - down - (contains? #{:down :both true} transactions)))) + (execute! down db transactions))) (defn sql-migration "Create a Ragtime migration from a map with a unique :id, and :up and :down diff --git a/next-jdbc/test/migrations/008-test.edn b/next-jdbc/test/migrations/008-test.edn new file mode 100644 index 0000000..56010f4 --- /dev/null +++ b/next-jdbc/test/migrations/008-test.edn @@ -0,0 +1,2 @@ +{:up migrations.008-next-jdbc-test/up + :down migrations.008-next-jdbc-test/down} diff --git a/next-jdbc/test/migrations/008_next_jdbc_test.clj b/next-jdbc/test/migrations/008_next_jdbc_test.clj new file mode 100644 index 0000000..fdda4f5 --- /dev/null +++ b/next-jdbc/test/migrations/008_next_jdbc_test.clj @@ -0,0 +1,8 @@ +(ns migrations.008-next-jdbc-test + (:require [next.jdbc :as jdbc])) + +(defn up [datasource] + (jdbc/execute! datasource ["CREATE TABLE clj (id int)"])) + +(defn down [datasource] + (jdbc/execute! datasource ["DROP TABLE clj"])) diff --git a/next-jdbc/test/migrations/009-test.edn b/next-jdbc/test/migrations/009-test.edn new file mode 100644 index 0000000..641e9da --- /dev/null +++ b/next-jdbc/test/migrations/009-test.edn @@ -0,0 +1,2 @@ +{:up migrations.009-next-jdbc-test/up + :down ["DROP TABLE clj_mixed1"]} diff --git a/next-jdbc/test/migrations/009_next_jdbc_test.clj b/next-jdbc/test/migrations/009_next_jdbc_test.clj new file mode 100644 index 0000000..c104d04 --- /dev/null +++ b/next-jdbc/test/migrations/009_next_jdbc_test.clj @@ -0,0 +1,5 @@ +(ns migrations.009-next-jdbc-test + (:require [next.jdbc :as jdbc])) + +(defn up [datasource] + (jdbc/execute! datasource ["CREATE TABLE clj_mixed1 (id int)"])) diff --git a/next-jdbc/test/migrations/010-test.edn b/next-jdbc/test/migrations/010-test.edn new file mode 100644 index 0000000..c034556 --- /dev/null +++ b/next-jdbc/test/migrations/010-test.edn @@ -0,0 +1,2 @@ +{:up ["CREATE TABLE clj_mixed2 (id int)"] + :down migrations.010-next-jdbc-test/down} diff --git a/next-jdbc/test/migrations/010_next_jdbc_test.clj b/next-jdbc/test/migrations/010_next_jdbc_test.clj new file mode 100644 index 0000000..4a1c941 --- /dev/null +++ b/next-jdbc/test/migrations/010_next_jdbc_test.clj @@ -0,0 +1,5 @@ +(ns migrations.010-next-jdbc-test + (:require [next.jdbc :as jdbc])) + +(defn down [datasource] + (jdbc/execute! datasource ["DROP TABLE clj_mixed2"])) diff --git a/next-jdbc/test/ragtime/next_jdbc_test.clj b/next-jdbc/test/ragtime/next_jdbc_test.clj index 754f055..8d2a08e 100644 --- a/next-jdbc/test/ragtime/next_jdbc_test.clj +++ b/next-jdbc/test/ragtime/next_jdbc_test.clj @@ -112,9 +112,11 @@ idx (core/into-index ms)] (core/migrate-all db idx ms) (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" - "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD"} + "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD" + "CLJ" "CLJ_MIXED1" "CLJ_MIXED2"} (table-names db))) - (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" "007-test"] + (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" + "007-test" "008-test" "009-test" "010-test"] (p/applied-migration-ids db))) (core/rollback-last db idx (count ms)) (is (= #{"RAGTIME_MIGRATIONS"} (table-names db))) @@ -126,9 +128,11 @@ idx (core/into-index ms)] (core/migrate-all db idx ms) (is (= #{"RAGTIME_MIGRATIONS" "FOO" "BAR" "BAZ" - "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD"} + "QUZA" "QUZB" "QUXA" "QUXB" "LAST_TABLE" "QUXC" "QUXD" + "CLJ" "CLJ_MIXED1" "CLJ_MIXED2"} (table-names db))) - (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" "007-test"] + (is (= ["001-test" "002-bar" "003-test" "004-test" "005-test" "006-test" + "007-test" "008-test" "009-test" "010-test"] (p/applied-migration-ids db))) (core/rollback-last db idx (count ms)) (is (= #{"RAGTIME_MIGRATIONS"} (table-names db))) diff --git a/sql/src/ragtime/sql.clj b/sql/src/ragtime/sql.clj index 94aedb5..6fb728f 100644 --- a/sql/src/ragtime/sql.clj +++ b/sql/src/ragtime/sql.clj @@ -11,6 +11,14 @@ (defn- normalize-migration [migration] (update migration :transactions (fnil identity :both))) +(defn- resolve-symbols-to-vars [migration] + (cond-> migration + (symbol? (:up migration)) + (update :up requiring-resolve) + + (symbol? (:down migration)) + (update :down requiring-resolve))) + (defn- file-extension [file] (re-find #"\.[^.]*$" (str file))) @@ -46,6 +54,7 @@ (wrap-single-migration) (compiler/compile) (map normalize-migration) + (map resolve-symbols-to-vars) (guess-id-from-file-extension f))) (defmulti load-file-seq diff --git a/sql/test/migrations/010-test.edn b/sql/test/migrations/010-test.edn new file mode 100644 index 0000000..24f7499 --- /dev/null +++ b/sql/test/migrations/010-test.edn @@ -0,0 +1,2 @@ +{:up migrations.010-sql-test/up + :down migrations.010-sql-test/down} diff --git a/sql/test/migrations/010_sql_test.clj b/sql/test/migrations/010_sql_test.clj new file mode 100644 index 0000000..649590b --- /dev/null +++ b/sql/test/migrations/010_sql_test.clj @@ -0,0 +1,5 @@ +(ns migrations.010-sql-test) + +(defn up [_db-spec]) + +(defn down [_db-spec]) diff --git a/sql/test/ragtime/sql_test.clj b/sql/test/ragtime/sql_test.clj index 2b24dbd..1c51568 100644 --- a/sql/test/ragtime/sql_test.clj +++ b/sql/test/ragtime/sql_test.clj @@ -27,6 +27,9 @@ :up ["CREATE TABLE aaa (id int)"] :down ["DROP TABLE aaa"]} {:id "009-test", :transactions :both :up ["CREATE TABLE bbb (id int)"] :down ["DROP TABLE bbb"]} + {:id "010-test" :transactions :both + :up (requiring-resolve 'migrations.010-sql-test/up) + :down (requiring-resolve 'migrations.010-sql-test/down)} {:id "create-table-ccc", :transactions :both :up ["CREATE TABLE ccc (id int)"] :down ["DROP TABLE ccc"]}] (sql/load-directory "test/migrations")))) @@ -56,6 +59,9 @@ :up ["CREATE TABLE aaa (id int)"] :down ["DROP TABLE aaa"]} {:id "009-test", :transactions :both :up ["CREATE TABLE bbb (id int)"] :down ["DROP TABLE bbb"]} + {:id "010-test" :transactions :both + :up (requiring-resolve 'migrations.010-sql-test/up) + :down (requiring-resolve 'migrations.010-sql-test/down)} {:id "create-table-ccc", :transactions :both :up ["CREATE TABLE ccc (id int)"] :down ["DROP TABLE ccc"]}] (sql/load-resources "migrations"))))