Skip to content

Commit 4852c5e

Browse files
committed
Add 'SQL Migrations' section
1 parent eb9fc2a commit 4852c5e

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

index.adoc

+54-4
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,14 @@ user=> (reset)
872872
:resumed
873873
----
874874

875-
The SQL module adds a database connection, but we need to add a ref to
876-
our route handlers in order for them to use it. We could do this
877-
by adding a ref to each handler:
875+
The SQL module adds a database connection pool under the key
876+
`:duct.database.sql/hikaricp`, which derives from the more general
877+
`:duct.database/sql` key. We can use this connection pool as a
878+
`javax.sql.DataSource` instance.
879+
880+
In order to give our route handlers access to this, we'll use a ref. We
881+
could manually add the ref to each of the handler's option map, as
882+
shown below.
878883

879884
[,clojure]
880885
----
@@ -900,7 +905,7 @@ generates.
900905
:routes [["/" {:get :todo.routes/index}]]}}}
901906
----
902907

903-
This will add a `javax.sql.DataSource` object to the `:db` key of the
908+
This will add the `DataSource` instance to the `:db` key of the
904909
component options. We can access this from the route handler function we
905910
created earlier.
906911

@@ -915,3 +920,48 @@ created earlier.
915920
[:head [:title "Hello World Wide Web"]]
916921
[:body [:h1 "Hello World Wide Web"]]]))
917922
----
923+
924+
Before we go further, however, we should set up the database schema via
925+
a migration.
926+
927+
=== SQL Migrations
928+
929+
Part of the SQL module is to add a **migrator**, a component that will
930+
manage database migrations. By default the
931+
https://github.com/weavejester/ragtime[Ragtime] library is used, and
932+
looks for a `migrations.edn` file in your project directory.
933+
934+
Let's create a migration for a table to store the todo list items.
935+
936+
.migrations.edn
937+
[,clojure]
938+
----
939+
[[:create-table todo
940+
[id "INTEGER PRIMARY KEY"]
941+
[description "TEXT"]
942+
[checked "INTEGER DEFAULT 0"]]]
943+
----
944+
945+
When we reset the REPL, the migration is automatically applied.
946+
947+
[,shell]
948+
----
949+
user=> (reset)
950+
:reloading (todo.routes)
951+
:duct.migrator.ragtime/applying {:id "create-table-todo#336f15d4"}
952+
:resumed
953+
----
954+
955+
If the migration is modified in any way, its ID will also change. At the
956+
REPL, this will result in the old version of the migration being rolled
957+
back, and the new version applied in its place.
958+
959+
Running the application via `--main` will also apply any new migrations
960+
to the database. However, if there is any mismatch between migrations,
961+
an error will be raised instead.
962+
963+
This difference reflects the environments that `--main` and `--repl` are
964+
anticipated to be used in. During development a REPL is used and
965+
mistakes are expected, so the migrator will work to sync the migrations
966+
with the database. During production migrations need to be applied with
967+
more care, and so any discrepancies should halt the migration process.

0 commit comments

Comments
 (0)