Skip to content

Commit

Permalink
Merge pull request #1 from elstc/develop
Browse files Browse the repository at this point in the history
Improve MigrationGroup
  • Loading branch information
nojimage authored Nov 18, 2019
2 parents 2673337 + 5fdadbf commit 74c0946
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Model/Migration/MigrationGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use Cake\Collection\Collection;
use Cake\Collection\CollectionInterface;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\Http\Exception\NotFoundException;
use Elastic\MigrationManager\Model\Entity\MigrationStatus;
use Migrations\CakeAdapter;
use Migrations\ConfigurationTrait;
use Phinx\Migration\Manager;
use ReflectionClass;
Expand Down Expand Up @@ -84,6 +87,7 @@ private function buildInput($name, $connection = null)
* マイグレーションリストの取得
*
* @return CollectionInterface|MigrationStatus[]
* @throws \Exception
*/
public function getMigrations()
{
Expand All @@ -98,21 +102,52 @@ public function getMigrations()

/**
* @return Manager|MigrationManager
* @throws \Exception
*/
private function getManager()
{
if ($this->manager === null) {
$this->output = new BufferedOutput();
$this->manager = new MigrationManager($this->getConfig(), $this->input, $this->output);
$this->setAdapter();
}

return $this->manager;
}

/**
* Sets the adapter the manager is going to need to operate on the DB
* This will make sure the adapter instance is a \Migrations\CakeAdapter instance
*
* @return void
* @throws \Exception
*/
private function setAdapter()
{
$env = $this->manager->getEnvironment('default');
$adapter = $env->getAdapter();

if ($adapter instanceof CakeAdapter) {
return;
}

$connectionName = 'default';
if ($this->input !== null && $this->input->getOption('connection')) {
$connectionName = $this->input->getOption('connection');
}
$connection = ConnectionManager::get($connectionName);
if (!$connection instanceof Connection) {
throw new \Exception('$connection must be ' . Connection::class);
}

$env->setAdapter(new CakeAdapter($adapter, $connection));
}

/**
* 最終のマイグレーション
*
* @return MigrationStatus
* @throws \Exception
*/
public function getLastMigration()
{
Expand All @@ -124,6 +159,7 @@ public function getLastMigration()
*
* @param string $id migration ID
* @return string
* @throws \Exception
*/
public function migrateTo($id)
{
Expand All @@ -138,6 +174,7 @@ public function migrateTo($id)
*
* @param string $id migration ID
* @return string
* @throws \Exception
*/
public function rollback($id)
{
Expand All @@ -147,13 +184,29 @@ public function rollback($id)
return $this->output->fetch();
}

/**
* シードを実行する
*
* @param string $seed seed name
* @return string
* @throws \Exception
*/
public function seed($seed = null)
{
$manager = $this->getManager();
$manager->seed($this->getConfig()->getDefaultEnvironment(), $seed);

return $this->output->fetch();
}

/**
* マイグレーションファイルの内容を取得する
*
* @param string $id migration ID
* @return string
* @throws NotFoundException
* @throws ReflectionException
* @throws \Exception
*/
public function getFileContent($id)
{
Expand Down Expand Up @@ -189,6 +242,7 @@ public function withConnection($connection)

/**
* @return array
* @throws \Exception
*/
public function __debugInfo()
{
Expand Down

0 comments on commit 74c0946

Please sign in to comment.