-
-
Save chayn1k/75e0f0f87c4240bf856e to your computer and use it in GitHub Desktop.
Phinx console command for execute migrations of the all shards http://samokhvalov.info/blog/all/phinx-multiple-databases/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment