Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2e544c4
Initial plan
Copilot Jan 19, 2026
347182e
Add wp db users create command implementation
Copilot Jan 19, 2026
891f713
Refactor DB_Users_Command to extend DB_Command and improve security
Copilot Jan 19, 2026
7f2d7d9
Simplify code by calling parent::run_query directly and remove test-o…
Copilot Jan 19, 2026
84b425f
Make esc_sql_ident protected in parent class and call it statically
Copilot Jan 19, 2026
421a9a4
Remove assert calls and improve SQL string escaping for security
Copilot Jan 19, 2026
db8bebf
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy Feb 15, 2026
c57a6c6
Remove redundant @var annotations and use explicit type casting
Copilot Mar 11, 2026
06044eb
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy Mar 11, 2026
f291b1e
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy Mar 22, 2026
6bfaf43
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy Apr 6, 2026
719cb77
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy Apr 17, 2026
36fb7e1
Namespaces, tests
swissspidy Apr 17, 2026
fcb2d05
Lint fix
swissspidy Apr 17, 2026
e70699d
Update src/DB_Users_Command.php
swissspidy Apr 19, 2026
381a520
Update src/DB_Users_Command.php
swissspidy Apr 19, 2026
f273be9
Update src/DB_Users_Command.php
swissspidy Apr 19, 2026
f23449f
Merge branch 'main' into copilot/add-wp-db-users-command
swissspidy May 23, 2026
7eb293c
Merge remote-tracking branch 'origin/main' into copilot/add-wp-db-use…
swissspidy Nov 24, 2026
cb1156a
Make `wp db users` a standalone command
swissspidy Nov 24, 2026
439f64e
Do not use DROP USER IF EXISTS in the db users tests
swissspidy Nov 24, 2026
7b85054
Merge remote-tracking branch 'origin/main' into copilot/add-wp-db-use…
swissspidy Nov 24, 2026
0a895d3
Keep the new password out of errors and intact in `wp db users create`
swissspidy Nov 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"db tables",
"db size",
"db columns",
"db status"
"db status",
"db users",
"db users create"
]
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions db-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
}

WP_CLI::add_command( 'db', 'DB_Command' );
WP_CLI::add_command( 'db users', 'WP_CLI\DB\Users_Command' );
141 changes: 141 additions & 0 deletions features/db-users.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
Feature: Manage database users

# The cleanup uses `I try` with a plain DROP USER, as MySQL 5.6 has no DROP USER IF EXISTS.
@require-mysql-or-mariadb
Scenario: Create a database user without privileges
Given a WP install
And I try `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

When I run `wp db users create wpcli_dbusers_1 '%' --password=first-Pa55 --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
Then STDOUT should be:
"""
Success: Created database user 'wpcli_dbusers_1'@'%'.
"""

When I run `wp db query "SELECT User FROM mysql.user WHERE User = 'wpcli_dbusers_1'" --skip-column-names --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
Then STDOUT should be:
"""
wpcli_dbusers_1
"""

When I try `wp db query "SELECT 1" --dbuser=wpcli_dbusers_1 --dbpass=first-Pa55`
Then the return code should be 1
And STDERR should contain:
"""
denied
"""

When I try `wp db users create wpcli_dbusers_1 '%' --password=dupe-Pa55-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
Then the return code should be 1
And STDERR should contain:
"""
Error: ERROR 1396
"""
And STDERR should not contain:
"""
dupe-Pa55-Value
"""

When I run `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

@require-mysql-or-mariadb
Scenario: Create a database user with privileges on the WordPress database
Given a WP install
And I try `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

When I run `echo "it's a \"tricky\" pass\\word;" | wp db users create wpcli_dbusers_2 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
Then STDOUT should be:
"""
Success: Created database user 'wpcli_dbusers_2'@'%' with all privileges on '{DB_NAME}'.
"""

When I run `wp db query "SELECT option_value FROM wp_options WHERE option_name = 'siteurl'" --skip-column-names --dbuser=wpcli_dbusers_2 --dbpass="it's a \"tricky\" pass\\word;"`
Then STDOUT should be:
"""
https://example.com
"""

When I run `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

@require-mysql-or-mariadb
Scenario: A password with an internal CRLF is kept as is
Given a WP install
And I try `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

When I run `printf 'crlf\r\nPa55\n' | wp db users create wpcli_dbusers_5 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
Then STDOUT should contain:
"""
Success:
"""

When I run `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\r\nPa55')"`
Then STDOUT should be:
"""
connected
"""

When I try `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\nPa55')"`
Then the return code should be 1
And STDERR should contain:
"""
ERROR 1045
"""

When I run `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

@require-mysql-or-mariadb
Scenario: The new user's password is not leaked into the debug output
Given a WP install
And I try `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

When I try `wp db users create wpcli_dbusers_3 '%' --password=leak-Ch3ck-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} --debug=db`
Then the return code should be 0
And STDERR should contain:
"""
Creating database user 'wpcli_dbusers_3'@'%'.
"""
And STDERR should not contain:
"""
leak-Ch3ck-Value
"""

When I run `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`

@require-mysql-or-mariadb
Scenario: A password is required
Given a WP install

When I try `wp db users create wpcli_dbusers_4 '%' --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} < /dev/null`
Then the return code should be 1
And STDERR should contain:
"""
Error: A password is required.
"""

@require-mysql-or-mariadb
Scenario: Only the create subcommand is available
Given a WP install

When I try `wp db users drop --yes`
Then the return code should be 1
And STDERR should contain:
"""
is not a registered subcommand of 'db users'
"""

When I run `wp db check`
Then STDOUT should contain:
"""
Success: Database checked.
"""

@require-sqlite
Scenario: Database users are not supported with SQLite
Given a WP install

When I try `wp db users create someone --password=some-Pa55`
Then the return code should be 1
And STDERR should be:
"""
Error: Database users are not supported with SQLite.
"""
Loading