wp-cli-tests icon indicating copy to clipboard operation
wp-cli-tests copied to clipboard

Fix database type detection to query server version instead of client binary

Open Copilot opened this issue 2 weeks ago • 6 comments
trafficstars

Fix MariaDB detection to query server version instead of client version

Problem: The current implementation queries the version of the mysql/mariadb client binary, but we need the database server version to determine the correct SQL dialect.

Changes Made:

  • [x] Analyze current get_db_version() function in utils/behat-tags.php
  • [x] Implement detection of available client binary (mysql, mariadb, etc.)
  • [x] Update function to connect to database server and retrieve server version
  • [x] Extract DB type (MySQL vs MariaDB) from server version string
  • [x] Update bin/install-package-tests script to use same approach
  • [x] Test changes with manual verification
  • [x] Verify the implementation works correctly
  • [x] Address code review feedback
  • [x] Fix PHPCS formatting issues
  • [x] Fix test failure by falling back to client version when server unavailable

Implementation Details:

  1. utils/behat-tags.php:

    • Added get_db_type_and_version() function that:
      • Detects available client binary (mysql or mariadb)
      • Properly initializes output arrays for exec() calls
      • Uses the client to connect and execute SELECT VERSION() on the server
      • Falls back to client binary version if server connection fails (for test environments)
      • Checks command exit codes and handles connection failures gracefully
      • Parses the server version string to determine if it's MySQL or MariaDB
      • Returns both type and version
    • Kept get_db_version() for backward compatibility
    • Updated logic to use detected server type instead of relying solely on WP_CLI_TEST_DBTYPE
    • Special handling for SQLite: respects WP_CLI_TEST_DBTYPE=sqlite explicitly
    • Fixed PHPCS: multi-item arrays with associative keys now have each value on a new line
  2. bin/install-package-tests:

    • Changed client binary detection to try both mysql and mariadb
    • Query server version using detected client binary
    • Determine database type from server version string (not client)
    • Use CLIENT_BINARY variable consistently throughout script
    • Simplified database readiness check to use single client binary
    • All database setup functions now use CLIENT_BINARY instead of hardcoded client names
    • Fixed typo: "Succesfully" → "Successfully"

Testing:

  • Verified PHP syntax with php -l
  • Verified shell script syntax with bash -n
  • Tested script execution with no database (graceful degradation with fallback)
  • Tested version string parsing for MySQL and MariaDB formats
  • Tested SQLite special case handling
  • Manually verified test scenario matches expected behavior
  • Addressed all code review feedback
  • Fixed PHPCS formatting issues
Original prompt

This section details on the original issue you should resolve

<issue_title>Broken MariaDB detection - looking at client instead of server</issue_title> <issue_description>The change in https://github.com/wp-cli/wp-cli-tests/pull/246 fixes the issue of allowing different client binary names.

However, I now realized that we're still using a faulty approach to even try to detect MySQL vs MariaDB to begin with. In the script, where we try to deduce the type, we're actually querying the version of the mysql client binary, not of the DB version. The type of the client is not really relevant here, all we are interested in is what dialect of SQL the server will need.

The script needs to split this into different phases:

  1. Detect what client binary can be used
  2. Use said client binary to connect to the server and retrieve the server version
  3. From the server version, extract the DB server type, i.e. MySQL or MariaDB</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes wp-cli/wp-cli-tests#251

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Nov 01 '25 19:11 Copilot