Skip to content

Commit

Permalink
Add load-migrations and multi-migration files
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Nov 7, 2024
1 parent d506a3d commit 11de34c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
58 changes: 43 additions & 15 deletions sql/src/ragtime/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
[clojure.string :as str]
[resauce.core :as resauce]))

(defn- wrap-single-migration [migration]
(if (map? migration) [migration] migration))

(defn- normalize-migration [migration]
(update migration :transactions (fnil identity :both)))

(defn- file-extension [file]
(re-find #"\.[^.]*$" (str file)))

Expand All @@ -14,28 +20,50 @@
(defn- remove-extension [file]
(second (re-matches #"(.*)\.[^.]*" (str file))))

(defmulti load-files
(defprotocol ToURI
(to-uri [x]))

(extend-protocol ToURI
String (to-uri [s] (.toURI (io/file s)))
java.io.File (to-uri [f] (.toURI f))
java.net.URL (to-uri [u] (.toURI u))
java.net.URI (to-uri [u] u))

(defn- guess-id-from-file-extension [f migrations]
(if (and (nil? (next migrations))
(nil? (:id (first migrations)))
(satisfies? ToURI f))
[(assoc (first migrations) :id (-> f to-uri str basename remove-extension))]
migrations))

(defn load-migrations
"Load one or more migrations from an edn reader source. Returns an ordered
collection of migration maps."
[f]
(->> (slurp f)
(edn/read-string)
(wrap-single-migration)
(map normalize-migration)
(guess-id-from-file-extension f)))

(defmulti load-file-seq
"Given an collection of files with the same extension, return a ordered
collection of migrations. Dispatches on extension (e.g. \".edn\"). Extend
this multimethod to support new formats for specifying SQL migrations."
(fn [files] (file-extension (first files))))

(defmethod load-files :default [_files])
(defmethod load-file-seq :default [_files])

(defmethod load-files ".edn" [files]
(for [file files]
(-> (slurp file)
(edn/read-string)
(update-in [:id] #(or % (-> file basename remove-extension)))
(update-in [:transactions] (fnil identity :both)))))
(defmethod load-file-seq ".edn" [files]
(mapcat load-migrations files))

(defn- sql-file-parts [file]
(rest (re-matches #"(.*?)\.(up|down)(?:\.(\d+))?\.sql" (str file))))

(defn- read-sql [file]
(str/split (slurp file) #"(?m)\n\s*--\s?;;\s*\n"))

(defmethod load-files ".sql" [files]
(defmethod load-file-seq ".sql" [files]
(for [[id files] (->> files
(group-by (comp first sql-file-parts))
(sort-by key))]
Expand All @@ -47,17 +75,17 @@
(defn- load-all-files [files]
(->> (group-by file-extension files)
(vals)
(mapcat load-files)
(mapcat load-file-seq)
(sort-by :id)))

(defn load-directory
"Load a collection of Ragtime migrations from a directory."
"Load a collection of Ragtime migrations from a directory. These can be in edn
or SQL format."
[path]
(->> (file-seq (io/file path))
(map #(.toURI ^java.io.File %))
(load-all-files)))
(load-all-files (file-seq (io/file path))))

(defn load-resources
"Load a collection of Ragtime migrations from a classpath prefix."
"Load a collection of Ragtime migrations from a classpath prefix. These can be
in edn or SQL format."
[path]
(load-all-files (resauce/resource-dir path)))
2 changes: 2 additions & 0 deletions sql/test/migrations/008-test.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{:id "008-test" :up ["CREATE TABLE aaa (id int)"] :down ["DROP TABLE aaa"]}
{:id "009-test" :up ["CREATE TABLE bbb (id int)"] :down ["DROP TABLE bbb"]}]
19 changes: 17 additions & 2 deletions sql/test/ragtime/sql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
:up ["--\n-- ISSUE 159 Test\n--\nCREATE TABLE quxc (id int);"
"CREATE TABLE quxd (id int);\n"]
:down ["--\n-- ISSUE 159 Test\n--\nDROP TABLE quxc;"
"DROP TABLE quxd;\n"]}]
"DROP TABLE quxd;\n"]}
{:id "008-test", :transactions :both
: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"]}]
(sql/load-directory "test/migrations"))))

(deftest test-load-resources
Expand All @@ -45,5 +49,16 @@
:up ["--\n-- ISSUE 159 Test\n--\nCREATE TABLE quxc (id int);"
"CREATE TABLE quxd (id int);\n"]
:down ["--\n-- ISSUE 159 Test\n--\nDROP TABLE quxc;"
"DROP TABLE quxd;\n"]}]
"DROP TABLE quxd;\n"]}
{:id "008-test", :transactions :both
: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"]}]
(sql/load-resources "migrations"))))

(deftest test-load-migrations
(is (= [{:id "008-test", :transactions :both
: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"]}]
(sql/load-migrations "test/migrations/008-test.edn"))))

0 comments on commit 11de34c

Please sign in to comment.