Database migrations

:bulb: It is recommended to keep structural (schema) and data-only migrations separated.


  • Create a file-migration

    $ yii migrate/create \
        --templateFile=@dmstr/db/mysql/templates/file-migration.php \
        --migrationPath=@project/migrations/dev-data \
  • Create the database export, which is basically an adjusted dump

     $ yii db/export --outputPath=@project/migrations/dev-data

    :exclamation: The dump truncates all exported tables by default

  • Create a migration

     $ yii file:migrate/create dev_data
  • Adjust public $file = '<NAME_OF_YOUR_EXPORTED_FILE>.sql'; in the newly created migration.

  • For development add the development-data migrations via ENV variable in


Additional information

If you create initial data for the application, which is required and should always be inserted in inital setup, do not forget to add the migration path to the configuration

'params' => [
    'yii.migrations' => [

Lookup paths for migrations can be defined in application configuration, for details see dmstr/yii2-migrate-command.

'params' => [
    'yii.migrations' => [

Advanced topics

Pre-configured command alias

Configure migrate command with predefined values use only for creating file migrations

'controllerMap' => [
    'file:migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'templateFile' => '@vendor/dmstr/yii2-db/db/mysql/templates/file-migration.php',
        'migrationPath' => '@project/migrations/dev-data',



Help us to improve the documentation, fork this page.