Skip to content

Commit

Permalink
ignore shadow translations tables *_translations
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 29, 2024
1 parent 0f28cba commit 84299d9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/Command/ModelAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,34 @@ 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);
}

return static::CODE_SUCCESS;
}

/**
* @param array<string, string> $tables
* @return array<string, string>
*/
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;
}
}
4 changes: 2 additions & 2 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
Expand All @@ -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<string>
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Utility/TableScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function __construct(Connection $connection, ?array $ignore = null)
/**
* Get all tables in the connection without applying ignores.
*
* @return array<string>
* @return array<string, string>
*/
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);
Expand All @@ -75,7 +75,7 @@ public function listAll(): array
/**
* Get all tables in the connection that aren't ignored.
*
* @return array<string>
* @return array<string, string>
*/
public function listUnskipped(): array
{
Expand All @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Command/ModelAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 84299d9

Please sign in to comment.