maze
  • Introduction
  • Getting Started
    • Quick Start
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params Validation
      • Cookies
      • Filters
      • Flash
      • Redirection
    • Views
      • View Helpers
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Crecto
        • Crecto's README
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
      • Deliver a new Email
  • Testing
    • System Tests
  • Deployment
    • Digital Ocean
    • Heroku
  • CLI
    • New
    • Recipes
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
    • Deploy
  • Examples
    • Maze Auth
    • Crystal Debug
    • Minimal Configuration
  • Troubleshooting
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Ask on Gitter
    • Ask on StackOverflow
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  • Migration scripts
  • Generators
  • Run the migrations
  • Drop and Create a database
  • Seed the database
  1. Guides
  2. Models
  3. Granite

Migrations

PreviousGraniteNextValidations

Last updated 7 years ago

This section is based on

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

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:

db/migrations/20171003212124_create_post.sql
-- +micrate Up
CREATE TABLE posts (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255),
  body TEXT,
  pages INT,
  published BOOL,
  created_at TIMESTAMP NULL,
  updated_at TIMESTAMP NULL
);

-- +micrate Down
DROP TABLE IF EXISTS posts;

Generators

Maze CLI provides the ability to generate migration scripts:

maze generate migration create_posts

This will generate an empty micrate script with the timestamp set in the filename:

db/migrations/20171003212124_create_post.sql

If you generate a model or scaffold, it will create a migration script that will create and drop the database table.

maze generate scaffold post name:string body:text pages:int published:bool

This will generate the migration script shown above.

Run the migrations

You can run the migrations using either:

maze db migrate

You can rollback a migration using either:

maze db rollback

Drop and Create a database

The maze db commmand also allows you to create and drop the database itself:

maze db drop create migrate

Seed the database

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

require "../config/application.cr"

user = User.new
user.email = "admin@example.com"
user.password = "password"
user.save

This example seeds.cr file creates an admin user.

You can execute the seeds using:

maze db seed
Granite's README
micrate