Phinx: Run migrations progmatically

Created on 11 Jun 2018  ·  3Comments  ·  Source: cakephp/phinx

I'm trying to integrate Phinx into my own custom CLI with Symfony Console Component.

My goal is to be able to reset the database completely and re-run all the migrations. The first part I accomplished, but I have no clue how I could run the migrations progmatically.

I know I can do something like exec('vendor/bin/phinx migrate'); and exec('vendor/bin/phinx seed:run'); but that's a pretty dirty solution IMO 🤷‍♂️

Anyone has some pointers?

Most helpful comment

I use this to simply run the phinx migrate command from my own CLI app.

```php
protected function execute(InputInterface $input, OutputInterface $output) {

$arguments = [
    'command' => 'migrate',
    '-c' => PATH.'res/database/phinx.php',
    '-e' => 'default',
    '-vvv' => true
];

return (new PhinxApplication())->find('migrate')->run(new ArrayInput($arguments), $output);

}

All 3 comments

Maybe you can look at how the CakePHP framework wraps phinx to run it inside its own CLI environment?

Here's an example of using the Migrate command to add some extra features: https://github.com/cakephp/migrations/blob/master/src/Command/Migrate.php#L59

I use this to simply run the phinx migrate command from my own CLI app.

```php
protected function execute(InputInterface $input, OutputInterface $output) {

$arguments = [
    'command' => 'migrate',
    '-c' => PATH.'res/database/phinx.php',
    '-e' => 'default',
    '-vvv' => true
];

return (new PhinxApplication())->find('migrate')->run(new ArrayInput($arguments), $output);

}

Thanks @sabl0r !

Edit: I took the time to actually implement the above, and I completely forgot that Phinx runs on the Symfony Console Component aswell. So this solution is actually documented here.

Was this page helpful?
0 / 5 - 0 ratings