Migrations are only available if our application uses the .

    Migrations are Ruby files stored by default under . Their name is composed by a UTC timestamp and a snake case name (eg db/migrations/20150621165604_create_books.rb).

    We use a create_table block to define the schema of that table.

    The first line is primary_key :id, which is a shortcut to create an autoincrement integer column.

    Then we have three lines for columns. The first argument that we pass to column is the name, then the type. The type can be a Ruby type such as String or Integer or a string that represents the native database type (eg. "varchar(32)" or ).

    As a last optional argument there is a Hash that specifies some extra details for the column. For instance NULL or uniqueness constraints, the size (for strings) or the default value.

    The final line defines a database check to ensure that price will always be greater than zero.

    When we “migrate” a database we are going into an “up” direction because we’re adding alterations to it. Migrations modifications can be rolled back (“down” direction).

    Imagine we have the following code:

    When we use create_table, Hanami::Model will use drop_table in case we want to rollback this migration.

    In case we want to have concrete code for our “down” policy, we can use and down blocks.

    To learn how to use migrations in command line, please have a look at this article.