Database Migrations

Package git repository

Requirements

  • PHP >= 7.2
  • Phalcon >= 4.0.5

Installing via Composer

composer require --dev phalcon/migrations

Quick start

What you need for quick start:

  • Configuration file in root of your project (you can also pass them as parameters inside CLI environment)
  • Create database tables structure
  • Execute command to generate migrations

After that you can execute that migrations (run) in another environment to create same DB structure.

Create configuration file

<?php  use Phalcon\Config;  return new Config([  'database' => [  'adapter' => 'mysql',  'host' => '127.0.0.1',  'username' => 'root',  'password' => '',  'dbname' => 'db-name',  'charset' => 'utf8',  ],  'application' => [  'logInDb' => true,  'migrationsDir' => 'db/migrations',  'migrationsTsBased' => true, // true - Use TIMESTAMP as version name, false - use versions  'exportDataFromTables' => [  // Tables names  // Attention! It will export data every new migration  ],  ], ]);

Generate migrations

Basic generationvendor/bin/phalcon-migrations generate

**Generate specific table and export data from itvendor/bin/phalcon-migrations generate \  --config=migrations.php \  --table=users \  --exportDataFromTables=users \  --data=oncreate

Run migrations

vendor/bin/phalcon-migrations run

List existing migrations

vendor/bin/phalcon-migrations list

Usage example

Run migrations from specific migrations directoryuse Phalcon\Migrations\Migrations;  $migration = new Migrations(); $migration::run([  'migrationsDir' => [  __DIR__ . '/migrations',  ],  'config' => [  'database' => [  'adapter' => 'Mysql',  'host' => 'phalcon-db-mysql',  'username' => 'root',  'password' => 'root',  'dbname' => 'vokuro',  ],  ] ]);

Migration methods

Each migration is an separate class that works as an entity for specific database table. Inside each class there are different methods that can occur during migration running.

Each migration file (and class) can implement specific methods, that will be executed based on the operation requested. There are no restrictions on the logic encapsulated in each method.

The tables below show the Migration Class methods. They are stored by order of execution, earliest to latest.

Running to up

Method nameDescription
morphMorph table structure
afterCreateTableMake something immediately after table was created
upTable is created and ready to work with
afterUpExtra method to work for some specific cases

Running to down

Method nameDescription
downNormally you put here table drop or data truncation
aferDownExtra method to work after all was cleaned up
morph (from previous migration)As migration was moved backward, there need to be all returned to previous state

CLI Arguments and options

Arguments

ArgumentDescription
generateGenerate a Migration
runRun a Migration
listList all available migrations

Options

ActionDescription
–config=sConfiguration file
–migrations=sMigrations directory. Use comma separated string to specify multiple directories
–directory=sDirectory where the project was created
–table=sTable to migrate. Table name or table prefix with asterisk. Default: all
–version=sVersion to migrate
–descr=sMigration description (used for timestamp based migration)
–data=sExport data [‘always’ or ‘oncreate’] (Data is imported during migration run)
–exportDataFromTables=sExport data from specific tables, use comma separated string.
–forceForces to overwrite existing migrations
–ts-basedTimestamp based migration version
–log-in-dbKeep migrations log in the database table rather then in file
–dryAttempt requested operation without making changes to system (Generating only)
–verboseOutput of debugging information during operation (Running only)
–no-auto-incrementDisable auto increment (Generating only)
–skip-ref-schemaSkip referencedSchema inside generated migration (Generating only)
–skip-foreign-checksWrap SET FOREIGN_KEY_CHECKS query before and after execution of a query (Running only)
–helpShows this help

Timestamp based migrations

Using this approach is useful when more than one developer is participating in the database structure management. Use 'migrationsTsBased' => true in config file or --ts-based option in CLI environment. Also, you need to specify suffix descr, which could be anything you want, for example: semantic version.

Current commandvendor/bin/phalcon-migrations generate --ts-based --descr=1.0.0

Will produce folder name with such names

  • 1582539287636860_1.0.0
  • 1682539471102635_1.0.0
  • 1782539471102635_1.0.0

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *