Skip to content

Commit

Permalink
Get more tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 30, 2024
1 parent 131c717 commit 0629a5e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
49 changes: 25 additions & 24 deletions src/Migration/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Migrations\Db\Adapter\PhinxAdapter;

Check failure on line 15 in src/Migration/Environment.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Migrations\Db\Adapter\PhinxAdapter is not used in this file.
use Migrations\SeedInterface;
use Migrations\MigrationInterface;

Check failure on line 17 in src/Migration/Environment.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Use statements should be sorted alphabetically. The first wrong one is Migrations\MigrationInterface.
use Migrations\Shim\MigrationAdapter;
use RuntimeException;

class Environment
Expand Down Expand Up @@ -91,32 +92,32 @@ public function executeMigration(MigrationInterface $migration, string $directio
}

if (!$fake) {
// TODO this is tricky as the adapter would need to implement
// this method, but then it always exists. One option is to copy this
// is to special case the adapter and move this logic there?
// 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());
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
12 changes: 10 additions & 2 deletions src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,19 @@ public function markMigrated(int $version, string $path): bool
/** @var class-string<\Phinx\Migration\MigrationInterface> $className */
$className = $this->getMigrationClassName($migrationFile);
require_once $migrationFile;
$Migration = new $className('default', $version);

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

Check failure on line 264 in src/Migration/Manager.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Else branch is unreachable because previous condition is always true.
$migration = new $className($version);
}
$config = $this->getConfig();
$migration->setConfig($config);

Check failure on line 268 in src/Migration/Manager.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyUndefinedMethod

src/Migration/Manager.php:268:21: PossiblyUndefinedMethod: Method Phinx\Migration\MigrationInterface::setConfig does not exist (see https://psalm.dev/108)

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

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

Check failure on line 272 in src/Migration/Manager.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyInvalidArgument

src/Migration/Manager.php:272:28: PossiblyInvalidArgument: Argument 1 of Migrations\Db\Adapter\AdapterInterface::migrated expects Migrations\MigrationInterface, but possibly different type Migrations\Shim\MigrationAdapter|Phinx\Migration\MigrationInterface provided (see https://psalm.dev/092)

return true;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Shim/MigrationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Migrations\Db\Adapter\PhinxAdapter;
use Migrations\Db\Table;
use Migrations\MigrationInterface;
use Phinx\Db\Adapter\AdapterFactory as PhinxAdapterFactory;
use Phinx\Migration\MigrationInterface as PhinxMigrationInterface;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down Expand Up @@ -101,6 +102,40 @@ public function init(): void
}
}

/**
* Compatibility shim for executing change/up/down
*/
public function applyDirection(string $direction): void
{
$adapter = $this->getAdapter();

// Run the migration
if (method_exists($this->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
$adapter = $this->migration->getAdapter();
assert($adapter !== null, 'Adapter must be set in migration');

/** @var \Phinx\Db\Adapter\ProxyAdapter $proxyAdapter */
$proxyAdapter = PhinxAdapterFactory::instance()
->getWrapper('proxy', $adapter);

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

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

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

/**
* {@inheritDoc}
*/
Expand Down
18 changes: 12 additions & 6 deletions tests/TestCase/Migration/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public function up(): void
}
};

$this->environment->executeMigration($upMigration, MigrationInterface::UP);
$migrationWrapper = new MigrationAdapter($upMigration, $upMigration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::UP);
$this->assertTrue($upMigration->executed);
}

Expand All @@ -156,7 +157,8 @@ public function down(): void
}
};

$this->environment->executeMigration($downMigration, MigrationInterface::DOWN);
$migrationWrapper = new MigrationAdapter($downMigration, $downMigration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::DOWN);
$this->assertTrue($downMigration->executed);
}

Expand Down Expand Up @@ -187,7 +189,8 @@ public function up(): void
}
};

$this->environment->executeMigration($migration, MigrationInterface::UP);
$migrationWrapper = new MigrationAdapter($migration, $migration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::UP);
$this->assertTrue($migration->executed);
}

Expand All @@ -212,7 +215,8 @@ public function change(): void
}
};

$this->environment->executeMigration($migration, MigrationInterface::UP);
$migrationWrapper = new MigrationAdapter($migration, $migration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::UP);
$this->assertTrue($migration->executed);
}

Expand All @@ -237,7 +241,8 @@ public function change(): void
}
};

$this->environment->executeMigration($migration, MigrationInterface::DOWN);
$migrationWrapper = new MigrationAdapter($migration, $migration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::DOWN);
$this->assertTrue($migration->executed);
}

Expand All @@ -262,7 +267,8 @@ public function change(): void
}
};

$this->environment->executeMigration($migration, MigrationInterface::UP, true);
$migrationWrapper = new MigrationAdapter($migration, $migration->getVersion());
$this->environment->executeMigration($migrationWrapper, MigrationInterface::UP, true);
$this->assertFalse($migration->executed);
}

Expand Down

0 comments on commit 0629a5e

Please sign in to comment.