Skip to content

Commit

Permalink
Merge pull request #21 from vierge-noire/#20_migration_status_access
Browse files Browse the repository at this point in the history
#20 Introduces a getConnectionsWithModifiedStatus method.

  • Loading branch information
pabloelcolombiano authored Jul 26, 2021
2 parents 8fe35e7 + 5ec1b4c commit 00afc40
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Migrator
*/
protected $io;

/**
* @var string[]
*/
protected $connectionsWithModifiedStatus = [];

/**
* Migrator constructor.
* @param bool $verbose
Expand Down Expand Up @@ -132,34 +137,33 @@ protected function runMigrations(array $config): void
protected function handleMigrationsStatus(): self
{
$schemaCleaner = new SchemaCleaner($this->io);
$connectionsToDrop = [];
foreach ($this->getConfigs() as &$config) {
$connectionName = $config['connection'] = $config['connection'] ?? 'test';
$this->io->info("Reading migrations status for {$this->stringifyConfig($config)}...");
$migrations = new Migrations($config);
if ($this->isStatusChanged($migrations)) {
if (!in_array($connectionName, $connectionsToDrop))
if (!in_array($connectionName, $this->connectionsWithModifiedStatus))
{
$connectionsToDrop[] = $connectionName;
$this->connectionsWithModifiedStatus[] = $connectionName;
}
}
}

if (empty($connectionsToDrop)) {
if (empty($this->connectionsWithModifiedStatus)) {
$this->io->success("No migration changes detected.");

return $this;
}

foreach ($connectionsToDrop as $connectionName) {
foreach ($this->connectionsWithModifiedStatus as $connectionName) {
$schemaCleaner->drop($connectionName);
}

foreach ($this->getConfigs() as $config) {
$this->runMigrations($config);
}

foreach ($connectionsToDrop as $connectionName) {
foreach ($this->connectionsWithModifiedStatus as $connectionName) {
$schemaCleaner->truncate($connectionName);
}

Expand Down Expand Up @@ -221,4 +225,15 @@ protected function getConfigReader(): ConfigReader
{
return $this->configReader;
}

/**
* Returns an array of strings with all the connections
* which migration status have changed and were migrated.
*
* @return string[]
*/
public function getConnectionsWithModifiedStatus(): array
{
return $this->connectionsWithModifiedStatus;
}
}
28 changes: 28 additions & 0 deletions tests/TestCase/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\TestSuite\TestCase;
use Cake\Utility\Hash;
use CakephpTestMigrator\Migrator;
use CakephpTestMigrator\SchemaCleaner;

class MigratorTest extends TestCase
{
Expand Down Expand Up @@ -60,6 +61,33 @@ public function testMigrate(): void
$this->assertSame(['BarMigration'], $barPluginMigrations);
}

public function testGetConnectionsWithModifiedStatus(): void
{
$connections = [
'test',
'test_2',
'test_3',
];

// Run the migrations once to make sure that all are up to date.
Migrator::migrate();

// Status did not changed.
$migrator = Migrator::migrate();
$this->assertSame([], $migrator->getConnectionsWithModifiedStatus());

// Drop all connections' tables. Statuses are reset.
$cleaner = new SchemaCleaner();
foreach ($connections as $connection) {
$cleaner->drop($connection);
}

// All connections were touched by the migrations.
$migrator = Migrator::migrate();
$connectionsWithModifiedStatus = $migrator->getConnectionsWithModifiedStatus();
$this->assertSame($connections, $connectionsWithModifiedStatus);
}

public function testTableRegistryConnectionName(): void
{
$Articles = TableRegistry::getTableLocator()->get('Articles');
Expand Down

0 comments on commit 00afc40

Please sign in to comment.