From 5ec1b4c2fb10d790860cbfa955df030b8ba1ee81 Mon Sep 17 00:00:00 2001 From: Juan Pablo Ramirez Date: Sat, 24 Jul 2021 16:11:04 +0200 Subject: [PATCH] =?UTF-8?q?#20=20Introduces=20a=20getConnectionsWithModifi?= =?UTF-8?q?edStatus=20method.=E2=80=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Migrator.php | 27 +++++++++++++++++++++------ tests/TestCase/MigratorTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Migrator.php b/src/Migrator.php index 8d21e0c..1035067 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -30,6 +30,11 @@ class Migrator */ protected $io; + /** + * @var string[] + */ + protected $connectionsWithModifiedStatus = []; + /** * Migrator constructor. * @param bool $verbose @@ -132,26 +137,25 @@ 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); } @@ -159,7 +163,7 @@ protected function handleMigrationsStatus(): self $this->runMigrations($config); } - foreach ($connectionsToDrop as $connectionName) { + foreach ($this->connectionsWithModifiedStatus as $connectionName) { $schemaCleaner->truncate($connectionName); } @@ -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; + } } diff --git a/tests/TestCase/MigratorTest.php b/tests/TestCase/MigratorTest.php index cfeb919..cfcfb47 100644 --- a/tests/TestCase/MigratorTest.php +++ b/tests/TestCase/MigratorTest.php @@ -19,6 +19,7 @@ use Cake\TestSuite\TestCase; use Cake\Utility\Hash; use CakephpTestMigrator\Migrator; +use CakephpTestMigrator\SchemaCleaner; class MigratorTest extends TestCase { @@ -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');