Skip to content

Quick Start

Create the configuration, then generate, run, roll back and list the migrations.

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 generate

Generate a specific table and export data from it

vendor/bin/phalcon-migrations generate \
    --config=migrations.php \
    --table=users \
    --data=oncreate

The --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*

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 version

The compare ignores the version and 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 run

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.1

The 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 list

The command shows each version and if it was applied:

┌───────────────────────────┬──────────────┐
│ Version                   │  Was applied │
├───────────────────────────┼──────────────┤
│ 1.0.0                     │            Y │
│ 1.0.1                     │            N │
└───────────────────────────┴──────────────┘
Navigation

Type to search…

↑↓ navigate↵ selectEsc close