Migrations are used to build and modify your database tables. This is done through use of migration files and the
Schema class. Migration files are really just wrappers around the
Schema class as well as a way for Masonite to manage which migrations have run and which ones have not.
Creating migrations are easy with the migration commands. To create one simply run:
$ masonite-orm migration migration_for_users_table
This will create a migration file for you and put it in the
If you want to create a starter migration, that is a migration with some boilerplate of what you are planning to do, you can use the
$ masonite-orm migration migration_for_users_table --create users
This will setup a migration for you with some boiler plate on creating a new table
$ masonite-orm migration migration_for_users_table --table users
This will setup a migration for you for boiler plate on modifying an existing table.
To start building up your migration, simply modify the
up method and start adding any of the available methods below to your migration.
A simple example would look like this for a new table:
class MigrationForUsersTable(Migration):def up(self):"""Run the migrations."""with self.schema.create("users") as table:table.increments('id')table.string('username')table.string('email').unique()table.string('password')table.bool('is_admin')table.integer('age')table.timestamps()def down(self):"""Revert the migrations."""self.schema.drop("users")
The varchar version of the table. Can optional pass in a length
The INT version of the database. Can also specify a length
The auto incrementing version of the table. An unsigned non nullable auto incrementing integer.
An unsigned non nullable auto incrementing big integer. Use this if you expect the rows in a table to be very large
BINARY equivalent column. Sometimes is text field on unsupported databases.
BOOLEAN equivalent column.
CHAR equivalent column.
DATE equivalent column.
DATETIME equivalent column.
TIMESTAMP equivalent column.
DECIMAL equivalent column. Can also specify the length and decimal position.
DOUBLE equivalent column. Can also specify a float length
ENUM equivalent column. You can also specify available options as a list.
TEXT equivalent column.
UNSIGNED INT equivalent column.
In addition to the available columns you can use, you can also specify some modifers which will change the behavior of the column:
Allows NULL values to be inserted into the column.
Forces all values in the column to be unique.
Adds the column after another column in the table. Can be used like
Makes the column unsigned. Used with the
Makes the column use the
In addition to columns, you can also create indexes. Below are the available indexes you can create:
Make the column use the PRIMARY KEY modifer.
Makes a unique index. Can pass in a column
Creates an index on the column.
If you want to create a foreign key you can do so simply as well:
And optionally specify an
You can use these options:
Sets the ON UPDATE SET NULL property on the constraint.
Sets the ON UPDATE CASCADE property on the constraint.
Sets the ON DELETE SET NULL property on the constraint.
Sets the ON DELETE CASCADE property on the constraint.
In addition to building up the migration, you should also build onto the
down method which should reverse whatever was done in the
up method. If you create a table in the up method, you should drop the table in the down method.
DROP TABLE equivalent statement.
DROP TABLE IF EXISTS equivalent statement.
DROP COLUMN equivalent statement.
Drops the constraint. Must pass in the name of the constraint.
Drops the uniqueness constraint. Must pass in the name of the constraint.
Drops the foreign key. Must specify the index name.
Drops the primary key constraint. Must pass in the constraint name
Refreshing a database is simply rolling back all migrations and then migrating again. This "refreshes" your database.
You can refresh by running the command:
$ masonite-orm migrate:refresh
At any time you can get the migrations that have run or need to be ran:
$ masonite-orm migrate:status
There currently is no "change" functionality, yet. In order to change a column you currently will have to drop the column and then create a new one
class MigrationForUsersTable(Migration):def up(self):"""Run the migrations."""with self.schema.table("users") as table:table.drop_column('email')with self.schema.table("users") as table:table.string('email').unique()def down(self):"""Revert the migrations."""pass