What you need for a quick start:
- Configuration file in the root of your project (you can also pass them as parameters inside CLI environment)
- Create database table structure
- Execute command to generate migrations
After that, you can execute that migration (run) in another environment to create the same DB structure.
Create the configuration file
The configuration file is a PHP file that returns an array. You can use any file name, for example migrations.php.
<?php
return [
'database' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'root',
'password' => '',
'dbname' => 'db-name',
],
'application' => [
'log-in-db' => true,
'migrations' => 'db/migrations',
// true - TIMESTAMP, false - versions
'ts-based' => true,
'export-data-from-tables' => [
// Tables names
],
],
];For all the configuration keys, see Configuration.
Generate migrations
Basic generation
vendor/bin/phalcon-migrations generateGenerate a specific table and export data from it
vendor/bin/phalcon-migrations generate \
--config=migrations.php \
--table=users \
--data=oncreateThe --table option accepts:
- One table name:
--table=users - A comma-separated list of table names:
--table=users,roles - A table prefix with an asterisk:
--table=user*
The package removes the spaces at the start and at the end of each name. For example, --table="users, roles" gives the tables users and roles. This also applies to --export-data-from-tables and export-data-from-tables.
For the data options, see Export data.
generate does not write a migration for the SQLite internal tables, for example sqlite_sequence, or for the PostgreSQL tables of an extension, for example spatial_ref_sys of PostGIS.
generate compares each table with the newest migration file of that table, in all the migrations directories. If no table changed, generate creates no version and shows this message:
Info: Nothing to generate: no table changed since the last versionThe compare ignores the MySQL AUTO_INCREMENT value. If one table changed, generate writes the files of all the tables. With --data or --export-data-from-tables, generate always creates a version.
Run migrations
vendor/bin/phalcon-migrations runWith --table, run migrates only the named tables of each version, but it adds the full version to the log. A later run, with or without --table, does not run the other tables of that version. It shows Version 1.0.1 was already applied. To run the other tables of the version, remove the version from the log, or roll back the version.
Use --table when one migrations directory has the migrations of more than one database, and each database uses its own tables and its own log.
If a version of the log has no version directory, for example a version of a different branch, run shows a warning for that version:
Warning: Version 1.0.5 is in the log, but it has no folder: run does not start from itrun then starts from the highest version of the log that has a version directory.
Roll back migrations
To roll back, use run with --version. Set --version to a version that is earlier than the current version:
vendor/bin/phalcon-migrations run --version=1.0.1The package goes back one version at a time. For each table, it calls the down() and afterDown() methods of the version that it removes. Then it calls the morph() method of the previous version. See Migration methods.
List existing migrations
vendor/bin/phalcon-migrations listThe command shows each version and if it was applied:
┌───────────────────────────┬──────────────┐
│ Version │ Was applied │
├───────────────────────────┼──────────────┤
│ 1.0.0 │ Y │
│ 1.0.1 │ N │
└───────────────────────────┴──────────────┘