A
A
Artyom Alexandrov2017-05-03 05:53:27
Yii
Artyom Alexandrov, 2017-05-03 05:53:27

Yii2. Freestanding Migrations. Why are unspecified migrations clinging?

common/config/main.php

return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],

    'bootstrap' => [
        'user-manager',
    ],

    'modules' => [
    	'user-manager' => [
    		'class' => 'common\modules\UserManager\Module',
            'plugins' => ['profile'],
    	],
    ],

    'controllerMap' => [
        'migrate-app' => [
            'class' => 'yii\console\controllers\MigrateController',
            //'migrationNamespaces' => [''],
            'migrationTable' => 'migration',
        ],
    ],
];

common/modules/UserManager/Module.php
class Module extends \yii\base\Module
{
    public $plugins = [];

    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'common\modules\UserManager\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        $plugins = [];
        if(isset($this->plugins) && !empty($this->plugins)){
            $this->plugins = array_flip($this->plugins);
            
            if(isset($this->plugins['profile'])){
                $plugins['profile'] = [
                    'class' => 'common\modules\UserManager\modules\Profile\Module'
                ];
            }
        }
        $this->modules = $plugins;
        if(\Yii::$app instanceof \yii\console\Application){
            $migName[] = 'common\modules\UserManager\migrations';
            if(isset($this->plugins['profile'])){
                $migName[] = 'common\modules\UserManager\modules\Profile\migrations';
            }

            \Yii::$app->controllerMap['migrate-user-manager'] = [
                'class' => 'yii\console\controllers\MigrateController',
                'migrationNamespaces' => $migName,
                'migrationTable' => 'migration_user_manager',
            ];
        }
    }
}

When commanded in the console:
./yii migrate-user-manager
Yii prompts you to run all migrations, even those that are not specified for this command.
Yii Migration Tool (based on Yii v2.0.11.2)

Total 3 new migrations to be applied:
  m130524_201442_init
  common\modules\UserManager\migrations\m170424_124532_user_manager_init
  common\modules\UserManager\modules\Profile\migrations\m170502_154236_user_manager_profile

Apply the above migrations? (yes|no) [no]:n

Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-05-03
@qonand

oh something you were wiser. Read better here how to organize stand-alone migrations

A
Artyom Aleksandrov, 2017-05-03
@kradwhite

I got into the sources and found that in the BaseMigrateController class, migrationNamespace is converted into paths to folders with migration files and merged into one array with migrationPath (which is '@app/migrations' by default). And accordingly, all migrations from this folder cling to the same heap with namespace migrations. And if you have different tables for storing module and application migrations, then migrations from the default folder are considered not completed. Either they forgot to add a check to not use migrationPath if migrationNamspace is specified, or this was done intentionally for some reason. I will not understand further...
Add the treatment tool to the migrationPath => null command config.
It turns out this is how it looks:

'controllerMap' => [
        'migrate-module' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => [
                'namespace\from\module',
                'namespace\another'
            ],
            'migrationTable' => 'migration_module',
            'migrationPath' => null,
        ],
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question