Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions jdbc/src/ragtime/jdbc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions jdbc/test/migrations/008-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up migrations.008-jdbc-test/up
:down migrations.008-jdbc-test/down}
8 changes: 8 additions & 0 deletions jdbc/test/migrations/008_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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"]))
2 changes: 2 additions & 0 deletions jdbc/test/migrations/009-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up migrations.009-jdbc-test/up
:down ["DROP TABLE clj_mixed1"]}
5 changes: 5 additions & 0 deletions jdbc/test/migrations/009_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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)"]))
2 changes: 2 additions & 0 deletions jdbc/test/migrations/010-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up ["CREATE TABLE clj_mixed2 (id int)"]
:down migrations.010-jdbc-test/down}
5 changes: 5 additions & 0 deletions jdbc/test/migrations/010_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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"]))
12 changes: 8 additions & 4 deletions jdbc/test/ragtime/jdbc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
15 changes: 9 additions & 6 deletions next-jdbc/src/ragtime/next_jdbc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions next-jdbc/test/migrations/008-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up migrations.008-next-jdbc-test/up
:down migrations.008-next-jdbc-test/down}
8 changes: 8 additions & 0 deletions next-jdbc/test/migrations/008_next_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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"]))
2 changes: 2 additions & 0 deletions next-jdbc/test/migrations/009-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up migrations.009-next-jdbc-test/up
:down ["DROP TABLE clj_mixed1"]}
5 changes: 5 additions & 0 deletions next-jdbc/test/migrations/009_next_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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)"]))
2 changes: 2 additions & 0 deletions next-jdbc/test/migrations/010-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up ["CREATE TABLE clj_mixed2 (id int)"]
:down migrations.010-next-jdbc-test/down}
5 changes: 5 additions & 0 deletions next-jdbc/test/migrations/010_next_jdbc_test.clj
Original file line number Diff line number Diff line change
@@ -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"]))
12 changes: 8 additions & 4 deletions next-jdbc/test/ragtime/next_jdbc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
9 changes: 9 additions & 0 deletions sql/src/ragtime/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sql/test/migrations/010-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:up migrations.010-sql-test/up
:down migrations.010-sql-test/down}
5 changes: 5 additions & 0 deletions sql/test/migrations/010_sql_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns migrations.010-sql-test)

(defn up [_db-spec])

(defn down [_db-spec])
6 changes: 6 additions & 0 deletions sql/test/ragtime/sql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))
Expand Down Expand Up @@ -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"))))
Expand Down
Loading