Skip to content

Conversation

@dereuromark
Copy link
Member

@dereuromark dereuromark commented Dec 26, 2025

Summary

  • Fix hasTable() returning stale results when mixing API calls with raw SQL via execute()
  • When a table is created via API (e.g. $this->table()->create()) and dropped via execute(), hasTable() would incorrectly return true due to an internal cache
  • The cache is now only used in dry-run mode where it's needed to track what tables "would" exist

Problem

// In a migration
$this->table('foo')->addColumn('bar', 'string')->create();
$this->table('foo')->exists();  // true ✓

$this->execute('DROP TABLE foo');
$this->table('foo')->exists();  // true ✗ (should be false!)

The $createdTables cache was being checked first in hasTable(), returning true without querying the database.

Solution

Only use the cache in dry-run mode. In normal execution, always query the database for accurate results.

Note:
There was also a 2nd issue in code.
The issue was a latent bug in SqlserverAdapter - it was passing the full nschema.ntable to dialect->hasTable() instead of just ntable. The cache was hiding this bug before.

When a table was created via the API (e.g. $this->table()->create()) and
then dropped via execute() (raw SQL), hasTable() would incorrectly return
true because it checked an internal cache before querying the database.

This fix limits the cache usage to dry-run mode only, where it's necessary
to track what tables "would" exist. In normal mode, hasTable() now always
queries the database to ensure accurate results.

Also fixes SqlserverAdapter to pass the table name without schema prefix
to the dialect, which was a latent bug hidden by the cache.
@dereuromark dereuromark force-pushed the fix-hasTable-cache-stale-after-execute branch from bb2a48f to d9dfd8c Compare December 26, 2025 23:03
@markstory markstory merged commit 037354b into 5.x Dec 28, 2025
14 checks passed
@markstory markstory deleted the fix-hasTable-cache-stale-after-execute branch December 28, 2025 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants