Skip to content

Add wpdb fallback for wp db query, wp db import, wp db drop, and wp db reset when mysql/mariadb binary is unavailable#320

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/fix-wp-db-query-fallback
Draft

Add wpdb fallback for wp db query, wp db import, wp db drop, and wp db reset when mysql/mariadb binary is unavailable#320
Copilot wants to merge 7 commits intomainfrom
copilot/fix-wp-db-query-fallback

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

Several wp db commands currently require the mysql/mariadb CLI binary, causing failures with drop-in database engines (HyperDB, custom drivers) or environments where the binary simply isn't installed. This PR adds automatic fallback to WordPress's $wpdb for all relevant commands.

Changes

src/DB_Command.php

  • is_mysql_binary_available() — probes for the mysql/mariadb binary via /usr/bin/env <binary> --version; result is statically cached
  • maybe_load_wpdb() — loads the minimal WordPress files needed for $wpdb (load.php, compat.php, plugin.php, functions.php, class-wpdb.php), includes any wp-content/db.php drop-in (HyperDB et al.), and falls back to creating a plain wpdb instance with wp-config.php credentials + $table_prefix
  • run_query() — now checks binary availability at the top and, when absent, routes through maybe_load_wpdb() + $wpdb->query() instead of invoking the mysql CLI. This covers drop and reset.
  • query() — after the existing SQLite branch, checks binary availability and routes to the new wpdb path when absent via wpdb_query()
  • wpdb_query() — executes arbitrary SQL via $wpdb; mirrors the mysql-path behaviour (row-count reporting for DML, formatted tabular output for SELECT, --skip-column-names support)
  • import() — when the mysql binary is unavailable, reads the SQL file (or STDIN) and delegates to wpdb_import()
  • wpdb_import() — executes a SQL dump through $wpdb statement-by-statement, applying the same autocommit=0 / unique_checks=0 / foreign_key_checks=0 … COMMIT optimizations as the mysql path (unless --skip-optimization is passed)
  • split_sql_statements() — splits a SQL string into individual statements using a state-machine parser that correctly handles single-quoted strings, double-quoted strings, -- line comments, and /* */ block comments

features/db-query.feature

Added a @require-mysql-or-mariadb scenario that shadows the real binary with a fake exit 127 script via PATH prepending, then asserts the query succeeds and the debug message confirms the fallback was taken.

features/db.feature

Added @require-mysql-or-mariadb scenarios for wp db drop and wp db reset that shadow the binary with a fake exit 127 script and assert each command succeeds via the wpdb path (verified via the Query via wpdb: debug line).

features/db-import.feature

Added a @require-mysql-or-mariadb scenario that exports a real SQL dump first, then shadows the binary and re-imports it, asserting success and the MySQL/MariaDB binary not available, falling back to wpdb for import. debug message.

Note: wp db create is not covered by the wpdb fallback because wpdb requires the target database to already exist in order to establish a connection — a from-scratch create (no DB yet) cannot be handled via vanilla wpdb.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 35.81081% with 95 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/DB_Command.php 35.81% 95 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI changed the title [WIP] Fix wp db query to fallback using WPDB Add wpdb fallback for wp db query when mysql/mariadb binary is unavailable Apr 2, 2026
Copilot AI requested a review from swissspidy April 2, 2026 21:31
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

Copilot AI changed the title Add wpdb fallback for wp db query when mysql/mariadb binary is unavailable Add wpdb fallback for wp db query, wp db import, wp db create, wp db drop, and wp db reset when mysql/mariadb binary is unavailable Apr 3, 2026
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

Copilot AI changed the title Add wpdb fallback for wp db query, wp db import, wp db create, wp db drop, and wp db reset when mysql/mariadb binary is unavailable Add wpdb fallback for wp db query, wp db import, wp db drop, and wp db reset when mysql/mariadb binary is unavailable Apr 3, 2026
Copilot AI requested a review from swissspidy April 3, 2026 09:12
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

query shouldn't require mysql/mariadb cli tools

2 participants