Skip to content

Commit

Permalink
MigrationGroups/MigrationGroup change connection support
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Nov 14, 2019
1 parent 81f797c commit 2673337
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/Model/Migration/MigrationGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ class MigrationGroup
* MigrationGroup constructor.
*
* @param string $name the app / plugin name
* @param string|null $connection the connection name
*/
public function __construct($name)
public function __construct($name, $connection = null)
{
$this->name = $name;

$this->input = $this->buildInput($name);
$this->input = $this->buildInput($name, $connection);
}

/**
Expand All @@ -63,14 +64,18 @@ public function getName()
* Inputオブジェクトの構築
*
* @param string $name the app / plugin name
* @param string|null $connection the connection name
* @return InputInterface
*/
private function buildInput($name)
private function buildInput($name, $connection = null)
{
$args = [];
if ($name !== Configure::read('App.namespace')) {
$args['--plugin'] = $name;
}
if ($connection) {
$args['--connection'] = $connection;
}

return (new InputBuilder())->build($args);
}
Expand Down Expand Up @@ -98,7 +103,7 @@ private function getManager()
{
if ($this->manager === null) {
$this->output = new BufferedOutput();
$this->manager = new MigrationManager($this->getConfig(), $this->buildInput($this->getName()), $this->output);
$this->manager = new MigrationManager($this->getConfig(), $this->input, $this->output);
}

return $this->manager;
Expand Down Expand Up @@ -166,9 +171,20 @@ public function getFileContent($id)
throw new NotFoundException(__d('elastic.migration_manager', 'Migration Not Found. ID: {0}', $id));
}

$refrection = new ReflectionClass($migration);
$reflection = new ReflectionClass($migration);

return file_get_contents($reflection->getFileName());
}

return file_get_contents($refrection->getFileName());
/**
* change connection
*
* @param string $connection target connection name
* @return $this
*/
public function withConnection($connection)
{
return new static($this->name, $connection);
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/Model/Migration/MigrationGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class MigrationGroups
{
/**
* @var string
*/
private $connection;

/**
* @return CollectionInterface|MigrationGroup[]
*/
Expand Down Expand Up @@ -46,7 +51,7 @@ private function createMigrationGroup($name = null)
$name = Configure::read('App.namespace');
}

return new MigrationGroup($name);
return new MigrationGroup($name, $this->connection);
}

/**
Expand All @@ -61,4 +66,18 @@ private function hasMigrations($pluginName)

return is_dir($migrationPath) && count(glob($migrationPath . '/*.php'));
}

/**
* コネクションのセット
*
* @param string|null $connection 指定コネクション
* @return MigrationGroups
*/
public function withConnection($connection)
{
$new = clone $this;
$new->connection = $connection;

return $new;
}
}
27 changes: 27 additions & 0 deletions tests/TestCase/Model/Migration/MigrationGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,31 @@ public function testGetFileContentNotExists()

$this->migrationManagerGroup->getFileContent('20110102030405');
}

/**
* 接続を指定して初期化できる
*/
public function testConstructWithConnection()
{
$migrationGroup = new MigrationGroup('Elastic/MigrationManager', 'other');

$this->assertSame('default', $migrationGroup->getConfig()->getDefaultEnvironment());
$this->assertSame(5432, $migrationGroup->getConfig()->getEnvironment('default')['port']);
}

/**
* 接続を指定できる
*/
public function testWithConnection()
{
$migrationGroup = new MigrationGroup('Elastic/MigrationManager');
$withConnection = $migrationGroup->withConnection('other');

$this->assertSame('default', $withConnection->getConfig()->getDefaultEnvironment());
$this->assertSame(5432, $withConnection->getConfig()->getEnvironment('default')['port']);

// immutable
$this->assertSame('default', $migrationGroup->getConfig()->getDefaultEnvironment());
$this->assertNull($migrationGroup->getConfig()->getEnvironment('default')['port']);
}
}
18 changes: 16 additions & 2 deletions tests/TestCase/Model/Migration/MigrationGroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
class MigrationGroupsTest extends TestCase
{
/**
* @var \Elastic\MigrationManager\Model\Migration\MigrationGroups
* @var MigrationGroups
*/
private $subject;

public function setUp()
{
parent::setUp();
$this->subject = new \Elastic\MigrationManager\Model\Migration\MigrationGroups();
$this->subject = new MigrationGroups();
}

public function tearDown()
Expand Down Expand Up @@ -52,4 +52,18 @@ public function testFetchAll()
});
$this->assertContains('Elastic/MigrationManager', $names->toList());
}

/**
* コネクションを指定できる
*/
public function testWithConnection()
{
$groups = $this->subject->withConnection('other');

$results = $groups->fetchAll();

$first = $results->first();
$this->assertSame('default', $first->getConfig()->getDefaultEnvironment());
$this->assertSame(5432, $first->getConfig()->getEnvironment('default')['port']);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
ConnectionManager::setConfig('default', ['url' => getenv('db_dsn')]);
ConnectionManager::setConfig('other', ['url' => getenv('db_dsn'), 'port' => 5432]);

// Disable deprecations for now when using 3.6
if (version_compare(Configure::version(), '3.6.0', '>=')) {
Expand Down

0 comments on commit 2673337

Please sign in to comment.