diff --git a/src/Command/ModelAllCommand.php b/src/Command/ModelAllCommand.php index e43b4dd7..de842983 100644 --- a/src/Command/ModelAllCommand.php +++ b/src/Command/ModelAllCommand.php @@ -83,7 +83,8 @@ public function execute(Arguments $args, ConsoleIo $io): ?int /** @var \Cake\Database\Connection $connection */ $connection = ConnectionManager::get($this->connection); $scanner = new TableScanner($connection); - foreach ($scanner->listUnskipped() as $table) { + $tables = $this->removeShadowTranslationTables($scanner->listUnskipped()); + foreach ($tables as $table) { $this->getTableLocator()->clear(); $modelArgs = new Arguments([$table], $args->getOptions(), ['name']); $this->modelCommand->execute($modelArgs, $io); @@ -91,4 +92,25 @@ public function execute(Arguments $args, ConsoleIo $io): ?int return static::CODE_SUCCESS; } + + /** + * @param array $tables + * @return array + */ + protected function removeShadowTranslationTables(array $tables): array + { + foreach ($tables as $key => $table) { + if (!preg_match('/^(.+)_translations$/', $table, $matches)) { + continue; + } + + if (empty($tables[$matches[1]])) { + continue; + } + + unset($tables[$key]); + } + + return $tables; + } } diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 60c07959..89fb4004 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -1251,7 +1251,7 @@ public function bakeTable(Table $model, array $data, Arguments $args, ConsoleIo } /** - * Outputs the a list of possible models or controllers from database + * Outputs the list of possible models or controllers from database * * @return array */ @@ -1270,7 +1270,7 @@ public function listAll(): array } /** - * Outputs the a list of unskipped models or controllers from database + * Outputs the list of unskipped models or controllers from database * * @return array */ diff --git a/src/Utility/TableScanner.php b/src/Utility/TableScanner.php index 70f56d15..20e55d2c 100644 --- a/src/Utility/TableScanner.php +++ b/src/Utility/TableScanner.php @@ -58,13 +58,13 @@ public function __construct(Connection $connection, ?array $ignore = null) /** * Get all tables in the connection without applying ignores. * - * @return array + * @return array */ public function listAll(): array { $schema = $this->connection->getSchemaCollection(); $tables = $schema->listTables(); - if (empty($tables)) { + if (!$tables) { throw new RuntimeException('Your database does not have any tables.'); } sort($tables); @@ -75,7 +75,7 @@ public function listAll(): array /** * Get all tables in the connection that aren't ignored. * - * @return array + * @return array */ public function listUnskipped(): array { @@ -97,7 +97,7 @@ public function listUnskipped(): array protected function shouldSkip(string $table): bool { foreach ($this->ignore as $ignore) { - if (strpos($ignore, '/') === 0) { + if (str_starts_with($ignore, '/')) { if ((bool)preg_match($ignore, $table)) { return true; } diff --git a/tests/TestCase/Command/ModelAllCommandTest.php b/tests/TestCase/Command/ModelAllCommandTest.php index 96a852b4..e5ae2deb 100644 --- a/tests/TestCase/Command/ModelAllCommandTest.php +++ b/tests/TestCase/Command/ModelAllCommandTest.php @@ -16,6 +16,7 @@ */ namespace Bake\Test\TestCase\Command; +use Bake\Command\ModelAllCommand; use Bake\Test\TestCase\TestCase; use Bake\Utility\SubsetSchemaCollection; use Cake\Console\CommandInterface; @@ -99,4 +100,31 @@ public function testExecute() 'Table test should not be created as options should be forwarded' ); } + + /** + * @return void + */ + public function testRemoveShadowTranslationTables(): void + { + $class = new class extends ModelAllCommand { + public function removeShadowTranslationTables(array $tables): array + { + return parent::removeShadowTranslationTables($tables); + } + }; + + $tables = [ + 'items' => 'items', + 'users' => 'users', + 'users_translations' => 'users_translations', + 'item_translations' => 'item_translations', + ]; + $result = $class->removeShadowTranslationTables($tables); + $expected = [ + 'items' => 'items', + 'users' => 'users', + 'item_translations' => 'item_translations', + ]; + $this->assertEquals($expected, $result); + } }