Last active
March 7, 2018 20:11
-
-
Save niksamokhvalov/3dace9bee42bc76c5f8a to your computer and use it in GitHub Desktop.
Phinx console command for execute migrations of the all shards
This file contains 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); | |
} | |
} | |
} |
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
Where in the source are you running this file from?