Skip to content

Commit

Permalink
Change :do key to contain vector of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Nov 7, 2024
1 parent d167c4c commit ca8b835
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
25 changes: 14 additions & 11 deletions sql/src/ragtime/sql/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
(:refer-clojure :exclude [compile])
(:require [clojure.string :as str]))

(defmulti compile-expr
(fn [_state [key & _args]] key))

(defmulti gen-id
(fn [[key & _args]] key))

Expand All @@ -19,26 +16,32 @@

(defn- normalize-migration [migration]
(if (vector? migration)
{:id (gen-id migration), :do migration}
{:id (gen-id migration), :do [migration]}
migration))

(defmulti ^:private compile-expr
(fn [_state [key & _args]] key))

(defn- compile-do [state expr]
(let [{:keys [state up down]} (compile-expr state expr)]
(-> state
(update-in [:migration :up] #(conj (or % []) up))
(update-in [:migration :down] #(into [down] %)))))

(defn- compile-migration
([{:keys [migrations]}] migrations)
([state migration]
(if-some [expr (:do migration)]
(let [{:keys [state up down]} (compile-expr state expr)
migration (-> migration
(dissoc :do)
(assoc :up [up], :down [down]))]
(update state :migrations conj migration))
(if-some [exprs (:do migration)]
(let [state (reduce compile-do (assoc state :migration migration) exprs)]
(update state :migrations conj (-> state :migration (dissoc :do))))
(update state :migrations conj migration))))

(defn compile
"Takes an ordered collection of migrations, and compiles the migrations using
vector syntax into SQL. This replaces the :do key with the :up and :down keys
on each affected migration. For example:
[{:id \"x\" :do [:create-table t [id \"int\"]]}]
[{:id \"x\" :do [[:create-table t [id \"int\"]]]}]
Is converted into:
Expand Down
2 changes: 1 addition & 1 deletion sql/test/migrations/008-test.edn
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[{:id "008-test" :up ["CREATE TABLE aaa (id int)"] :down ["DROP TABLE aaa"]}
{:id "009-test" :do [:create-table bbb [id "int"]]}]
{:id "009-test" :do [[:create-table bbb [id "int"]]]}]
23 changes: 11 additions & 12 deletions sql/test/ragtime/sql/compiler_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
{:id "create-index-foo_name"
:up ["CREATE INDEX foo_name ON TABLE foo (name)"]
:down ["DROP INDEX foo_name"]}
{:id "drop-index-foo_name"
:up ["DROP INDEX foo_name"]
:down ["CREATE INDEX foo_name ON TABLE foo (name)"]}
{:id "drop-column-foo-name"
:up ["ALTER TABLE foo DROP COLUMN name"]
:down ["ALTER TABLE foo ADD COLUMN name text"]}
{:id "drop-table-foo"
:up ["DROP TABLE foo"]
{:id "cleanup"
:up ["DROP INDEX foo_name"
"ALTER TABLE foo DROP COLUMN name"
"DROP TABLE foo"]
:down [(str "CREATE TABLE foo (id int primary key,"
" updated_at timestamp default now())")]}]
" updated_at timestamp default now())")
"ALTER TABLE foo ADD COLUMN name text"
"CREATE INDEX foo_name ON TABLE foo (name)"]}]
(compiler/compile
'[[:create-table foo [id "int primary key"] [full_name "text"]]
[:add-column foo updated_at "timestamp default now()"]
[:rename-column foo full_name name]
[:create-index foo_name foo [name]]
[:drop-index foo_name]
[:drop-column foo name]
[:drop-table foo]]))))
{:id "cleanup"
:do [[:drop-index foo_name]
[:drop-column foo name]
[:drop-table foo]]}]))))

0 comments on commit ca8b835

Please sign in to comment.