Skip to content

Commit

Permalink
add environment support and migration init method
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Apr 28, 2016
1 parent fa6641e commit 0177b5a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
49 changes: 41 additions & 8 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

namespace Sokil\Mongo\Migrator;

use Sokil\Mongo\Client;

abstract class AbstractMigration
{
/**
*
* @var \Sokil\Mongo\Client
* @var Client
*/
private $_client;
private $client;

private $environment;

public function __construct(\Sokil\Mongo\Client $client)
{
$this->_client = $client;
return $this;
public function __construct(
Client $client
) {
$this->client = $client;

$this->init();
}

/**
* Do some job before migrating up or down
*/
protected function init() {}

/**
*
Expand All @@ -23,7 +34,7 @@ public function __construct(\Sokil\Mongo\Client $client)
*/
protected function getDatabase($name = null)
{
return $this->_client->getDatabase($name);
return $this->client->getDatabase($name);
}

/**
Expand All @@ -33,7 +44,29 @@ protected function getDatabase($name = null)
*/
protected function getCollection($name)
{
return $this->_client->getCollection($name);
return $this->client->getCollection($name);
}

/**
* Define environment of migration to run in
*
* @param $environment
* @return $this
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}

/**
* Get environment of running migration
*
* @return mixed
*/
protected function getEnvironment()
{
return $this->environment;
}

public function up() {}
Expand Down
7 changes: 6 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ protected function executeMigration($targetRevision, $environment, $direction)
require_once $this->getMigrationsDir() . '/' . $revision->getFilename();
$className = $revision->getName();

$migration = new $className($this->getClient($environment));
$migration = new $className(
$this->getClient($environment)
);

$migration->setEnvironment($environment);

$migration->up();

$this->logUp($revision->getId(), $environment);
Expand Down

0 comments on commit 0177b5a

Please sign in to comment.