Migrations
Database Migrations with micrate
If you're using Granite to query your data, you likely want to manage your database schema as well. Migrations are a great way to do that, so let's take a look at micrate, a project to manage migrations. We'll use it as a dependency instead of a pre-build binary.
Install
Add micrate your shards.yml
dependencies:
micrate:
github: juanedi/micrateUpdate shards
$ shards updateCreate an executable to run the Micrate::Cli. For this example, we'll create bin/micrate in the root of our project where we're using Granite ORM. This assumes you're exporting the DATABASE_URL for your project and an environment variable instead of using a database.yml.
#! /usr/bin/env crystal
#
# To build a standalone command line client, require the
# driver you wish to use and use `Micrate::Cli`.
#
require "micrate"
require "pg"
Micrate::DB.connection_url = ENV["DATABASE_URL"]
Micrate::Cli.runMake it executable:
We should now be able to run micrate commands.
$ bin/micrate help => should output help commands.
Creating a migration
Let's create a posts table in our database.
This will create a file under db/migrations. Let's open it and define our posts schema.
And now let's run the migration
You should now have a posts table in your database ready to query.
Last updated