Skip to content

Commit 4a9e2e5

Browse files
swissspidyclaude
andcommitted
Merge branch 'main' into exclude_revisions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
2 parents 75adfec + bb7d457 commit 4a9e2e5

11 files changed

Lines changed: 1798 additions & 20 deletions

‎README.md‎

Lines changed: 161 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ wp db create [--dbuser=<value>] [--dbpass=<value>] [--defaults]
8181

8282
Runs `CREATE_DATABASE` SQL statement using `DB_HOST`, `DB_NAME`,
8383
`DB_USER` and `DB_PASSWORD` database credentials specified in
84-
wp-config.php.
84+
wp-config.php. Requires MySQL/MariaDB client binaries to be available.
8585

8686
**OPTIONS**
8787

@@ -111,7 +111,7 @@ wp db drop [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]
111111

112112
Runs `DROP_DATABASE` SQL statement using `DB_HOST`, `DB_NAME`,
113113
`DB_USER` and `DB_PASSWORD` database credentials specified in
114-
wp-config.php.
114+
wp-config.php. Requires MySQL/MariaDB client binaries to be available.
115115

116116
**OPTIONS**
117117

@@ -144,7 +144,7 @@ wp db reset [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]
144144

145145
Runs `DROP_DATABASE` and `CREATE_DATABASE` SQL statements using
146146
`DB_HOST`, `DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
147-
specified in wp-config.php.
147+
specified in wp-config.php. Requires MySQL/MariaDB client binaries to be available.
148148

149149
**OPTIONS**
150150

@@ -177,7 +177,7 @@ wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--default
177177

178178
Runs `mysqlcheck` utility with `--check` using `DB_HOST`,
179179
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
180-
specified in wp-config.php.
180+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
181181

182182
[See docs](http://dev.mysql.com/doc/refman/5.7/en/check-table.html)
183183
for more details on the `CHECK TABLE` statement.
@@ -216,7 +216,7 @@ wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defa
216216

217217
Runs `mysqlcheck` utility with `--optimize=true` using `DB_HOST`,
218218
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
219-
specified in wp-config.php.
219+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
220220

221221
[See docs](http://dev.mysql.com/doc/refman/5.7/en/optimize-table.html)
222222
for more details on the `OPTIMIZE TABLE` statement.
@@ -269,7 +269,7 @@ wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaul
269269

270270
Runs `mysqlcheck` utility with `--repair=true` using `DB_HOST`,
271271
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
272-
specified in wp-config.php.
272+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
273273

274274
[See docs](http://dev.mysql.com/doc/refman/5.7/en/repair-table.html) for
275275
more details on the `REPAIR TABLE` statement.
@@ -297,14 +297,16 @@ more details on the `REPAIR TABLE` statement.
297297

298298
### wp db cli
299299

300-
Opens a MySQL console using credentials from wp-config.php
300+
Opens a MySQL console using credentials from wp-config.php.
301301

302302
~~~
303303
wp db cli [--database=<database>] [--default-character-set=<character-set>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
304304
~~~
305305

306306
**Alias:** `connect`
307307

308+
Requires `mysql` or `mariadb` client binary to be available.
309+
308310
**OPTIONS**
309311

310312
[--database=<database>]
@@ -342,7 +344,9 @@ wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [-
342344
~~~
343345

344346
Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
345-
and `DB_PASSWORD` database credentials specified in wp-config.php.
347+
and `DB_PASSWORD` database credentials specified in wp-config.php.
348+
If MySQL/MariaDB client binaries are not available, falls back to
349+
executing queries via WordPress's `wpdb`.
346350

347351
Use the `--skip-column-names` MySQL argument to exclude the headers
348352
from a SELECT query. Pipe the output to remove the ASCII table
@@ -438,13 +442,14 @@ To confirm the ID for the site you want to query, you can use the `wp site list`
438442
Exports the database to a file or to STDOUT.
439443

440444
~~~
441-
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
445+
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
442446
~~~
443447

444448
**Alias:** `dump`
445449

446450
Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
447451
`DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid [`mysqldump` flags](https://dev.mysql.com/doc/en/mysqldump.html#mysqldump-option-summary).
452+
Requires `mysqldump` or `mariadb-dump` client binary to be available.
448453

449454
**OPTIONS**
450455

@@ -467,6 +472,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
467472
[--exclude_tables=<tables>]
468473
The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
469474

475+
[--exclude_tables_data=<tables>]
476+
The comma separated list of specific tables for which only the structure will be exported. Excluding this parameter will export data for all tables in the export.
477+
Requires a dump client that supports --ignore-table-data (mariadb-dump from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13, 10.5.3 or later); MySQL's mysqldump does not. Cannot be combined with --tables.
478+
470479
[--include-tablespaces]
471480
Skips adding the default --no-tablespaces option to mysqldump.
472481

@@ -514,6 +523,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
514523
$ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
515524
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
516525

526+
# Skip data of certain tables from the exported database
527+
$ wp db export --exclude_tables_data=wp_actionscheduler_logs
528+
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
529+
517530
# Export database to STDOUT.
518531
$ wp db export -
519532
-- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
@@ -536,7 +549,10 @@ wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
536549
Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and
537550
`DB_PASSWORD` database credentials specified in wp-config.php. This
538551
does not create database by itself and only performs whatever tasks are
539-
defined in the SQL.
552+
defined in the SQL. If MySQL/MariaDB client binaries are not available,
553+
falls back to importing via WordPress's `wpdb`. The fallback reads the
554+
whole file into memory, so it is slower and needs more memory than the
555+
client binaries for large imports.
540556

541557
**OPTIONS**
542558

@@ -1039,6 +1055,141 @@ wp db columns <table> [--format]
10391055
| comment_count | bigint(20) | NO | | 0 | |
10401056
+-----------------------+---------------------+------+-----+---------------------+----------------+
10411057

1058+
1059+
1060+
### wp db status
1061+
1062+
Displays an overview of the database.
1063+
1064+
~~~
1065+
wp db status [--dbuser=<value>] [--dbpass=<value>] [--skip-check] [--format=<format>] [--defaults]
1066+
~~~
1067+
1068+
Shows the database name, the number of tables and their total size, the
1069+
table prefix, the storage engine, character set and collation of the
1070+
tables, and whether `mysqlcheck` reports any problems. Engine, charset
1071+
and collation read "Mixed" when the tables don't all share the same one.
1072+
1073+
Unlike `wp db size` or `wp db prefix`, this doesn't load WordPress, so it
1074+
also works before WordPress is installed.
1075+
1076+
For SQLite databases, the engine is reported as SQLite, and charset,
1077+
collation and check status are not available.
1078+
1079+
**OPTIONS**
1080+
1081+
[--dbuser=<value>]
1082+
Username to pass to mysql and mysqlcheck. Defaults to DB_USER.
1083+
1084+
[--dbpass=<value>]
1085+
Password to pass to mysql and mysqlcheck. Defaults to DB_PASSWORD.
1086+
1087+
[--skip-check]
1088+
Don't run `mysqlcheck`, which reads every table and can take a while on large databases.
1089+
1090+
[--format=<format>]
1091+
Render output in a particular format. The JSON output lists the total size in bytes.
1092+
---
1093+
default: list
1094+
options:
1095+
- list
1096+
- json
1097+
---
1098+
1099+
[--defaults]
1100+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
1101+
1102+
**EXAMPLES**
1103+
1104+
$ wp db status
1105+
Database Name: wordpress_dbase
1106+
Tables: 12
1107+
Total Size: 868.35 KB
1108+
Prefix: wp_
1109+
Engine: InnoDB
1110+
Charset: utf8mb4
1111+
Collation: utf8mb4_unicode_520_ci
1112+
Check Status: OK
1113+
1114+
$ wp db status --skip-check --format=json
1115+
{"database":"wordpress_dbase","tables":12,"size":868352,"prefix":"wp_","engine":"InnoDB","charset":"utf8mb4","collation":"utf8mb4_unicode_520_ci","check":"Skipped"}
1116+
1117+
1118+
1119+
### wp db users
1120+
1121+
Manages MySQL and MariaDB user accounts.
1122+
1123+
~~~
1124+
wp db users
1125+
~~~
1126+
1127+
Creating users needs a database account with the `CREATE USER` privilege
1128+
(and `GRANT OPTION` for `--grant-privileges`), which the one in wp-config.php
1129+
usually doesn't have. Pass a privileged account with `--dbuser` and `--dbpass`.
1130+
1131+
**EXAMPLES**
1132+
1133+
# Create a user with full privileges on the WordPress database, prompting for both passwords.
1134+
$ wp db users create appuser localhost --grant-privileges --dbuser=root --prompt=dbpass
1135+
1/1 [--dbpass=<value>]:
1136+
Password:
1137+
Success: Created database user 'appuser'@'localhost' with all privileges on 'wp_database'.
1138+
1139+
1140+
1141+
1142+
1143+
### wp db users create
1144+
1145+
Creates a database user.
1146+
1147+
~~~
1148+
wp db users create <username> [<host>] [--password=<password>] [--grant-privileges] [--dbuser=<value>] [--dbpass=<value>] [--defaults]
1149+
~~~
1150+
1151+
The statements are sent to the `mysql` client through STDIN, so the new
1152+
user's password doesn't show up in the process list or in `--debug` output.
1153+
1154+
**OPTIONS**
1155+
1156+
<username>
1157+
Name of the user to create.
1158+
1159+
[<host>]
1160+
Host the user is allowed to connect from. Use `%` for any host.
1161+
---
1162+
default: localhost
1163+
---
1164+
1165+
[--password=<password>]
1166+
Password for the new user. If omitted, it is read from STDIN when input is piped, and prompted for otherwise. Avoid passing it here on shared systems, as it ends up in the shell history.
1167+
1168+
[--grant-privileges]
1169+
Grant the new user all privileges on the WordPress database (DB_NAME).
1170+
1171+
[--dbuser=<value>]
1172+
Username to connect as. Needs the `CREATE USER` privilege. Defaults to DB_USER.
1173+
1174+
[--dbpass=<value>]
1175+
Password to connect with. Defaults to DB_PASSWORD.
1176+
1177+
[--defaults]
1178+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
1179+
1180+
**EXAMPLES**
1181+
1182+
# Create a user that can connect from any host, reading its password from a file.
1183+
# This connects as DB_USER, which then needs the CREATE USER privilege.
1184+
$ wp db users create appuser '%' < password.txt
1185+
Success: Created database user 'appuser'@'%'.
1186+
1187+
# Create a user with full privileges on the WordPress database.
1188+
$ wp db users create appuser localhost --grant-privileges --dbuser=root --prompt=dbpass
1189+
1/1 [--dbpass=<value>]:
1190+
Password:
1191+
Success: Created database user 'appuser'@'localhost' with all privileges on 'wp_database'.
1192+
10421193
## Installing
10431194

10441195
This package is included with WP-CLI itself, no additional installation necessary.

‎composer.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
"db search",
5151
"db tables",
5252
"db size",
53-
"db columns"
53+
"db columns",
54+
"db status",
55+
"db users",
56+
"db users create"
5457
]
5558
},
5659
"autoload": {

‎db-command.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
}
1111

1212
WP_CLI::add_command( 'db', 'DB_Command' );
13+
WP_CLI::add_command( 'db users', 'WP_CLI\DB\Users_Command' );

‎features/db-export.feature‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,87 @@ Feature: Export a WordPress database
7070
PRAGMA foreign_keys=OFF
7171
"""
7272

73+
# Support depends on the dump client, not the server (e.g. mariadb-dump works against a MySQL server),
74+
# so fake a dump client without --ignore-table-data, like MySQL's mysqldump.
75+
@skip-sqlite
76+
Scenario: Exclude data of certain tables is not supported by the dump client
77+
Given a WP install
78+
And a fake-bin/mysqldump file:
79+
"""
80+
#!/bin/sh
81+
case "$1" in
82+
--version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;;
83+
--help) echo " --ignore-table=name Do not dump the specified table." ;;
84+
esac
85+
exit 0
86+
"""
87+
And a fake-bin/mariadb-dump file:
88+
"""
89+
#!/bin/sh
90+
case "$1" in
91+
--version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;;
92+
--help) echo " --ignore-table=name Do not dump the specified table." ;;
93+
esac
94+
exit 0
95+
"""
96+
97+
When I run `chmod +x fake-bin/mysqldump fake-bin/mariadb-dump`
98+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db export wp_cli_test.sql --exclude_tables_data=wp_users`
99+
Then the return code should be 1
100+
And STDERR should contain:
101+
"""
102+
Error: The --exclude_tables_data option requires a dump client that supports --ignore-table-data
103+
"""
104+
And the wp_cli_test.sql file should not exist
105+
106+
# Only the MariaDB dump client supports --ignore-table-data.
107+
@require-mariadb
108+
Scenario: Exclude data of certain tables when exporting the database
109+
Given a WP install
110+
111+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users,wp_options --porcelain`
112+
Then the wp_cli_test.sql file should exist
113+
And the wp_cli_test.sql file should contain:
114+
"""
115+
CREATE TABLE `wp_users`
116+
"""
117+
And the wp_cli_test.sql file should contain:
118+
"""
119+
CREATE TABLE `wp_options`
120+
"""
121+
And the wp_cli_test.sql file should contain:
122+
"""
123+
INSERT INTO `wp_posts`
124+
"""
125+
And the wp_cli_test.sql file should not contain:
126+
"""
127+
INSERT INTO `wp_users`
128+
"""
129+
And the wp_cli_test.sql file should not contain:
130+
"""
131+
INSERT INTO `wp_options`
132+
"""
133+
134+
@skip-sqlite
135+
Scenario: Exclude data of certain tables cannot be combined with --tables
136+
Given a WP install
137+
138+
When I try `wp db export wp_cli_test.sql --tables=wp_users,wp_posts --exclude_tables_data=wp_users`
139+
Then the return code should be 1
140+
And STDERR should contain:
141+
"""
142+
Error: The --exclude_tables_data option cannot be combined with --tables.
143+
"""
144+
145+
@require-sqlite
146+
Scenario: Exclude data of certain tables when exporting a SQLite database
147+
Given a WP install
148+
149+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain`
150+
Then the contents of the wp_cli_test.sql file should match /CREATE TABLE [`"]?wp_users[`"]?/
151+
And the contents of the wp_cli_test.sql file should match /INSERT INTO [`"]?wp_posts[`"]?/
152+
And the contents of the wp_cli_test.sql file should not match /INSERT INTO [`"]?wp_users[`"]?/
153+
73154
@skip-sqlite
74155
Scenario: Export database to STDOUT
75156
Given a WP install

0 commit comments

Comments
 (0)