Skip to content

Commit 2d3706f

Browse files
Copilotswissspidy
andauthored
Add Behat tests for wpdb fallback in drop, reset, and import commands
Agent-Logs-Url: https://github.com/wp-cli/db-command/sessions/fa41acc5-de2a-4dc3-9f7a-f5b282ed670a Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 47906b9 commit 2d3706f

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

features/db-import.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ Feature: Import a WordPress database
6969
Success: Imported from 'wp_cli_test.sql'.
7070
"""
7171

72+
@require-mysql-or-mariadb
73+
Scenario: Database import falls back to wpdb when mysql binary is unavailable
74+
Given a WP install
75+
And a fake-bin/mysql file:
76+
"""
77+
#!/bin/sh
78+
exit 127
79+
"""
80+
And a fake-bin/mariadb file:
81+
"""
82+
#!/bin/sh
83+
exit 127
84+
"""
85+
86+
When I run `wp db export wp_cli_test.sql`
87+
Then the wp_cli_test.sql file should exist
88+
89+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
90+
And I run `env PATH={RUN_DIR}/fake-bin:$PATH wp db import wp_cli_test.sql --debug`
91+
Then STDOUT should be:
92+
"""
93+
Success: Imported from 'wp_cli_test.sql'.
94+
"""
95+
And STDERR should contain:
96+
"""
97+
MySQL/MariaDB binary not available, falling back to wpdb for import.
98+
"""
99+
72100
# SQLite doesn't support the --dbuser flag.
73101
@require-mysql-or-mariadb
74102
Scenario: Import from database name path by default with passed-in dbuser/dbpass

features/db.feature

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,56 @@ Feature: Perform database operations
383383
Query succeeded. Rows affected: 1
384384
"""
385385
386+
@require-mysql-or-mariadb
387+
Scenario: Database drop falls back to wpdb when mysql binary is unavailable
388+
Given a WP install
389+
And a fake-bin/mysql file:
390+
"""
391+
#!/bin/sh
392+
exit 127
393+
"""
394+
And a fake-bin/mariadb file:
395+
"""
396+
#!/bin/sh
397+
exit 127
398+
"""
399+
400+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
401+
And I run `env PATH={RUN_DIR}/fake-bin:$PATH wp db drop --yes --debug`
402+
Then STDOUT should contain:
403+
"""
404+
Success: Database dropped.
405+
"""
406+
And STDERR should contain:
407+
"""
408+
Query via wpdb:
409+
"""
410+
411+
@require-mysql-or-mariadb
412+
Scenario: Database reset falls back to wpdb when mysql binary is unavailable
413+
Given a WP install
414+
And a fake-bin/mysql file:
415+
"""
416+
#!/bin/sh
417+
exit 127
418+
"""
419+
And a fake-bin/mariadb file:
420+
"""
421+
#!/bin/sh
422+
exit 127
423+
"""
424+
425+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
426+
And I run `env PATH={RUN_DIR}/fake-bin:$PATH wp db reset --yes --debug`
427+
Then STDOUT should contain:
428+
"""
429+
Success: Database reset.
430+
"""
431+
And STDERR should contain:
432+
"""
433+
Query via wpdb:
434+
"""
435+
386436
@require-sqlite
387437
Scenario: SQLite DB CRUD operations
388438
Given a WP install

src/DB_Command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,11 @@ protected function run_query( $query, $assoc_args = [] ) {
19471947
WP_CLI::debug( "Query via wpdb: {$query}", 'db' );
19481948
$this->maybe_load_wpdb();
19491949
global $wpdb;
1950+
1951+
if ( ! isset( $wpdb ) ) {
1952+
WP_CLI::error( 'WordPress database (wpdb) is not available. Please install MySQL or MariaDB client tools.' );
1953+
}
1954+
19501955
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
19511956
$result = $wpdb->query( $query );
19521957
if ( false === $result ) {

0 commit comments

Comments
 (0)