Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ public function quoteTableName(string $tableName): string
*/
public function hasTable(string $tableName): bool
{
if ($this->hasCreatedTable($tableName)) {
// Only use the cache in dry-run mode where tables aren't actually created.
// In normal mode, always check the database to handle cases where tables
// are dropped via execute() which doesn't update the cache.
if ($this->isDryRunEnabled() && $this->hasCreatedTable($tableName)) {
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ public function quoteTableName(string $tableName): string
*/
public function hasTable(string $tableName): bool
{
if ($this->hasCreatedTable($tableName)) {
// Only use the cache in dry-run mode where tables aren't actually created.
// In normal mode, always check the database to handle cases where tables
// are dropped via execute() which doesn't update the cache.
if ($this->isDryRunEnabled() && $this->hasCreatedTable($tableName)) {
return true;
}

$parts = $this->getSchemaName($tableName);
$tableName = $parts['table'];

Expand Down
9 changes: 8 additions & 1 deletion src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,14 @@ protected function resolveTable(string $tableName): array
*/
public function hasTable(string $tableName): bool
{
return $this->hasCreatedTable($tableName) || $this->resolveTable($tableName)['exists'];
// Only use the cache in dry-run mode where tables aren't actually created.
// In normal mode, always check the database to handle cases where tables
// are dropped via execute() which doesn't update the cache.
if ($this->isDryRunEnabled() && $this->hasCreatedTable($tableName)) {
return true;
}

return $this->resolveTable($tableName)['exists'];
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ public function quoteTableName(string $tableName): string
*/
public function hasTable(string $tableName): bool
{
if ($this->hasCreatedTable($tableName)) {
// Only use the cache in dry-run mode where tables aren't actually created.
// In normal mode, always check the database to handle cases where tables
// are dropped via execute() which doesn't update the cache.
if ($this->isDryRunEnabled() && $this->hasCreatedTable($tableName)) {
return true;
}

$parts = $this->getSchemaName($tableName);
$dialect = $this->getSchemaDialect();

return $dialect->hasTable($tableName, $parts['schema']);
return $dialect->hasTable($parts['table'], $parts['schema']);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Db/Adapter/SqliteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,27 @@ public static function provideTableNamesForPresenceCheck()
];
}

/**
* Test that hasTable() returns false after a table is dropped via execute().
*
* This verifies that hasTable() always checks the database rather than
* relying on an internal cache that could become stale when raw SQL is used.
*/
public function testHasTableAfterExecuteDrop(): void
{
// Create table via API
$table = new Table('cache_test', [], $this->adapter);
$table->addColumn('name', 'string')
->save();

$this->assertTrue($this->adapter->hasTable('cache_test'));

// Drop via execute() - hasTable() must still return false
$this->adapter->execute('DROP TABLE "cache_test"');

$this->assertFalse($this->adapter->hasTable('cache_test'));
}

#[DataProvider('provideIndexColumnsToCheck')]
public function testHasIndex($tableDef, $cols, $exp)
{
Expand Down