Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Phinx console command for execute migrations of the all shards
<?php
/**
* Console command for Phinx with argument "all" for executed migrations for multiple databases.
*
* Structure directory for migrations and configs:
* ```
* migrations/
* .db1/
* .db2/
* .db3/
* db1.yml
* db2.yml
* db3.yml
* ```
*
* Console commands for migrate one DB:
* ```
* php phinx:migrate -c migrations/db1.yml
* ```
* Migrate all DB:
* ```
* php phinx:migrate all
* ```
*/
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;
class Migrate extends \Phinx\Console\Command\Migrate
{
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this->setName('phinx:migrate')
->addArgument('all', InputArgument::OPTIONAL, 'To perform all migration for all shards');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$migrationsConfigs = '/path/to/migrations/configs/';
if ($input->getArgument('all'))
{
if ($handle = opendir($migrationsConfigs))
{
while (false !== ($file = readdir($handle)))
{
$fileInfo = pathinfo($file);
if ($fileInfo['extension'] === 'yml')
{
$params = [
'--configuration' => $migrationsConfigs . $file
];
if ($input->getOption('environment'))
{
$params['--environment'] = $input->getOption('environment');
}
$migrate = new Migrate();
$migrate->run(new ArrayInput($params), $output);
}
}
closedir($handle);
}
}
else
{
parent::execute($input, $output);
}
}
}
@jamespsterling

This comment has been minimized.

Copy link

commented Apr 27, 2016

Where in the source are you running this file from?

@malutanpetronel

This comment has been minimized.

Copy link

commented Feb 17, 2017

I would like to use it in a codeigniter project Nik ! Ho do you use it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.