Code-based Migrations with Migratus in Clojure

Migratus is a wonderful migration tool for Clojure. You can use it on its own, or if you have a Luminus project, it is already included.

There are a few areas that could use some extra tutorials, though. One of them is code-based migrations. Especially, examples on how to modify the database would be nice.

Here’s a quick explanation of how to modify the database with a code-based migration in Migratus.

First, create an *.edn file in the folder with the regular migrations. It takes the same format as other migrations, so you can really just change the extension of one of your created migrations to ‘.edn’ and change the contents.

The contents of your ‘edn’ file should be

The namespace specified with ‘:ns’ needs to exist, so create it. Now, copy the following to the Clojure file.

That’s nice, but how do you change the database?

My recommendation is to import next.jdbc into your project. You have an active JDBC connection in the config argument that is passed into the migrate-up and migrate-down functions. You can use that connection as your next.jdbc data source (ds).

Here’s a quick example of the Clojure end of the code-based migration.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *