Migrations
Last updated
Last updated
Granite doesn't have migrations built in. Instead, we leveraged the excellent work done by Juan Edi called . We have integrated Micrate into the Maze CLI.
Migration scripts are created in the db/migrations
directory. It's recommended that the files use a timestamp as a way to keep the order of execution.
A micrate script has two sections, Up and Down. Here is an example file:
Maze CLI provides the ability to generate migration scripts:
This will generate an empty micrate script with the timestamp set in the filename:
If you generate a model or scaffold, it will create a migration script that will create and drop the database table.
This will generate the migration script shown above.
You can run the migrations using either:
You can rollback a migration using either:
The maze db
commmand also allows you to create and drop the database itself:
Sometimes you need to pre-populate your tables with data. This is common if you need an beginning administrator account or populate lookup tables that rarely change.
You can do this by creating a db/seeds.cr
file
This example seeds.cr
file creates an admin user.
You can execute the seeds using: