Refine Your App

Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (12 / 107)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Build Your Firs t Web App | TOC | What You ’ve Learned 👉

Okay, enough with the overview. Let’s write some code.

Managing Database Migrations

The first thing we need to do is to initialize the database. The template generates two example migration files in the resources/migrations folder. Note the date on your files will be different since it’s set to the date the application was instantiated. Let’s delete the files generated by the template before proceeding.

​ ​$ ​​rm​​ ​​resources/migrations/*​

Next, let’s see how to add migrations that are appropriate for our application. New pairs of migration files can be generated from the REPL by running the create-migration function:

​ ​user=>​ (​create-migration​ ​"guestbook"​)
​ nil
​ ​user=>​

A common beginner error that you may encounter is forgetting to start the database connection. If you do get this error, don’t worry — just run (start) and try again. In fact, any error that mentions mount.core.DerefableState is probably due to forgetting to run (start).

The create-migration function creates a pair of migration files, one for updating the database and another for rolling back the changes. These files should look something like the following:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.