Fix false "SITE IS DISCONNECTED" error on empty query results - #1966
Open
anamwp wants to merge 2 commits into
Open
Fix false "SITE IS DISCONNECTED" error on empty query results#1966anamwp wants to merge 2 commits into
anamwp wants to merge 2 commits into
Conversation
wp stream query treated any empty result set as a disconnected site, since db->query() returns array() for both "no matching records" and implicitly for failures. This misfired on any site with no logged activity yet (e.g. a fresh install or an untouched multisite subsite), as reported in xwp#1829. Check $wpdb->last_error instead, which WordPress only populates on an actual database error, so a legitimately empty result no longer trips the disconnected check.
Covers both branches of the fix from the previous commit: a query that legitimately matches zero records should not trip WP_CLI::error(), while a genuine database failure still should. Uses WP_CLI's capture_exit mechanism (via reflection, since it's private) to catch the resulting ExitException instead of terminating the test process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1829
Problem
wp stream queryruns a connectivity check (CLI::connection()) before executing, by running a real query for 1 record and treating a falsy result as "disconnected":DB::get_records()returns a plainarray()when the query runs successfully but matches zero rows. In PHP, an empty array is falsy, so "no records yet" and "the query actually failed" were indistinguishable — any site with an emptywp_streamtable (a fresh install, or a multisite subsite with no site-level activity logged) hitSITE IS DISCONNECTEDeven though nothing was actually wrong.This matches the report in #1829: one multisite install logs plenty of activity and works fine, while a second logs almost nothing (only network-level actions) and fails on every
wp stream querycall.Fix
Check
$wpdb->last_errorinstead of the query's return shape. WordPress only populateslast_errorwhen a query genuinely fails at the database level (bad SQL, missing table, dropped connection, etc.), never for a query that simply matched nothing. This restores the original intent of the check — "is the database actually reachable" — without misreading an empty-but-healthy result as a failure.Testing
wp stream query --url=<subsite>→SITE IS DISCONNECTED(confirmed the root cause, not just multisite-specific — a single-site install with an emptywp_streamtable hits the same bug).wp_streamtable and confirmedSITE IS DISCONNECTEDis still reported, then restored the table.composer lint(PHPCS): clean.Test_CLIcoverage added for the fix (both the empty-result and genuine-failure paths):Checklist
contributing.md).Release Changelog
wp stream queryno longer reports "SITE IS DISCONNECTED" when a site simply has no logged activity yet.