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 59219f4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Command/AllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get($this->connection);
$scanner = new TableScanner($connection);
$tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped());

if (!$name && !$args->getOption('everything')) {
$io->out('Choose a table to generate from the following:');
foreach ($scanner->listUnskipped() as $table) {
foreach ($tables as $table) {

Check warning on line 91 in src/Command/AllCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/AllCommand.php#L91

Added line #L91 was not covered by tests
$io->out('- ' . $this->_camelize($table));
}

return static::CODE_SUCCESS;
}
if ($args->getOption('everything')) {
$tables = $scanner->listUnskipped();
} else {
if (!$args->getOption('everything')) {
$tables = [$name];
}

Expand Down
4 changes: 3 additions & 1 deletion src/Command/FixtureAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
$connection = ConnectionManager::get($args->getOption('connection') ?? 'default');
$scanner = new TableScanner($connection);
$fixture = new FixtureCommand();
foreach ($scanner->listUnskipped() as $table) {

$tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped());
foreach ($tables as $table) {
$fixtureArgs = new Arguments([$table], $args->getOptions(), ['name']);
$fixture->execute($fixtureArgs, $io);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Command/ModelAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $scanner->removeShadowTranslationTables($scanner->listUnskipped());
foreach ($tables as $table) {
$this->getTableLocator()->clear();
$modelArgs = new Arguments([$table], $args->getOptions(), ['name']);
$this->modelCommand->execute($modelArgs, $io);
Expand Down
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
3 changes: 2 additions & 1 deletion src/Command/TemplateAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function execute(Arguments $args, ConsoleIo $io): int
$connection = ConnectionManager::get($this->connection);
$scanner = new TableScanner($connection);

foreach ($scanner->listUnskipped() as $table) {
$tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped());
foreach ($tables as $table) {
$parser = $this->templateCommand->getOptionParser();
$templateArgs = new Arguments(
[$table],
Expand Down
31 changes: 27 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 @@ -90,14 +90,37 @@ public function listUnskipped(): array
return $tables;
}

/**
* Call from any All command that needs the shadow translation tables to be skipped.
*
* @param array<string, string> $tables
* @return array<string, string>
*/
public 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;
}

/**
* @param string $table Table name.
* @return bool
*/
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
22 changes: 22 additions & 0 deletions tests/TestCase/Utility/TableScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,26 @@ public function testListUnskippedRegex()
}
}
}

/**
* @return void
*/
public function testRemoveShadowTranslationTables(): void
{
$this->tableScanner = new TableScanner($this->connection);

$tables = [
'items' => 'items',
'users' => 'users',
'users_translations' => 'users_translations',
'item_translations' => 'item_translations',
];
$result = $this->tableScanner->removeShadowTranslationTables($tables);
$expected = [
'items' => 'items',
'users' => 'users',
'item_translations' => 'item_translations',
];
$this->assertEquals($expected, $result);
}
}

0 comments on commit 59219f4

Please sign in to comment.