Skip to content

Commit

Permalink
Fixed connection in combination with phinx TablePrefixAdapter #13
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed May 22, 2017
1 parent 7a3a4d6 commit 929066b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Migration/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Odan\Migration\Command;

use Exception;
use Odan\Migration\Generator\MigrationGenerator;
use PDO;
use Phinx\Console\Command\AbstractCommand;
use Phinx\Db\Adapter\PdoAdapter;
use Phinx\Migration\Manager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Odan\Migration\Generator\MigrationGenerator;

class GenerateCommand extends AbstractCommand
{
Expand Down Expand Up @@ -92,7 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Gets the database adapter.
$dbAdapter = $manager->getEnvironment($environment)->getAdapter();
$pdo = $dbAdapter->getConnection();

$pdo = $this->getPdo($manager, $environment);

$name = $input->getOption('name');
$overwrite = $input->getOption('overwrite');
Expand All @@ -110,9 +114,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
'overwrite' => $overwrite,
'mark_migration' => true
);
//var_dump($settings);

$generator = new MigrationGenerator($settings, $input, $output);
return $generator->generate();
}

/**
* Get PDO instance.
*
* @param Manager $manager Manager
* @param string $environment Environment name
* @return PDO PDO object
* @throws Exception On error
*/
protected function getPdo(Manager $manager, $environment)
{
// Gets the database adapter.
$dbAdapter = $manager->getEnvironment($environment)->getAdapter();

if ($dbAdapter instanceof PdoAdapter) {
$pdo = $dbAdapter->getConnection();
} else {
$dbAdapter->connect();
$pdo = $dbAdapter->getAdapter()->getConnection();
}
if (!$pdo) {
$pdo = $dbAdapter->getOption('connection');
}
if (!$pdo instanceof PDO) {
throw new Exception('No PDO database connection found.');
}

return $pdo;
}
}

0 comments on commit 929066b

Please sign in to comment.