yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.92k forks source link

Migration Create Error - Undefined index: version #15253

Open dersonsena opened 6 years ago

dersonsena commented 6 years ago

Hi everyone!

I trying to use Yii Migrations with the follow command:

./yii migrate/create migration_name

and the Migration Tool return the error below:

PHP Notice 'yii\base\ErrorException' with message 'Undefined index: version'

in /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/controllers/MigrateController.php:336

Stack trace:
#0 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/controllers/MigrateController.php(336): yii\base\ErrorHandler->handleError(8, 'Undefined index...', '/Users/kilderso...', 336, Array)
#1 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(631): yii\console\controllers\MigrateController->getMigrationNameLimit()
#2 [internal function]: yii\console\controllers\BaseMigrateController->actionCreate('migration_name')
#3 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#4 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#5 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/Controller.php(135): yii\base\Controller->runAction('create', Array)
#6 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('create', Array)
#7 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate/create', Array)
#8 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate/create', Array)
#9 /Users/kildersonsena/web-workspace/cadimoweb/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#10 /Users/kildersonsena/web-workspace/cadimoweb/yii(27): yii\base\Application->run()
#11 {main}

I dont find out nothing about this, so... I created the issue here for this.

Q A
Yii version 2.0.13.1
PHP version 7.1.11
Database PostgreSQL 9.6
Operating system MacOS
samdark commented 6 years ago

Are you able to reproduce it on clean basic application template?

dersonsena commented 6 years ago

Firstly, thanks for your answer @samdark ! In a clean basic template application is working. However, a few days ago I used the migrations tools and was working normally. I dont changed nothing in the core, I only running yesterday and appears this error.

What would this "version" that is returning? I tried fix, but, not successful. Can you help me?

dersonsena commented 6 years ago

Sorry! The single thing that I changed was the composer.json requirements. See below: PS: I dont know if this has anything to do with the error, but, was the single modification.

{
    "name": "yiisoft/yii2-app-advanced",
    "description": "Cadimo WEB",
    "authors": [
        {
            "name": "Kilderson Sena",
            "email": "kilderson@imobsoft.com.br"
        },
        {
            "name": "Diego Pedrosa",
            "email": "diego@imobsoft.com.br"
        }
    ],
    "homepage": "http://www.cadimoweb.com.br",
    "type": "project",
    "license": "BSD-3-Clause",
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "stable",
    "repositories": [
        {
            "type": "composer",
            "url": "https://asset-packagist.org"
        }
    ],
    "require": {
        "php": ">=7",
        "ext-intl": "*",
        "ext-mbstring": "*",
        "ext-soap": "*",
        "yiisoft/yii2": "~2.0.11",
        "yiisoft/yii2-bootstrap": "~2.0.0",
        "yiisoft/yii2-swiftmailer": "~2.0.0",
        "yiisoft/yii2-jui": "~2.0.0",
        "kartik-v/yii2-widget-growl": "*",
        "kartik-v/yii2-widget-activeform": "^1.4",
        "kartik-v/yii2-widget-select2": "2.0.8",
        "kartik-v/yii2-widget-datepicker": "@dev",
        "kartik-v/bootstrap-fileinput": "dev-master",
        "yiibr/yii2-br-validator": "*",
        "mpdf/mpdf": "^6.1",
        "kartik-v/yii2-widget-fileinput": "@dev",
        "bower-asset/jquery-numeric_input": "@dev",
        "bower-asset/bootstrap-datepicker": "@dev",
        "bower-asset/lightbox2": "*",
        "bower-asset/jquery-maskmoney": "^3.0.2",
        "bower-asset/bootstrap-toggle": "^2.2",
        "bower-asset/select2": "^4.0",
        "bower-asset/bootstrap-fileinput": "^4.4",
        "bower-asset/summernote": "^0.8.6",
        "dersonsena/yii2-widget-inputsaddon": "@dev",
        "dersonsena/cadimoweb-gii": "^1"
    },
    "require-dev": {
        "yiisoft/yii2-debug": "~2.0.0",
        "yiisoft/yii2-gii": "~2.0.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/base": "^2.2.3",
        "codeception/verify": "~0.3.1",
        "codeception/codeception": "^2.3",
        "deployer/deployer": "^6.0"
    },
    "config": {
        "process-timeout": 1800
    }
}
schmunk42 commented 6 years ago

How does your migration table look like? Did you accidentally create a migration table with a wrong structure manually?

dersonsena commented 6 years ago

Hi @schmunk42 ! In fact, when I execute the command it already issues this error. Since it is something version related, I will put another version of my postgresql.

dersonsena commented 6 years ago

I just discovered the problem! I had a similar issue with this issue https://github.com/yiisoft/yii2/issues/13229 by commit #8f5f5

The error is giving in the method below:

protected function getMigrationNameLimit()
{
    if ($this->_migrationNameLimit !== null) {
        return $this->_migrationNameLimit;
    }
    $tableSchema = $this->db->schema ? $this->db->schema->getTableSchema($this->migrationTable, true) : null;
    if ($tableSchema !== null) {
        // The error happens in the line below...
        return $this->_migrationNameLimit = $tableSchema->columns['version']->size;
    }

    return static::MAX_NAME_LENGTH;
}

If run the print_r($tableSchema->columns) returns:

Array
(
    [VERSION] => yii\db\ColumnSchema Object
        (
            [name] => VERSION
            [allowNull] => 
            [type] => string
            [phpType] => string
            [dbType] => varchar
            [defaultValue] => 
            [enumValues] => 
            [size] => 180
            [precision] => 
            [scale] => 
            [isPrimaryKey] => 1
            [autoIncrement] => 
            [unsigned] => 
            [comment] => 
        )
    [APPLY_TIME] => yii\db\ColumnSchema Object
        (
            [name] => APPLY_TIME
            [allowNull] => 1
            [type] => integer
            [phpType] => integer
            [dbType] => int4
            [defaultValue] => 
            [enumValues] => 
            [size] => 
            [precision] => 32
            [scale] => 0
            [isPrimaryKey] => 
            [autoIncrement] => 
            [unsigned] => 
            [comment] => 
        )
)

All the fields in my database are in the uppercase, so it is necessary to make the same correction that was made in the issue quoted at the beginning:

protected function getMigrationNameLimit()
{
    if ($this->_migrationNameLimit !== null) {
        return $this->_migrationNameLimit;
    }
    $tableSchema = $this->db->schema ? $this->db->schema->getTableSchema($this->migrationTable, true) : null;
    if ($tableSchema !== null) {
        if ($this->db->slavePdo->getAttribute(\PDO::ATTR_CASE) === \PDO::CASE_UPPER) {
            $tableSchema->columns = array_change_key_case($tableSchema->columns, CASE_LOWER);
        }
        return $this->_migrationNameLimit = $tableSchema->columns['version']->size;
    }

    return static::MAX_NAME_LENGTH;
}

I changed here locally and it worked =)

schmunk42 commented 6 years ago

Tests also fail on PgSQL >=9.5 see https://github.com/yiisoft/yii2/issues/15254 - but the error looks not directly related.

Renkas commented 6 years ago

maybe you upgraded your PgSQL version?

dersonsena commented 6 years ago

I'm using the PostgreSQL 9.6, but, this error appears in the versions 9.5 and 9.6! The code above worked in the 2 versions :)

bscheshirwork commented 6 years ago

@dersonsena try PostgreSQL 9.4

dersonsena commented 6 years ago

Hi @bscheshirwork! My production Postgresql Version is 9.5, so .... my Postgresql container it has to be highter 9.5. But, this solution above fix the problem... How @klimov-paul helped me in another issue ;)

bscheshirwork commented 6 years ago

@dersonsena you can add link to @klimov-paul 's comment?

PostgreSQL v9.5 not convert default values to column format. check your default value for this problem. int column can contain string default value float column can contain string default value

10.0 also not convert CURRENT_TIMESTAMP to now(), convert to string 'CURRENT_TIMESTAMP'

15266

dersonsena commented 6 years ago

But in the issue that was solved the problem was the uppercase array keys. Same thing happened here. Upstairs has the commit links and the issue.