Commands

Phinx is run using a number of commands.

Migration Commands

The Init Command

The Init command (short for initialize) is used to prepare your project forPhinx. This command generates the phinx.yml file in the root of yourproject directory.

  1. $ cd yourapp
  2. $ phinx init .

Open this file in your text editor to setup your project configuration. Pleasesee the Configuration chapter for more information.

The Create Command

The Create command is used to create a new migration file. It requires oneargument: the name of the migration. The migration name should be specified inCamelCase format.

  1. $ phinx create MyNewMigration

Open the new migration file in your text editor to add your databasetransformations. Phinx creates migration files using the path specified in yourphinx.yml file. Please see the Configuration chapterfor more information.

You are able to override the template file used by Phinx by supplying analternative template filename.

  1. $ phinx create MyNewMigration --template="<file>"

You can also supply a template generating class. This class must implement theinterface Phinx\Migration\CreationInterface.

  1. $ phinx create MyNewMigration --class="<class>"

In addition to providing the template for the migration, the class can also definea callback that will be called once the migration file has been generated from thetemplate.

You cannot use —template and —class together.

The Migrate Command

The Migrate command runs all of the available migrations, optionally up to aspecific version.

  1. $ phinx migrate -e development

To migrate to a specific version then use the —target parameter or -tfor short.

  1. $ phinx migrate -e development -t 20110103081132

Use —dry-run to print the queries to standard output without executing them

  1. $ phinx migrate --dry-run

The Rollback Command

The Rollback command is used to undo previous migrations executed by Phinx. Itis the opposite of the Migrate command.

You can rollback to the previous migration by using the rollback commandwith no arguments.

  1. $ phinx rollback -e development

To rollback all migrations to a specific version then use the —targetparameter or -t for short.

  1. $ phinx rollback -e development -t 20120103083322

Specifying 0 as the target version will revert all migrations.

  1. $ phinx rollback -e development -t 0

To rollback all migrations to a specific date then use the —dateparameter or -d for short.

  1. $ phinx rollback -e development -d 2012
  2. $ phinx rollback -e development -d 201201
  3. $ phinx rollback -e development -d 20120103
  4. $ phinx rollback -e development -d 2012010312
  5. $ phinx rollback -e development -d 201201031205
  6. $ phinx rollback -e development -d 20120103120530

If a breakpoint is set, blocking further rollbacks, you can override thebreakpoint using the —force parameter or -f for short.

  1. $ phinx rollback -e development -t 0 -f

Use —dry-run to print the queries to standard output without executing them

  1. $ phinx rollback --dry-run

Note

When rolling back, Phinx orders the executed migrations usingthe order specified in the version_order option of yourphinx.yml file.Please see the Configuration chapter for more information.

The Status Command

The Status command prints a list of all migrations, along with their currentstatus. You can use this command to determine which migrations have been run.

  1. $ phinx status -e development

This command exits with code 0 if the database is up-to-date (ie. all migrations are up) or one of the following codes otherwise:

  • 1: There is at least one migration left to be executed.
  • 2: There was a migration run and recorded in the database, but the migration file is now missing.

The Breakpoint Command

The Breakpoint command is used to set breakpoints, allowing you to limitrollbacks. You can toggle the breakpoint of the most recent migration bynot supplying any parameters.

  1. $ phinx breakpoint -e development

To toggle a breakpoint on a specific version then use the —targetparameter or -t for short.

  1. $ phinx breakpoint -e development -t 20120103083322

You can remove all the breakpoints by using the —remove-all parameteror -r for short.

  1. $ phinx breakpoint -e development -r

Breakpoints are visible when you run the status command.

Database Seeding

The Seed Create Command

The Seed Create command can be used to create new database seed classes. Itrequires one argument, the name of the class. The class name should be specifiedin CamelCase format.

  1. $ phinx seed:create MyNewSeeder

Open the new seed file in your text editor to add your database seed commands.Phinx creates seed files using the path specified in your phinx.yml file.Please see the Configuration chapter for more information.

The Seed Run Command

The Seed Run command runs all of the available seed classes or optionally justone.

  1. $ phinx seed:run -e development

To run only one seed class use the —seed parameter or -s for short.

  1. $ phinx seed:run -e development -s MyNewSeeder

Configuration File Parameter

When running Phinx from the command line, you may specify a configuration fileusing the —configuration or -c parameter. In addition to YAML, theconfiguration file may be the computed output of a PHP file as a PHP array:

  1. <?php
  2. return [
  3. "paths" => [
  4. "migrations" => "application/migrations"
  5. ],
  6. "environments" => [
  7. "default_migration_table" => "phinxlog",
  8. "default_database" => "dev",
  9. "dev" => [
  10. "adapter" => "mysql",
  11. "host" => $_ENV['DB_HOST'],
  12. "name" => $_ENV['DB_NAME'],
  13. "user" => $_ENV['DB_USER'],
  14. "pass" => $_ENV['DB_PASS'],
  15. "port" => $_ENV['DB_PORT'],
  16. ]
  17. ]
  18. ];

Phinx auto-detects which language parser to use for files with .yml and .php extensions. The appropriateparser may also be specified via the —parser and -p parameters. Anything other than "php" is treated as YAML.

When using a PHP array, you can provide a connection key with an existing PDO instance. It is also important to passthe database name too, as Phinx requires this for certain methods such as hasTable():

  1. <?php
  2. return [
  3. "paths" => [
  4. "migrations" => "application/migrations"
  5. ],
  6. "environments" => [
  7. "default_migration_table" => "phinxlog",
  8. "default_database" => "dev",
  9. "dev" => [
  10. "name" => "dev_db",
  11. "connection" => $pdo_instance
  12. ]
  13. ]
  14. ];

Running Phinx in a Web App

Phinx can also be run inside of a web application by using the Phinx\Wrapper\TextWrapperclass. An example of this is provided in app/web.php, which can be run as astandalone server:

  1. $ php -S localhost:8000 vendor/robmorgan/phinx/app/web.php

This will create local web server at http://localhost:8000 which will show currentmigration status by default. To run migrations up, use http://localhost:8000/migrateand to rollback use http://localhost:8000/rollback.

The included web app is only an example and should not be used in production!

Note

To modify configuration variables at runtime and override %%PHINX_DBNAME%%or other another dynamic option, set $_SERVER['PHINX_DBNAME'] beforerunning commands. Available options are documented in the Configuration page.

Using Phinx with PHPUnit

Phinx can be used within your unit tests to prepare or seed the database. You can use it programatically :

  1. public function setUp ()
  2. {
  3. $app = new PhinxApplication();
  4. $app->setAutoExit(false);
  5. $app->run(new StringInput('migrate'), new NullOutput());
  6. }

If you use a memory database, you’ll need to give Phinx a specific PDO instance. You can interact with Phinx directly using the Manager class :

  1. use PDO;
  2. use Phinx\Config\Config;
  3. use Phinx\Migration\Manager;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\Console\Input\StringInput;
  6. use Symfony\Component\Console\Output\NullOutput;
  7.  
  8. class DatabaseTestCase extends TestCase {
  9.  
  10. public function setUp ()
  11. {
  12. $pdo = new PDO('sqlite::memory:', null, null, [
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  14. ]);
  15. $configArray = require('phinx.php');
  16. $configArray['environments']['test'] = [
  17. 'adapter' => 'sqlite',
  18. 'connection' => $pdo
  19. ];
  20. $config = new Config($configArray);
  21. $manager = new Manager($config, new StringInput(' '), new NullOutput());
  22. $manager->migrate('test');
  23. $manager->seed('test');
  24. // You can change default fetch mode after the seeding
  25. $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  26. $this->pdo = $pdo;
  27. }
  28.  
  29. }