Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations adapter #752

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Migrations\Db\Literal;
use Migrations\Db\Table\Column;
use Migrations\Db\Table\Table;
use Phinx\Migration\MigrationInterface;
use Migrations\MigrationInterface;

/**
* Adapter Interface.
Expand Down Expand Up @@ -138,7 +138,7 @@ public function getColumnForType(string $columnName, string $type, array $option
/**
* Records a migration being run.
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @param string $direction Direction
* @param string $startTime Start Time
* @param string $endTime End Time
Expand All @@ -149,7 +149,7 @@ public function migrated(MigrationInterface $migration, string $direction, strin
/**
* Toggle a migration breakpoint.
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @return $this
*/
public function toggleBreakpoint(MigrationInterface $migration);
Expand All @@ -164,15 +164,15 @@ public function resetAllBreakpoints(): int;
/**
* Set a migration breakpoint.
*
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint set
* @param \Migrations\MigrationInterface $migration The migration target for the breakpoint set
* @return $this
*/
public function setBreakpoint(MigrationInterface $migration);

/**
* Unset a migration breakpoint.
*
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint unset
* @param \Migrations\MigrationInterface $migration The migration target for the breakpoint unset
* @return $this
*/
public function unsetBreakpoint(MigrationInterface $migration);
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Adapter/AdapterWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Migrations\Db\Literal;
use Migrations\Db\Table\Column;
use Migrations\Db\Table\Table;
use Phinx\Migration\MigrationInterface;
use Migrations\MigrationInterface;

/**
* Adapter Wrapper.
Expand Down
4 changes: 2 additions & 2 deletions src/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
use Migrations\Db\Table\ForeignKey;
use Migrations\Db\Table\Index;
use Migrations\Db\Table\Table;
use Migrations\MigrationInterface;
use PDO;
use PDOException;
use Phinx\Config\Config;
use Phinx\Migration\MigrationInterface;
use Phinx\Util\Literal as PhinxLiteral;
use ReflectionMethod;
use RuntimeException;
Expand Down Expand Up @@ -570,7 +570,7 @@ public function unsetBreakpoint(MigrationInterface $migration): AdapterInterface
/**
* Mark a migration breakpoint.
*
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint
* @param \Migrations\MigrationInterface $migration The migration target for the breakpoint
* @param bool $state The required state of the breakpoint
* @return \Migrations\Db\Adapter\AdapterInterface
*/
Expand Down
23 changes: 14 additions & 9 deletions src/Db/Adapter/PhinxAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Migrations\Db\Table\ForeignKey;
use Migrations\Db\Table\Index;
use Migrations\Db\Table\Table;
use Migrations\Shim\MigrationAdapter;
use Phinx\Db\Action\Action as PhinxAction;
use Phinx\Db\Action\AddColumn as PhinxAddColumn;
use Phinx\Db\Action\AddForeignKey as PhinxAddForeignKey;
Expand All @@ -52,7 +53,7 @@
use Phinx\Db\Table\ForeignKey as PhinxForeignKey;
use Phinx\Db\Table\Index as PhinxIndex;
use Phinx\Db\Table\Table as PhinxTable;
use Phinx\Migration\MigrationInterface;
use Phinx\Migration\MigrationInterface as PhinxMigrationInterface;
use Phinx\Util\Literal as PhinxLiteral;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -491,19 +492,21 @@ public function getVersionLog(): array
/**
* @inheritDoc
*/
public function migrated(MigrationInterface $migration, string $direction, string $startTime, string $endTime): PhinxAdapterInterface
public function migrated(PhinxMigrationInterface $migration, string $direction, string $startTime, string $endTime): PhinxAdapterInterface
{
$this->adapter->migrated($migration, $direction, $startTime, $endTime);
$wrapped = new MigrationAdapter($migration, $migration->getVersion());
$this->adapter->migrated($wrapped, $direction, $startTime, $endTime);

return $this;
}

/**
* @inheritDoc
*/
public function toggleBreakpoint(MigrationInterface $migration): PhinxAdapterInterface
public function toggleBreakpoint(PhinxMigrationInterface $migration): PhinxAdapterInterface
{
$this->adapter->toggleBreakpoint($migration);
$wrapped = new MigrationAdapter($migration, $migration->getVersion());
$this->adapter->toggleBreakpoint($wrapped);

return $this;
}
Expand All @@ -519,19 +522,21 @@ public function resetAllBreakpoints(): int
/**
* @inheritDoc
*/
public function setBreakpoint(MigrationInterface $migration): PhinxAdapterInterface
public function setBreakpoint(PhinxMigrationInterface $migration): PhinxAdapterInterface
{
$this->adapter->setBreakpoint($migration);
$wrapped = new MigrationAdapter($migration, $migration->getVersion());
$this->adapter->setBreakpoint($wrapped);

return $this;
}

/**
* @inheritDoc
*/
public function unsetBreakpoint(MigrationInterface $migration): PhinxAdapterInterface
public function unsetBreakpoint(PhinxMigrationInterface $migration): PhinxAdapterInterface
{
$this->adapter->unsetBreakpoint($migration);
$wrapped = new MigrationAdapter($migration, $migration->getVersion());
$this->adapter->unsetBreakpoint($wrapped);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Migrations\Db\Table\ForeignKey;
use Migrations\Db\Table\Index;
use Migrations\Db\Table\Table;
use Phinx\Migration\MigrationInterface;
use Migrations\MigrationInterface;

/**
* Migrations SqlServer Adapter.
Expand Down Expand Up @@ -1238,7 +1238,7 @@ public function getColumnTypes(): array
/**
* Records a migration being run.
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @param string $direction Direction
* @param string $startTime Start Time
* @param string $endTime End Time
Expand Down
55 changes: 29 additions & 26 deletions src/Migration/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Cake\Datasource\ConnectionManager;
use Migrations\Db\Adapter\AdapterFactory;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Db\Adapter\PhinxAdapter;
use Migrations\MigrationInterface;
use Migrations\SeedInterface;
use Phinx\Migration\MigrationInterface;
use Migrations\Shim\MigrationAdapter;
use RuntimeException;

class Environment
Expand Down Expand Up @@ -62,7 +62,7 @@ public function __construct(string $name, array $options)
/**
* Executes the specified migration on this environment.
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @param string $direction Direction
* @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
* @return void
Expand All @@ -73,11 +73,11 @@ public function executeMigration(MigrationInterface $migration, string $directio
$migration->setMigratingUp($direction === MigrationInterface::UP);

$startTime = time();

// Use an adapter shim to bridge between the new migrations
// engine and the Phinx compatible interface
$adapter = $this->getAdapter();
$phinxShim = new PhinxAdapter($adapter);
$migration->setAdapter($phinxShim);
$migration->setAdapter($adapter);

$migration->preFlightCheck();

Expand All @@ -91,29 +91,32 @@ public function executeMigration(MigrationInterface $migration, string $directio
}

if (!$fake) {
// Run the migration
if (method_exists($migration, MigrationInterface::CHANGE)) {
if ($direction === MigrationInterface::DOWN) {
// Create an instance of the RecordingAdapter so we can record all
// of the migration commands for reverse playback

/** @var \Migrations\Db\Adapter\RecordingAdapter $recordAdapter */
$recordAdapter = AdapterFactory::instance()
->getWrapper('record', $adapter);

// Wrap the adapter with a phinx shim to maintain contain
$phinxAdapter = new PhinxAdapter($recordAdapter);
$migration->setAdapter($phinxAdapter);

$migration->{MigrationInterface::CHANGE}();
$recordAdapter->executeInvertedCommands();

$migration->setAdapter(new PhinxAdapter($this->getAdapter()));
if ($migration instanceof MigrationAdapter) {
$migration->applyDirection($direction);
} else {
// Run the migration
if (method_exists($migration, MigrationInterface::CHANGE)) {
if ($direction === MigrationInterface::DOWN) {
// Create an instance of the RecordingAdapter so we can record all
// of the migration commands for reverse playback

/** @var \Migrations\Db\Adapter\RecordingAdapter $recordAdapter */
$recordAdapter = AdapterFactory::instance()
->getWrapper('record', $adapter);

// Wrap the adapter with a phinx shim to maintain contain
$migration->setAdapter($adapter);

$migration->{MigrationInterface::CHANGE}();
$recordAdapter->executeInvertedCommands();

$migration->setAdapter($this->getAdapter());
} else {
$migration->{MigrationInterface::CHANGE}();
}
} else {
$migration->{MigrationInterface::CHANGE}();
$migration->{$direction}();
}
} else {
$migration->{$direction}();
}
}

Expand Down
63 changes: 28 additions & 35 deletions src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
use Exception;
use InvalidArgumentException;
use Migrations\Config\ConfigInterface;
use Migrations\MigrationInterface;
use Migrations\SeedInterface;
use Migrations\Shim\OutputAdapter;
use Migrations\Shim\MigrationAdapter;
use Migrations\Shim\SeedAdapter;
use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface;
use Phinx\Migration\MigrationInterface as PhinxMigrationInterface;
use Phinx\Seed\SeedInterface as PhinxSeedInterface;
use Phinx\Util\Util;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;

class Manager
{
Expand All @@ -47,7 +46,7 @@ class Manager
protected ?Environment $environment;

/**
* @var \Phinx\Migration\MigrationInterface[]|null
* @var \Migrations\MigrationInterface[]|null
*/
protected ?array $migrations = null;

Expand Down Expand Up @@ -255,14 +254,22 @@ public function markMigrated(int $version, string $path): bool
}

$migrationFile = $migrationFile[0];
/** @var class-string<\Phinx\Migration\MigrationInterface> $className */
/** @var class-string<\Phinx\Migration\MigrationInterface|\Migrations\MigrationInterface> $className */
$className = $this->getMigrationClassName($migrationFile);
require_once $migrationFile;
$Migration = new $className('default', $version);

if (is_subclass_of($className, PhinxMigrationInterface::class)) {
$migration = new MigrationAdapter($className, $version);
} else {
$migration = new $className($version);
}
/** @var \Migrations\MigrationInterface $migration */
$config = $this->getConfig();
$migration->setConfig($config);

$time = date('Y-m-d H:i:s', time());

$adapter->migrated($Migration, 'up', $time, $time);
$adapter->migrated($migration, 'up', $time, $time);

return true;
}
Expand Down Expand Up @@ -441,7 +448,7 @@ public function migrate(?int $version = null, bool $fake = false): void
/**
* Execute a migration against the specified environment.
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @param string $direction Direction
* @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
* @return void
Expand Down Expand Up @@ -505,7 +512,7 @@ public function executeSeed(SeedInterface $seed): void
/**
* Print Migration Status
*
* @param \Phinx\Migration\MigrationInterface $migration Migration
* @param \Migrations\MigrationInterface $migration Migration
* @param string $status Status of the migration
* @param string|null $duration Duration the migration took the be executed
* @return void
Expand Down Expand Up @@ -771,7 +778,7 @@ public function setContainer(ContainerInterface $container)
/**
* Sets the database migrations.
*
* @param \Phinx\Migration\AbstractMigration[] $migrations Migrations
* @param \Migrations\MigrationInterface[] $migrations Migrations
* @return $this
*/
public function setMigrations(array $migrations)
Expand All @@ -786,7 +793,7 @@ public function setMigrations(array $migrations)
* order
*
* @throws \InvalidArgumentException
* @return \Phinx\Migration\MigrationInterface[]
* @return \Migrations\MigrationInterface[]
*/
public function getMigrations(): array
{
Expand All @@ -806,7 +813,7 @@ function ($phpFile) {

// filter the files to only get the ones that match our naming scheme
$fileNames = [];
/** @var \Phinx\Migration\AbstractMigration[] $versions */
/** @var \Migrations\MigrationInterface[] $versions */
$versions = [];

$io = $this->getIo();
Expand Down Expand Up @@ -850,29 +857,15 @@ function ($phpFile) {
}

$io->verbose("Constructing <info>$class</info>.");

$config = $this->getConfig();
// TODO Subset config and pass forward.
// Move this to the Migration/phinx shim
$input = new ArrayInput([
'--plugin' => $config['plugin'] ?? null,
'--source' => $config['source'] ?? null,
'--connection' => $config->getConnection(),
]);
// TODO move this to the migration/phinx shim
$output = new OutputAdapter($io);

// TODO constructor should take $io and $config
// instantiate it
$migration = new $class('default', $version, $input, $output);

if (!($migration instanceof AbstractMigration)) {
throw new InvalidArgumentException(sprintf(
'The class "%s" in file "%s" must extend \Phinx\Migration\AbstractMigration',
$class,
$filePath
));
if (is_subclass_of($class, PhinxMigrationInterface::class)) {
$migration = new MigrationAdapter($class, $version);
} else {
$migration = new $class($version);
}
/** @var \Migrations\MigrationInterface $migration */
$config = $this->getConfig();
$migration->setConfig($config);
$migration->setIo($io);

$versions[$version] = $migration;
} else {
Expand Down
Loading
Loading