Each version is a directory in the migrations directory. The version directory has one PHP file for each table. The name of the file is the table name, for example users.php.
Each file returns an object of an anonymous class, which extends Phalcon\Migrations\Mvc\Model\Migration. The TABLE_NAME constant of the class has the table name. The methods use self::TABLE_NAME in place of the table name. Any table name works, also a name with a space or a quote.
A generated file is similar to this (shortened) example:
<?php
declare(strict_types=1);
use Phalcon\Migrations\Db\Column;
use Phalcon\Migrations\Db\Index;
use Phalcon\Migrations\Db\Reference;
use Phalcon\Migrations\Mvc\Model\Migration;
// The migration of the table in TABLE_NAME
return new class extends Migration {
public const TABLE_NAME = 'users';
/**
* Define the table structure
*
* @return void
* @throws \Exception
*/
public function morph(): void
{
$this->morphTable(self::TABLE_NAME, [
'columns' => [
new Column(
'id',
[
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'autoIncrement' => true,
'first' => true
]
),
new Column(
'email',
[
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 255,
'comment' => "Login email",
'after' => 'id'
]
),
new Column(
'created_at',
[
'type' => Column::TYPE_TIMESTAMP,
'default' => "CURRENT_TIMESTAMP",
'notNull' => true,
'after' => 'email'
]
),
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY KEY'),
new Index('users_email_uindex', ['email'], 'UNIQUE'),
],
'references' => [],
'options' => [
'ENGINE' => 'InnoDB',
'TABLE_COLLATION' => 'utf8mb4_unicode_ci',
],
]);
}
/**
* Run the migrations
*
* @return void
*/
public function up(): void
{
}
/**
* Reverse the migrations
*
* @return void
*/
public function down(): void
{
}
};To put the generated classes in a namespace, use the --namespace option or the namespace key, for example --namespace=App\Migrations. The file then has a namespace App\Migrations; line. Files with a namespace and files with no namespace run together.
A migration file must not declare functions or named classes. The package can load a file more than one time in a run. A file that declares a named class is an old file (see below).
Old migration files
In version 4.0.1 and earlier, each file has a named class. The class name is the table name in CamelCase, then Migration_, then the version stamp, for example UsersMigration_100. These files run as before, also next to new files of the same table. migrate-files does not change them. To get the new format for a table, run generate again.
Columns
These rules apply to the column definitions of a generated file:
| Column | Rule |
|---|---|
| Integer | On MySQL 8.0.19 and later, generate writes no size. On MariaDB, the size is the display width, for example 11. The size of an integer is only a display width. The morph does not compare it |
Column::TYPE_BOOLEAN |
No size |
| JSON and JSONB | No size |
Column::TYPE_UUID |
PostgreSQL UUID. No size. MySQL and SQLite have no mapping for this type |
Column::TYPE_SET |
MySQL SET. The values are in 'options' => [...], as for Column::TYPE_ENUM |
'comment' |
Part of the column. The morph applies a change of only the comment. If the file has no comment for a column, the morph clears the comment of the live column. PostgreSQL writes the comment with COMMENT ON COLUMN. SQLite has no column comments |
'default' => "CURRENT_TIMESTAMP" |
generate writes a plain CURRENT_TIMESTAMP default (MySQL and PostgreSQL). It does not write a default with a precision, for example CURRENT_TIMESTAMP(6), or a different expression |
'onUpdate' => "CURRENT_TIMESTAMP" |
MySQL ON UPDATE CURRENT_TIMESTAMP. Only the plain form. The morph compares it |
'defaultExpression' => true |
PostgreSQL. The default is an expression, for example gen_random_uuid(). The SQL has the default with no quotes. generate sets it for a default in the form of a function call with no quotes. The morph does not compare it |
'primary' => true |
Written on the primary key columns on PostgreSQL and SQLite |
'autoIncrement' => true |
On SQLite, written for an AUTOINCREMENT column |
Renamed columns
The morph renames a column of the table when the file has a column with a new name at the place of that column, and the definition in the file is the same as the definition that the database gives for that column. The place is the column before it, or the column after it. The data of the column stays. MySQL, PostgreSQL and SQLite (3.25.0 and later) rename the column.
<?php
use Phalcon\Migrations\Db\Column;
// Version 1.0.0
$email = new Column('email', [
'type' => Column::TYPE_VARCHAR,
'size' => 255,
'notNull' => true,
'after' => 'id',
]);
// Version 1.0.1: the same definition with a new name. On MySQL and PostgreSQL, the morph renames email to login.
$login = new Column('login', [
'type' => Column::TYPE_VARCHAR,
'size' => 255,
'notNull' => true,
'after' => 'id',
]);The morph compares the file with the definition that the database reads. A file that generate wrote has this definition. A definition that you wrote can be different. For example:
- SQLite creates a
Column::TYPE_VARCHARcolumn asTEXT, and reads it asColumn::TYPE_TEXTwith no size. For this reason, the example above does not rename on SQLite. - For a
TEXTcolumn, MySQL gives the size65535, and SQLite gives no size.
If the definition is not the same, the morph adds the new column and drops the old column. The data of the old column is lost. On SQLite and PostgreSQL, a new NOT NULL column with no default stops the run when the table has rows.
To rename a column, rename it in your development database (ALTER TABLE ... RENAME COLUMN), then run generate. Change the definition of the column in a different version.
UUID primary keys
The package does not make UUID values. Use one of these two methods for a UUID primary key.
A default of the database. On PostgreSQL, use DEFAULT gen_random_uuid(), or uuid_generate_v4() with the uuid-ossp extension. generate writes the column like this:
<?php
use Phalcon\Migrations\Db\Column;
new Column(
'id',
[
'type' => Column::TYPE_UUID,
'default' => "gen_random_uuid()",
'defaultExpression' => true,
'primary' => true,
'notNull' => true,
'first' => true
]
);A value from the application. Use Phalcon\Encryption\Security\Uuid. Set the value before the insert, for example in the beforeValidationOnCreate() method of the model. The column then needs no default. This works on MySQL, PostgreSQL and SQLite. v7() gives time-ordered values, which are better for indexes than the random values of v4().
<?php
use Phalcon\Encryption\Security\Uuid;
use Phalcon\Mvc\Model;
class Invoices extends Model
{
public string $id;
public function beforeValidationOnCreate(): void
{
$this->id = (string) (new Uuid())->v7();
}
}Indexes and foreign keys
A generated file always has the indexes and references keys, also when the lists are empty. The morph changes the indexes and the foreign keys only when the file has the key:
- With the key, the morph adds, changes and removes indexes or foreign keys, so that the table matches the file.
- With no key, for example in a file that you wrote, the morph does not change them.
Before the morph drops a column, it drops the foreign keys that use that column.
Table options
generate writes the options of the table, for example ENGINE, TABLE_COLLATION, AUTO_INCREMENT and, if the MySQL table sets it, ROW_FORMAT. The options apply only when the morph creates the table. The morph does not change the options of a table that exists.
Files from version 4.0.1 or earlier
Some migration files from version 4.0.1 or earlier do not have all the data of the table. Generate again the versions that did not run yet in these conditions:
- The table has a PostgreSQL
uuidcolumn. The old file hasColumn::TYPE_VARCHARwith'size' => 1. - The table has a
CURRENT_TIMESTAMPdefault. The old file has no default, so its run removes the default from the live column. - The table has a MySQL
ON UPDATE CURRENT_TIMESTAMPcolumn. The old file has noonUpdate, so its run removes it from the live column. - The table has a PostgreSQL default that is an expression, for example
gen_random_uuid(). The old file has nodefaultExpression, so a new table gets the default as a text. - The table is a SQLite table. The old file has no primary key, so a table that the file creates has no primary key.