Skip to content

Commit

Permalink
Update Migrations to use backends in more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 11, 2024
1 parent 022e9f8 commit 02e70e1
Showing 1 changed file with 6 additions and 106 deletions.
112 changes: 6 additions & 106 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,9 @@ public function migrate(array $options = []): bool
*/
public function rollback(array $options = []): bool
{
$this->setCommand('rollback');
$input = $this->getInput('Rollback', [], $options);
$method = 'rollback';
$params = ['default', $input->getOption('target')];

if ($input->getOption('date')) {
$method = 'rollbackToDateTime';
$params[1] = new DateTime($input->getOption('date'));
}

$this->run($method, $params, $input);
$backend = $this->getBackend();

return true;
return $backend->rollback($options);
}

/**
Expand All @@ -235,32 +225,9 @@ public function rollback(array $options = []): bool
*/
public function markMigrated(int|string|null $version = null, array $options = []): bool
{
$this->setCommand('mark_migrated');

if (
isset($options['target']) &&
isset($options['exclude']) &&
isset($options['only'])
) {
$exceptionMessage = 'You should use `exclude` OR `only` (not both) along with a `target` argument';
throw new InvalidArgumentException($exceptionMessage);
}

$input = $this->getInput('MarkMigrated', ['version' => $version], $options);
$this->setInput($input);

// This will need to vary based on the config option.
$migrationPaths = $this->getConfig()->getMigrationPaths();
$config = $this->getConfig(true);
$params = [
array_pop($migrationPaths),
$this->getManager($config)->getVersionsToMark($input),
$this->output,
];

$this->run('markVersionsAsMigrated', $params, $input);
$backend = $this->getBackend();

return true;
return $backend->markMigrated($version, $options);
}

/**
Expand All @@ -277,76 +244,9 @@ public function markMigrated(int|string|null $version = null, array $options = [
*/
public function seed(array $options = []): bool
{
$this->setCommand('seed');
$input = $this->getInput('Seed', [], $options);

$seed = $input->getOption('seed');
if (!$seed) {
$seed = null;
}

$params = ['default', $seed];
$this->run('seed', $params, $input);

return true;
}

/**
* Runs the method needed to execute and return
*
* @param string $method Manager method to call
* @param array $params Manager params to pass
* @param \Symfony\Component\Console\Input\InputInterface $input InputInterface needed for the
* Manager to properly run
* @return mixed The result of the CakeManager::$method() call
*/
protected function run(string $method, array $params, InputInterface $input): mixed
{
// This will need to vary based on the backend configuration
if ($this->configuration instanceof Config) {
$migrationPaths = $this->getConfig()->getMigrationPaths();
$migrationPath = array_pop($migrationPaths);
$seedPaths = $this->getConfig()->getSeedPaths();
$seedPath = array_pop($seedPaths);
}

$pdo = null;
if ($this->manager instanceof Manager) {
$pdo = $this->manager->getEnvironment('default')
->getAdapter()
->getConnection();
}

$this->setInput($input);
$newConfig = $this->getConfig(true);
$manager = $this->getManager($newConfig);
$manager->setInput($input);

// Why is this being done? Is this something we can eliminate in the new code path?
if ($pdo !== null) {
/** @var \Phinx\Db\Adapter\PdoAdapter|\Migrations\CakeAdapter $adapter */
/** @psalm-suppress PossiblyNullReference */
$adapter = $this->manager->getEnvironment('default')->getAdapter();
while ($adapter instanceof WrapperInterface) {
/** @var \Phinx\Db\Adapter\PdoAdapter|\Migrations\CakeAdapter $adapter */
$adapter = $adapter->getAdapter();
}
$adapter->setConnection($pdo);
}

$newMigrationPaths = $newConfig->getMigrationPaths();
if (isset($migrationPath) && array_pop($newMigrationPaths) !== $migrationPath) {
$manager->resetMigrations();
}
$newSeedPaths = $newConfig->getSeedPaths();
if (isset($seedPath) && array_pop($newSeedPaths) !== $seedPath) {
$manager->resetSeeds();
}

/** @var callable $callable */
$callable = [$manager, $method];
$backend = $this->getBackend();

return call_user_func_array($callable, $params);
return $backend->seed($options);
}

/**
Expand Down

0 comments on commit 02e70e1

Please sign in to comment.