Run migrations from specific migrations directory
<?php
use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Utils\Config;
$config = Config::fromArray([
'database' => [
'adapter' => 'mysql',
'host' => 'phalcon-db-mysql',
'username' => 'root',
'password' => 'root',
'dbname' => 'vokuro',
],
]);
Migrations::run([
'directory' => __DIR__,
'migrationsDir' => [
__DIR__ . '/migrations',
],
'config' => $config,
'migrationsInDb' => true,
]);The config value must be a Phalcon\Migrations\Utils\Config object. Use Config::fromArray() to create it from an array with the database and application sections. If you use a plain array, Migrations::run() throws an exception with the message The option config must be an object of Phalcon\Migrations\Utils\Config (Config::fromArray()).
Migrations::run() does not read the log-in-db key of the configuration. Use the migrationsInDb key.
Migrations::run() accepts these options:
| Key | Type | Description |
|---|---|---|
config |
Config |
Database configuration (required) |
migrationsDir |
array / string |
Migrations directories (required) |
directory |
string |
Project directory, which holds the file log. Default: the BASE_PATH constant, or the parent directory of APP_PATH. Set it if you do not define these constants |
version |
string |
Version to migrate to. Default: the latest version |
tableName |
string |
Table name, comma-separated table names or a prefix with an asterisk. Default: @ (all tables) |
tsBased |
bool |
true - use timestamp based versions |
migrationsInDb |
bool |
true - keep the migrations log in the database. false - keep it in a file. If you do not set it, the package uses the file log and gives a deprecation notice |
log-path |
string |
Path of the log file (file log). Default: .phalcon/migration-version in the project directory |
log-table |
string |
Name of the log table (database log). Default: phalcon_migrations |
verbose |
bool |
true - show the SQL that the package sends. See The verbose key |
skip-foreign-checks |
bool |
true - disable the foreign key checks during the run |
truncate |
bool |
true - empty each table before batchInsert() imports its data |
Other actions in PHP
The other actions also have a static method in Phalcon\Migrations\Migrations. Each method needs the config key, with a Phalcon\Migrations\Utils\Config object.
Each method starts with the default state of the package. Thus one PHP process can call more than one method, for example Migrations::run() and then Migrations::listAll(), and a method does not use the options of an earlier call.
Migrations::generate()
| Key | Type | Description |
|---|---|---|
config |
Config |
Database configuration (required) |
migrationsDir |
array / string |
Migrations directories (required) |
tableName |
string |
Table name, comma-separated table names or a prefix with an asterisk. @ - all the tables (required) |
version |
string |
Version to generate. Default: the next version |
force |
bool |
true - overwrite the files of an existing version |
tsBased |
bool |
true - generate a timestamp based version |
descr |
string |
Description of a timestamp based version. Use it with tsBased |
exportData |
string |
always or oncreate. Other values throw an exception. See Export data |
exportDataFromTables |
array |
Tables to export data from |
noAutoIncrement |
bool |
true - leave out the AUTO_INCREMENT value of the table |
skip-ref-schema |
bool |
true - leave out the schema of the referenced table in the foreign keys |
namespace |
string |
Namespace of the generated migration classes |
log-table |
string |
Name of the log table, which generate skips. Default: phalcon_migrations |
verbose |
bool |
true - dry run. generate writes no files. See The verbose key |
Migrations::listAll()
| Key | Type | Description |
|---|---|---|
config |
Config |
Database configuration (required) |
migrationsDir |
array / string |
Migrations directories (required) |
tsBased |
bool |
true - use timestamp based versions |
listAll() also accepts the directory, migrationsInDb, log-path and log-table keys of Migrations::run().
Migrations::clean()
| Key | Type | Description |
|---|---|---|
config |
Config |
Database configuration (required) |
tableName |
string |
Comma-separated table names. Default: @ (all the tables) |
data-only |
bool |
true - delete the rows and keep the tables |
clean() also accepts the directory, migrationsInDb, log-path and log-table keys of Migrations::run().
Migrations::export()
| Key | Type | Description |
|---|---|---|
config |
Config |
Database configuration (required) |
migrationsDir |
array / string |
Migrations directories (required) |
version |
string |
Version to export. Default: the newest version |
tableName |
string |
Comma-separated table names. Default: @ (all the tables) |
tsBased |
bool |
true - use timestamp based versions |
export() prints the SQL. See Export the schema.
<?php
use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Utils\Config;
$config = Config::fromArray([
'database' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'shop',
],
]);
Migrations::generate([
'config' => $config,
'migrationsDir' => [__DIR__ . '/db/migrations'],
'tableName' => 'invoices,customers',
]);The verbose key
The verbose key has two meanings in the PHP API:
| Method | 'verbose' => true |
CLI option |
|---|---|---|
Migrations::run() |
Shows the SQL that the package sends, also the SQL of the database log | --verbose |
Migrations::generate() |
Dry run: generate writes no files. It also shows the SQL that the package sends |
--dry |
To see the SQL of generate and get the files, you cannot use verbose. A call with 'verbose' => true writes no files.
The package shows the SQL only when PHP runs on the command line (PHP_SAPI is cli).
<?php
use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Utils\Config;
$config = Config::fromArray([
'database' => [
'adapter' => 'mysql',
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'shop',
],
]);
// Shows the SQL of the run
Migrations::run([
'config' => $config,
'migrationsDir' => [__DIR__ . '/db/migrations'],
'migrationsInDb' => true,
'verbose' => true,
]);
// A dry run: no files
Migrations::generate([
'config' => $config,
'migrationsDir' => [__DIR__ . '/db/migrations'],
'tableName' => '@',
'verbose' => true,
]);