Skip to content

fix: guard CLI cleanup and DB viewer with wp_presence_has_table() - #257

Closed
aldorizona10-glitch wants to merge 1 commit into
WordPress:mainfrom
aldorizona10-glitch:fix/210-availability-check-cli-db-viewer
Closed

aldorizona10-glitch wants to merge 1 commit into
WordPress:mainfrom
aldorizona10-glitch:fix/210-availability-check-cli-db-viewer

Conversation

@aldorizona10-glitch

Copy link
Copy Markdown
Contributor

What & why

Resolves #210. The WP-CLI cleanup command (includes/cli/class-wp-presence-cli-command.php) and the debug DB viewer (includes/db-viewer.php) were the only two paths that hit the presence table directly without a wp_presence_has_table() check. Every other read/write path in functions.php and the REST controller guards on it and degrades to an empty result, so these two were the last places that would surface a raw wpdb error if the table were ever missing.

As #210 notes, cli_init / admin_init provision the table before either can run, so this is a consistency fix, not a live bug — it was deliberately left out of #203 to keep that change scoped.

Changes

  • db-viewer.php — wrap the SELECT / COUNT(*) queries in the availability check; when the table is absent, fall back to an empty row set and a zero count. The template already renders an empty state (No entries.) and the timestamp foreach is empty()-guarded, so no markup changes are needed.
  • cli cleanup() — short-circuit to a 0 entries deleted. success before the destructive DELETE (and before the confirmation prompt, since there is nothing to delete).

No new strings, no schema changes, no behavior change on a provisioned site.

Testing

WP-CLI is not loaded in the PHPUnit suite and CLI coverage is being handled separately (#218 / #256), so I've kept this change focused on the guards rather than adding CLI-invoking tests here. Happy to add coverage if you'd prefer a particular approach.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Investigating the guard pattern and drafting the change

The WP-CLI `cleanup` command and the debug DB viewer were the only two
read/write paths that queried the presence table directly without first
checking wp_presence_has_table(). Every other path in functions.php and
the REST controller guards on it and degrades to an empty result; these
two would surface a raw wpdb error if the table were ever missing.

In practice cli_init / admin_init provision the table before either can
run, so this is a consistency fix rather than a live bug (as noted in
 WordPress#210, deliberately left out of WordPress#203 to keep that change scoped).

- db-viewer.php: wrap the SELECT/COUNT queries; when the table is absent,
  fall back to an empty row set and a zero count. The template already
  renders an empty state ("No entries."), so no markup changes are needed.
- cli cleanup(): short-circuit to a "0 entries deleted" success before the
  destructive DELETE (and before the confirmation prompt, since there is
  nothing to delete).

Fixes WordPress#210

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Investigating the guard pattern and drafting the change
@github-actions

Copy link
Copy Markdown
Contributor

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aldorza, iamchitti, joefusco.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown
Contributor

▶ Preview in WordPress Playground

Open in WordPress Playground

Boots a fresh WordPress with this PR's presence-api build, seeds 5 demo users, and drops you on the dashboard.

Stress-test variant: 40 demo users · Built from c340132c0db848a44d5dfb3f9107c680a3378acc. Auto-updates when you push.

@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.39%. Comparing base (bfb701f) to head (c340132).

❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #257      +/-   ##
============================================
- Coverage     72.51%   72.39%   -0.12%     
- Complexity      175      176       +1     
============================================
  Files            14       14              
  Lines          1819     1822       +3     
============================================
  Hits           1319     1319              
- Misses          500      503       +3     
Flag Coverage Δ
multisite 72.39% <0.00%> (-0.12%) ⬇️
phpunit 71.07% <0.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aldorizona10-glitch

Copy link
Copy Markdown
Contributor Author

@i-am-chitti you're right, and apologies for the duplication. I went straight to a PR without commenting on #210 to claim it first, which is exactly what CONTRIBUTING asks — that's on me. Since you called it first and already had it written, I'll close this in favor of your PR. I'll comment to claim before starting next time. Thanks for the nudge.

@josephfusco josephfusco added [Type] Enhancement A suggestion for improvement of an existing feature [Area] CLI Issues for the WP-CLI commands [Area] Database Issues for the wp_presence table and cron cleanup php Pull requests that update php code labels Aug 11, 2026
@i-am-chitti

Copy link
Copy Markdown
Collaborator

Thanks @aldorizona10-glitch for the graceful close. We'd independently landed on almost the same guard, which was oddly reassuring.
Opening mine shortly. Happy for you to be in props on it since you did the same work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Area] CLI Issues for the WP-CLI commands [Area] Database Issues for the wp_presence table and cron cleanup php Pull requests that update php code [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI and debug viewer query the presence table without an availability check

3 participants