Skip to content

Instantly share code, notes, and snippets.

@niksamokhvalov
Last active March 7, 2018 20:11
Show Gist options
  • Save niksamokhvalov/3dace9bee42bc76c5f8a to your computer and use it in GitHub Desktop.
Save niksamokhvalov/3dace9bee42bc76c5f8a to your computer and use it in GitHub Desktop.
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
Copy link

Where in the source are you running this file from?

@malutanpetronel
Copy link

malutanpetronel 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