Skip to content

Commit

Permalink
Merge pull request #89 from HavokInspiration/fix-mark-migrated
Browse files Browse the repository at this point in the history
Fix baking snapshots
  • Loading branch information
markstory committed May 29, 2015
2 parents f3335bf + 33f56d8 commit b5758f5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
29 changes: 26 additions & 3 deletions src/Shell/MigrationsShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Cake\Console\Shell;
use Migrations\MigrationsDispatcher;
use Symfony\Component\Console\Input\ArgvInput;

/**
* A wrapper shell for phinx migrations, used to inject our own
Expand All @@ -22,6 +23,13 @@
class MigrationsShell extends Shell
{

/**
* Array of arguments to run the shell with.
*
* @var array
*/
public $argv = [];

/**
* Defines what options can be passed to the shell.
* This is required because CakePHP validates the passed options
Expand Down Expand Up @@ -62,14 +70,29 @@ public function initialize()
* responsible for parsing the command line from phinx and gives full control of
* the rest of the flow to it.
*
* The input parameter of the ``MigrationDispatcher::run()`` method is manually built
* in case a MigrationsShell is dispatched using ``Shell::dispatch()``.
*
* @return void
*/
public function main()
{
array_shift($_SERVER['argv']);
$_SERVER['argv']--;
$app = new MigrationsDispatcher(PHINX_VERSION);
$app->run();
$input = new ArgvInput($this->argv);
$app->run($input);
}

/**
* Override the default behavior to save the command called
* in order to pass it to the command dispatcher
*
* {@inheritDoc}
*/
public function runCommand($argv, $autoMethod = false)
{
array_unshift($argv, 'migrations');
$this->argv = $argv;
return parent::runCommand($argv, $autoMethod);
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/Shell/Task/MigrationSnapshotTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,18 @@ protected function markSnapshotApplied($path)
$fileName = pathinfo($path, PATHINFO_FILENAME);
list($version, ) = explode('_', $fileName, 2);

$argv = $_SERVER['argv'];
$_SERVER['argv'] = [
'',
'migrations',
'mark_migrated',
$version
];

$dispatchCommand = 'migrations mark_migrated ' . $version;
if (!empty($this->params['connection'])) {
$dispatchCommand .= ' -c ' . $this->params['connection'];
}

if (!empty($this->params['plugin'])) {
$dispatchCommand .= ' -p ' . $this->params['plugin'];
}

$this->_io->out('Marking the snapshot ' . $fileName . ' as migrated...');
$result = $this->dispatchShell('migrations', 'mark_migrated', $version);
$_SERVER['argv'] = $argv;
$this->dispatchShell($dispatchCommand);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ public function setUp()
public function testNotEmptySnapshot()
{
$this->Task->params['require-table'] = false;
$this->Task->params['connection'] = 'test';
$this->Task->params['plugin'] = 'BogusPlugin';

$version = Util::getCurrentTimestamp();

$this->Task->expects($this->once())
->method('dispatchShell')
->with('migrations', 'mark_migrated', $version);
->with(
$this->logicalAnd(
$this->stringContains('migrations mark_migrated'),
$this->stringContains('-c test -p BogusPlugin')
)
);

$result = $this->Task->bake('NotEmptySnapshot');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
Expand Down

0 comments on commit b5758f5

Please sign in to comment.