sjaakp / yii2-pluto

User management extension for Yii2
https://demo.sjaakpriester.nl/
MIT License
8 stars 12 forks source link

Migration issue if project already initialized with yii migrate #33

Open masiorama opened 3 years ago

masiorama commented 3 years ago

Hello, first of all thank you for plugo module.

I have an issue while installing the module. I don't understand why the pluto migration creates a user table instead of altering the user table already created initializing the yii project.

To be clear, these are the steps I took, after installing yii and pluto:

./yii migrate
./yii migrate --migrationPath=@yii/rbac/migrations

The first migrate create 2 tables, migration and user.

Note: to apply pluto migration as a second step, it is important the remove pluto from the bootstrap. When I bootstrap pluto its migration tries to create another user table, which breaks the command.

The only way to fix it is to rename or delete the original user table... my question is: why a create table instead of altering what already is available?

I don't know if I am wrong, so if anybody can help me clear my thoughts, I thank him in advance! :)

nathan005 commented 3 years ago

It doesn't, my guess is you included the module/bootstrap in both common and in console for an advanced template. It only needs to be declared in both if you are using the basic app (this should probably be clearer in the readme)

From the authManager Docs:

Note: If you are using yii2-basic-app template, there is a config/console.php configuration file where the authManager needs to be declared additionally to config/web.php. In case of yii2-advanced-app the authManager should be declared only once in common/config/main.php.

Same is true for modules - common applies to all 3 apps (front/back/console)

If you set up the module only in common, does try to update the existing user table. However, if you run the command above you will get an error: "user table already exists"

To fix this error simply add /up to the command php yii migrate/up --migrationPath=@yii/rbac/migrations

EDIT: I did a test on my dev machine, you may be right, I'll test again with a fresh install to be sure I'm using postgres so my db wouldn't allow the second table.

nathan005 commented 3 years ago

So You are right, on a fresh install, when running yii migrate it fails if you have added the module to your config.

Caused by: Exception 'PDOException' with message 'SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "user" already exists'

Error seems to be on line 1099 'CREATE TABLE'

Should be wrapped in an if statement to prevent this error


$tableName = $this->db->tablePrefix . 'users';
if ($this->db->getTableSchema($tableName, true) === null) {

Steps To reproduce:

  1. Install yii 2 advanced: composer create-project --prefer-dist yiisoft/yii2-app-advanced myAppFolder && php init
  2. Install pluto: composer require sjaakp/yii2-pluto:dev-master
  3. Add Pluto / RBAC Config to common/config/main.php:

    'bootstrap' => [
        'pluto',
    ],
    'modules' => [
        'pluto' => [
            'class' => 'sjaakp\pluto\Module',
            // several options
        ],
    ],
    
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            // uncomment if you want to cache RBAC items hierarchy
            // 'cache' => 'cache',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
  4. Set DB settings in common/config/main-local.php
  5. Run yii migrate
  6. ERROR
nathan005 commented 3 years ago

Created a pull Request - https://github.com/sjaakp/yii2-pluto/pull/34