yiisoft / yii2-app-basic

Yii 2.0 Basic Application Template
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
656 stars 788 forks source link

Error during CLI migration not returning exitcode 1 as expected #180

Open mdaize opened 6 years ago

mdaize commented 6 years ago

What steps will reproduce the problem?

  1. Re-run migration up after a partially complete migration due to an error (eg. table already exists)
  2. Attempt to run migration:
    
    ➜ php tests/bin/yii migrate
    Yii Migration Tool (based on Yii v2.0.15.1)

Total 2 new migrations to be applied: m000001_000000_item m000002_000000_item_groups

Apply the above migrations? (yes|no) [no]:yes *** applying m000001_000000_item

create table items ...Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1 table items already exists Failed to prepare SQL: CREATE TABLE items ( ... Error Info: Array ( [0] => HY000 [1] => 1 [2] => table items already exists )

Stack trace:

0 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/db/Command.php(1070): yii\db\Command->prepare(false)

1 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/db/sqlite/Command.php(32): yii\db\Command->execute()

2 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/db/Migration.php(323): yii\db\sqlite\Command->execute()

3 /home/mdaize/Projects/services/item-service/src/migrations/m000001_000000_item.php(53): yii\db\Migration->createTable('items', Array, NULL)

4 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(725): m000001_000000_item->up()

5 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(199): yii\console\controllers\BaseMigrateController->migrateUp('m000001000000...')

6 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)

7 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

8 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

9 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('', Array)

10 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('', Array)

11 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate', Array)

12 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate', Array)

13 /home/mdaize/Projects/services/item-service/src/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))

14 /home/mdaize/Projects/services/item-service/tests/bin/yii(28): yii\base\Application->run()

15 {main}

3. Check exit code with `echo $?`

➜ echo $? 0


### What's expected?
Exitcode should be *1*

### What do you get instead?
Exitcode returned at *0*

### Additional info
This was discovered when using a sqlite database during our local testing process using the following shell script:

!/bin/bash

php tests/bin/yii migrate up --interactive=0 if [ $? -gt 0 ]; then exit 1 fi

Other detailed information and stuff:

➜ cat tests/bin/yii

!/usr/bin/env php

<?php /**

defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'test');

require(DIR . '/../../src/vendor/autoload.php'); require(DIR . '/../../src/vendor/yiisoft/yii2/Yii.php');

$config = yii\helpers\ArrayHelper::merge( require(DIR . '/../../src/config/console.php'), [ 'components' => [ 'db' => require(DIR . '/../config/test_db.php') ] ] );

$application = new yii\console\Application($config); $exitCode = $application->run(); exit($exitCode);

➜ cat /etc/fedora-release Fedora release 27 (Twenty Seven)

➜ php --version PHP 7.1.14 (cli) (built: Jan 30 2018 19:05:27) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.14, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

➜ composer info
behat/gherkin v4.5.1 Gherkin DSL parser for PHP 5.3 bower-asset/bootstrap v3.3.7
bower-asset/inputmask 3.3.11
bower-asset/jquery 3.2.1
bower-asset/punycode v1.3.2
bower-asset/typeahead.js v0.11.1 bower-asset/yii2-pjax 2.0.7.1 cebe/markdown 1.1.2 A super fast, highly extensible markdown parser for PHP cilex/cilex 1.1.0 The PHP micro-framework for Command line tools based on the Symfony2 Components cilex/console-service-provider 1.0.0 Console Service Provider codeception/base 2.4.1 BDD-style testing framework codeception/phpunit-wrapper 7.1.1 PHPUnit classes used by Codeception codeception/specify 0.4.6 BDD code blocks for PHPUnit and Codeception codeception/stub 1.0.2 Flexible Stub wrapper for PHPUnit's Mock Builder codeception/verify 0.3.3 BDD assertion library for PHPUnit container-interop/container-interop 1.2.0 Promoting the interoperability of container objects (DIC, SL, etc.) doctrine/annotations v1.6.0 Docblock Annotations Parser doctrine/instantiator 1.1.0 A small, lightweight utility to instantiate objects in PHP without invoking their construc... doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. erusev/parsedown 1.7.1 Parser for Markdown. ezyang/htmlpurifier v4.10.0 Standards compliant HTML filter written in PHP fzaninotto/faker v1.7.1 Faker is a PHP library that generates fake data for you. guzzlehttp/guzzle 6.3.3 Guzzle is a PHP HTTP client library guzzlehttp/promises v1.3.1 Guzzle promises library guzzlehttp/psr7 1.4.2 PSR-7 message implementation that also provides common utility methods herrera-io/json 1.0.3 A library for simplifying JSON linting and validation. herrera-io/phar-update 1.0.3 A library for self-updating Phars. jms/metadata 1.6.0 Class/method/property metadata management in PHP jms/parser-lib 1.0.0 A library for easily creating recursive-descent parsers. jms/serializer 1.11.0 Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML. justinrainbow/json-schema 1.6.1 A library to validate a json schema. kherge/version 1.0.1 A parsing and comparison library for semantic versioning. monolog/monolog 1.23.0 Sends your logs to files, sockets, inboxes, databases and various web services myclabs/deep-copy 1.7.0 Create deep copies (clones) of your objects nikic/php-parser v1.4.1 A PHP parser written in PHP pdepend/pdepend 2.5.0 Official version of pdepend to be handled with Composer phar-io/manifest 1.0.1 Component for reading phar.io manifest information from a PHP Archive (PHAR) phar-io/version 1.0.1 Library for handling version information and constraints phpcollection/phpcollection 0.5.0 General-Purpose Collection Library for PHP phpdocumentor/fileset 1.0.0 Fileset component for collecting a set of files given directories and file paths phpdocumentor/graphviz 1.0.4
phpdocumentor/phpdocumentor v2.9.0 Documentation Generator for PHP phpdocumentor/reflection 3.0.1 Reflection library to do Static Analysis for PHP Projects phpdocumentor/reflection-docblock 2.0.5
phpoption/phpoption 1.5.0 Option Type for PHP phpspec/php-diff v1.1.0 A comprehensive library for generating differences between two hashable objects (strings o... phpspec/prophecy 1.7.6 Highly opinionated mocking framework for PHP 5.3+ phpunit/php-code-coverage 6.0.4 Library that provides collection, processing, and rendering functionality for PHP code cov... phpunit/php-file-iterator 1.4.5 FilterIterator implementation that filters files based on a list of suffixes. phpunit/php-text-template 1.2.1 Simple template engine. phpunit/php-timer 2.0.0 Utility class for timing phpunit/php-token-stream 3.0.0 Wrapper around PHP's tokenizer extension. phpunit/phpunit 7.1.4 The PHP Unit Testing framework. phpunit/phpunit-mock-objects 6.1.1 Mock Object library for PHPUnit pimple/pimple v1.1.1 Pimple is a simple Dependency Injection Container for PHP 5.3 psr/cache 1.0.1 Common interface for caching libraries psr/container 1.0.0 Common Container Interface (PHP FIG PSR-11) psr/http-message 1.0.1 Common interface for HTTP messages psr/log 1.0.2 Common interface for logging libraries psr/simple-cache 1.0.1 Common interfaces for simple caching sebastian/code-unit-reverse-lookup 1.0.1 Looks up which function or method a line of code belongs to sebastian/comparator 2.1.3 Provides the functionality to compare PHP values for equality sebastian/diff 3.0.0 Diff implementation sebastian/environment 3.1.0 Provides functionality to handle HHVM/PHP environments sebastian/exporter 3.1.0 Provides the functionality to export PHP variables for visualization sebastian/global-state 2.0.0 Snapshotting of global state sebastian/object-enumerator 3.0.3 Traverses array structures and object graphs to enumerate all referenced objects sebastian/object-reflector 1.1.1 Allows reflection of object attributes, including inherited and non-public ones sebastian/recursion-context 3.0.0 Provides functionality to recursively process PHP variables sebastian/resource-operations 1.0.0 Provides a list of PHP built-in functions that operate on resources sebastian/version 2.0.1 Library that helps with managing the version number of Git-hosted PHP projects seld/jsonlint 1.7.1 JSON Linter squizlabs/php_codesniffer 3.2.2 PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a define... symfony/browser-kit v4.0.9 Symfony BrowserKit Component symfony/config v2.8.39 Symfony Config Component symfony/console v2.8.39 Symfony Console Component symfony/css-selector v4.0.9 Symfony CssSelector Component symfony/debug v3.0.9 Symfony Debug Component symfony/dependency-injection v3.2.14 Symfony DependencyInjection Component symfony/dom-crawler v4.0.9 Symfony DomCrawler Component symfony/event-dispatcher v2.8.39 Symfony EventDispatcher Component symfony/filesystem v3.0.9 Symfony Filesystem Component symfony/finder v2.8.39 Symfony Finder Component symfony/polyfill-mbstring v1.8.0 Symfony polyfill for the Mbstring extension symfony/process v2.8.39 Symfony Process Component symfony/stopwatch v2.8.39 Symfony Stopwatch Component symfony/translation v3.0.9 Symfony Translation Component symfony/validator v2.8.39 Symfony Validator Component symfony/yaml v3.3.16 Symfony Yaml Component theseer/tokenizer 1.1.0 A small library for converting tokenized PHP source code into XML and potentially other fo... tuyakhov/yii2-json-api v0.1.6 Implementation of JSON API specification for the Yii framework twig/twig v1.35.3 Twig, the flexible, fast, and secure template language for PHP yiisoft/yii2 2.0.15.1 Yii PHP Framework Version 2 yiisoft/yii2-bootstrap 2.0.8 The Twitter Bootstrap extension for the Yii framework yiisoft/yii2-composer 2.0.6 The composer plugin for Yii extension installer yiisoft/yii2-debug 2.0.13 The debugger extension for the Yii framework yiisoft/yii2-faker 2.0.4 Fixture generator. The Faker integration for the Yii framework. yiisoft/yii2-gii 2.0.7 The Gii extension for the Yii framework yiisoft/yii2-httpclient 2.0.6 HTTP client extension for the Yii framework zendframework/zend-cache 2.8.2 Caching implementation with a variety of storage options, as well as codified caching stra... zendframework/zend-config 2.6.0 provides a nested object property based user interface for accessing this configuration da... zendframework/zend-eventmanager 3.2.1 Trigger and listen to events within a PHP application zendframework/zend-filter 2.8.0 provides a set of commonly needed data filters zendframework/zend-hydrator 1.1.0
zendframework/zend-i18n 2.8.0 Provide translations for your application, and filter and validate internationalized values zendframework/zend-json 3.1.0 provides convenience methods for serializing native PHP to JSON and decoding JSON to nativ... zendframework/zend-serializer 2.8.1 provides an adapter based interface to simply generate storable representation of PHP type... zendframework/zend-servicemanager 2.7.10
zendframework/zend-stdlib 2.7.7
zetacomponents/base 1.9.1 The Base package provides the basic infrastructure that all packages rely on. Therefore ev... zetacomponents/document 1.3.1 The Document components provides a general conversion framework for different semantic doc...

SamMousa commented 5 years ago

@MatthewDaize I think this is because YII_ENV_TEST is true. To not interrupt testing (by exiting PHP), we don't call exit when we detect testing is true.

In this case the migrations should not be run with YII_ENV_TEST set to true.

rumours86 commented 5 years ago

This is not the right behavior! You suggest to start migrations for test base so? export YII_ENV_TEST=false && php yii_test migrate --interactive=0

rumours86 commented 5 years ago

Unfortunately, it still does not work!

machour commented 5 years ago

Re-opening issue as requested by @rumours86

rob006 commented 5 years ago

export YII_ENV_TEST=false && php yii_test migrate --interactive=0

YII_ENV_TEST is not environment variable, it is PHP constant.

rumours86 commented 5 years ago

How can I run "php yii_test migrate" so that they return the exitcode=1 in case of an error?

rumours86 commented 5 years ago

Yii Migration Tool (based on Yii v2.0.17)

rumours86 commented 5 years ago

wrote bash script

#!/usr/bin/env bash
YII_TEST_MIGRATE=$(php yii_test migrate --interactive=0)
if echo $YII_TEST_MIGRATE | grep 'Migrated up successfully.' -o || echo $YII_TEST_MIGRATE | grep 'No new migrations found. Your system is up-to-date.' -o
    then echo "Migrate done"
else
    echo "Migrate failed"
    echo "$YII_TEST_MIGRATE"
    exit 1
fi
rumours86 commented 5 years ago

wrote in yii_test

try {
    $exitCode = $application->run();
} catch (\Exception $exception) {
    echo $exception;
    $exitCode = ExitCode::UNSPECIFIED_ERROR;
}
Blacknife commented 5 years ago

Is there an adequate solution to this problem?

samdark commented 5 years ago

@Blacknife not yet.

particleflux commented 1 year ago

If you're running yii >= 2.0.36, you can configure this in console config like the following:

'controllerMap'       => [
    'migrate' => [
        'class' => \yii\console\controllers\MigrateController::class,
        'silentExitOnException' => false,
    ],
]

(see https://www.yiiframework.com/doc/api/2.0/yii-console-controller#$silentExitOnException-detail )