Yii DB Migration

dev-master / 1.0.x-dev 2023-06-04 07:23 UTC



Yii DB Migration

The package implementing migration for yiisoft/db.

Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage


The package could be installed via composer:

composer require yiisoft/yii-db-migration --prefer-dist

Note: You must install the repository of the implementation to use.


composer require yiisoft/db-sqlite --prefer-dist


  • PHP 8.0 or higher.
  • Filter PHP extension.


Example using yiisoft/app.


Create config/common/db.php with content:



use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Sqlite\Connection as SqliteConnection;

return [
    ConnectionInterface::class => [
        'class' => SqliteConnection::class,
        '__construct()' => [
            'dsn' => 'sqlite:' . __DIR__ . '/Data/yiitest.sq3'

Add to config/params.php:

'yiisoft/yii-db-migration' => [
    'createNamespace' => 'App\\Migration',
    'updateNamespaces' => ['App\\Migration'],

Now the MigrationService::class uses the View of the application that is already registered in yiisoft/view.

Execute composer du in console config its rebuild.

Now we have the yiisoft/yii-db-migration package configured and it can be called in the console.

View the list of available commands execute in console: ./yii list

Available commands:
  database:list    Lists all tables in the database.
  migrate:create  Generate migration template.
  help             Displays help for a command
  list             Lists commands
  migrate:down     Downgrades the application by reverting old migrations.
  migrate:history  Displays the migration history.
  migrate:new      Displays the first 10 new migrations.
  migrate:redo     Redoes the last few migrations.
  migrate:up       Upgrades the application by applying new migrations.
  serve            Runs PHP built-in web server

Help simple command execute in console ./yii migrate:create --help.

  Generate migration template.

  migrate:create [options] [--] <name>

  name                         Table name for generate migration.

  -c, --command[=COMMAND]      Command to execute. [default: "create"]
  -f, --fields[=FIELDS]        To create table fields right away
      --and[=AND]              And junction
      --namespace[=NAMESPACE]  Namespace migration
  -h, --help                   Display this help message
  -q, --quiet                  Do not output any message
  -V, --version                Display this application version
      --ansi                   Force ANSI output
      --no-ansi                Disable ANSI output
  -n, --no-interaction         Do not ask any interactive question
  -v|vv|vvv, --verbose         Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

  This command Generate migration template


Unit testing

The package is tested with PHPUnit. For tests need PostgreSQL database with configuration:

  • host:
  • port: 5432
  • name: yiitest
  • user: root
  • password: root

Docker Image

To easily set up a pre-configured PostgreSQL instance for testing you can use the docker-compose.yml file in this repository.

For running the docker containers you can use the following command:

docker compose up -d

To run tests:


Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:


Static analysis

The code is statically analyzed with Psalm. To run static analysis:



The Yii Db Migration is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack