Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Wrapper crashes when calling rollback twice #960

Closed
moritz-h opened this issue Sep 21, 2016 · 3 comments
Closed

Text Wrapper crashes when calling rollback twice #960

moritz-h opened this issue Sep 21, 2016 · 3 comments

Comments

@moritz-h
Copy link
Contributor

moritz-h commented Sep 21, 2016

When i try to call getRollback twice on a text wrapper i get a Symfony\Component\Console\Exception\RuntimeException: Unable to write output. Exception.

Example:

$app = new \Phinx\Console\PhinxApplication();
$wrap = new \Phinx\Wrapper\TextWrapper($app);
$wrap->setOption('environment', 'db');
$wrap->setOption('configuration', 'phinx.php');
$wrap->setOption('parser', 'php');

echo $wrap->getRollback(null, 0);
echo $wrap->getMigrate();
echo $wrap->getRollback(null, 0); // <- exception here

When i setup a new wrapper in between it works:

$app = new \Phinx\Console\PhinxApplication();
$wrap = new \Phinx\Wrapper\TextWrapper($app);
$wrap->setOption('environment', 'db');
$wrap->setOption('configuration', 'phinx.php');
$wrap->setOption('parser', 'php');

echo $wrap->getRollback(null, 0);
echo $wrap->getMigrate();

$app = new \Phinx\Console\PhinxApplication();
$wrap = new \Phinx\Wrapper\TextWrapper($app);
$wrap->setOption('environment', 'db');
$wrap->setOption('configuration', 'phinx.php');
$wrap->setOption('parser', 'php');

echo $wrap->getRollback(null, 0); // <- no exception here

Edit: Version is 0.6.4

@robmorgan
Copy link
Member

hi @moritz-h, what are you putting in your phinx.php file?

@moritz-h
Copy link
Contributor Author

@robmorgan I created a minimal example: https://gist.github.com/moritz-h/579f43c8af02f3644115d91185b73f6e
(The 20160922000000_init_db.php file should be in a migrations subdirectory, could not name it with path in the gist.)

I run composer install and start it with php -S localhost:8000 index.php.

The database settings in the phinx.php match to an existing mysql database and my php version is 7.0.8 (default Ubuntu 16.04).

@moritz-h
Copy link
Contributor Author

@robmorgan I found out where this error comes from:

The TextWrapper creates a new Output Instance for each call:
https://github.com/robmorgan/phinx/blob/master/src/Phinx/Wrapper/TextWrapper.php#L215

This OutputInterface Object is recieved by execute() of a command (i.e. Status) and then passed to bootstrap()
https://github.com/robmorgan/phinx/blob/master/src/Phinx/Console/Command/Status.php#L68-L70

AbstractCommand::bootstrap() passes this to AbstractCommand::loadManager() where a new Manager object is created, but only if no Manager was created before:
https://github.com/robmorgan/phinx/blob/master/src/Phinx/Console/Command/AbstractCommand.php#L285-L288

The Manager object saves the OutputInterface as property. And that is the source of the error.
If i call the command a second time the TextWrapper creates a new OutputInterface and passes this to the command. But the command then has cached the Manager with the reference to the old OutputInterface from the first call.

To fix this, it would be possible to here create always a new Manager:
https://github.com/robmorgan/phinx/blob/master/src/Phinx/Console/Command/AbstractCommand.php#L285-L288
But i don't know if this would has any other bad side effects.
Otherwise perhaps its possible to just update the InputInterface and OutputInterface of the Manager.
Or perhaps the TextWrapper also should reuse the same OutputInterface for all command calls.
(Then with Symfony\Component\Console\Output\BufferedOutput instead of Symfony\Component\Console\Output\StreamOutput no not mess up with fopen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants