Commands
Masonite ORM comes with several terminal commands you can run to speed up your development. Here are a list of commands and their descriptions
Below are examples to use Masonite ORM standalone but if you are using Masonite ORM with Masonite then you can replace the calls to masonite-orm
with your applications python craft
file.
For example you would replace the call for:
with
Migrations
Migration commands are used to create migration files, roll them back and refresh your database
Creating
You can create migration files easily. Migration files will create a class with an up
method and a down
method. You should perform your schema logic in the up
method and then reverse what you did in the down
method.
Argument | Description | Example |
---|---|---|
| The name of the migration file to create |
|
Options | Description | Example |
---|---|---|
| Makes a migration file for creating a new table |
|
| Makes a migration file for altering an existing table |
|
| Specifies where the migration directory is |
|
Migrating
Migrating will run each unmigrated migration's up
method. Each group of migrations that are ran will create a batch number and store information in the migrations
table. The batch number will allow groups of migrations to be rolled back if needed.
Options | Description | Example |
---|---|---|
| Specify a specific migration to rollback. This will default to rolling back the previous batch of migrations. |
|
| The name of the connection to use to rollback the migrations |
|
| If passed this command will output the SQL that will run to modify the schema and will not actually run the SQL. |
|
| If the |
|
| Specifies where the migration directory is |
|
Rollback
You can rollback your migration files as well. This would run your migration files that are already migrated but in reverse order. This will run the down
method on each migration file in reverse order to "undo" the migration changes that were previously ran.
When a group of migrations are migrated, that group will create a batch number. The rollback command will only rollback the last batch, or the migrations that were ran in the last group of migrations that you ran.
Options | Description | Example |
---|---|---|
| Specify a specific migration to rollback. This will default to rolling back the previous batch of migrations. |
|
| The name of the connection to use to rollback the migrations |
|
| If passed this command will output the SQL that will run to modify the schema and will not actually run the SQL. |
|
| Specifies where the migration directory is |
|
Resetting
While the rollback method will rollback the previous batch of migrations, the reset command will rollback all migrations.
Options | Description | Example |
---|---|---|
| Specify a specific migration to rollback. This will default to rolling back the previous batch of migrations. |
|
| The name of the connection to use to rollback the migrations |
|
| Specifies where the migration directory is |
|
Refreshing
Refreshing migrations is a simple way to redo all of your migrations. First it will rollback all migrations and then automatically migrate all the migrations again.
Options | Description | Example |
---|---|---|
| Specify a specific migration to refresh. This will default to refreshing everything. |
|
| The name of the connection to use to refresh the migrations |
|
| Whether the seed:run command should be ran after the database schema is refreshed |
|
| Specifies where the migration directory is |
|
| Specifies where the seed directory is |
|
Getting Migration Status
Sometimes its good to know the status of the migrations so you can know if you have any migrations that need to be ran:
Options | Description | Example |
---|---|---|
| The name of the connection to use to refresh the migrations |
|
| Specifies where the migration directory is |
|
Creating Models
You can create a new model class quickly. There are also several options you can pass to this command to quickly create migrations and seeds quickly.
Argument | Description | Example |
---|---|---|
| The name of the model to create |
|
Options | Description | Example |
---|---|---|
| Specify a specific migration to refresh. This will default to refreshing everything. |
|
| Whether to create a migration file as well |
|
| Whether the seed:run command should be ran after the database schema is refreshed |
|
| Whether the created migration should have the create option. |
|
| Whether the created migration should have the table option. |
|
| Whether the created file should follow pep8 |
|
| Which file the model should be created in |
|
| The directory of the migrations. Use this option if using the |
|
| The directory of the seeding classes. Use this option if using the |
|
Model Docstrings
One of the downsides of Masonite ORM compared to other models is you don't know what columns and data types you have on your models / tables.
For example, on other ORMs, columns are class attributes on your models so you can always reference your models to know what your tables look like.
To solve this with Masonite ORM you can use the model:docstring
command. This command will output an example docstring of all your tables columns and their data types so you can put it on your model for reference. When you make schema changes you can rerun this command to get the updated schema.
Another downside is not being able to see IDE type hints on your models. This is also solved using the --type-hints
option you can find below.
Argument | Description | Example |
---|---|---|
| The name of the table you want to create the docstring for |
|
Options | Description | Example |
---|---|---|
| Used to also optionally output type hints you can add to your model class that your IDE can use to assist in column type hinting |
|
| The name of the connection to use to use. |
|
Make Observers
You can easily build observer files:
Argument | Description | Example |
---|---|---|
| The name of the observer to create |
|
Options | Description | Example |
---|---|---|
| Name of the model to build the observer for |
|
| The location of the observers directory. |
|
Seeding
Seeding is a great way to get test data into your database.
Making a Seed File
To make a seed file is simple:
Argument | Description | Example |
---|---|---|
| The table of the seed to create |
|
Options | Description | Example |
---|---|---|
| The location of the seeds directory. |
|
Running Seeds
To run the seeds is simple:
Options | Description | Example |
---|---|---|
| The name of the connection to use to refresh the migrations |
|
| If the seed should run in dry mode |
|
| The name of the table to seed |
|
| The location of the seeds directory. |
|
Last updated