Database Migrations

A properly formed database schema is an integral part to the quality of any software product, hence Apex highly encourages writing your SQL database schemas in SQL. Plus with a proper database schema, Apex can automatically generate model classes including relationships. If you are not familiar with SQL, don't fret as it's very easy, and you may view the article How to Write Proper SQL Database Schemas which will take you through everything you need to know.

If you are resistant to the idea of using SQL, Apex does have full support for Eloquent and Doctrine migrations as well.

Package Installation Migration

Every package has one main migration that is executed during installation / removal, and is located at ~/etc/PACKAGE/migrate.php. As you will notice, there are four methods within this class -- install(), remove(), reset(), resetRedis().

When the package is initially installed, the install() method will be executed. By default, this method simply executes any SQL code found within the install.sql file of this directory.

Create New Migrations

You may create a new migration with the CLI command:

./apex migrate create <PKG_ALIAS>> [<ALIAS>]

For example:

./apex migration create my-shop adding_reccuring_payments`

This will create a new directory at `~/etc/MyShop/Migrations/

adding_recurring_payments_1627500836