Sometimes when I interrupt PHP unit tests in a wp-env environment I end up getting the database in a bad state so that the next time I try to run tests I get:
wp_die() called
Message: One or more database tables are unavailable. The database may need to be <a href="maint/repair.php?referrer=is_blog_installed">repaired</a>.
Title: WordPress › Error
Args:
response: 500
code: wp_die
exit: 1
back_link:
link_url:
link_text:
text_direction: ltr
charset: UTF-8
additional_errors: array (
)
✖ Command failed with exit code 1
Code language: plaintext (plaintext)
I then have to re-remember what I do to fix this every time, so I’m blogging it so I won’t have to try to remember again.
First I try to repair the DB with WP-CLI by doing the following (very concise) command:
npm run wp-env run tests-cli wp db repair
Code language: Shell Session (shell)
I then get the output like the following:
> wp-env
> wp-env run tests-cli wp db repair
ℹ Starting 'wp db repair' on the tests-cli container.
tests-wordpress.wp_users
note : The storage engine for the table doesn't support repair
Success: Database repaired.
✔ Ran `wp db repair` in 'tests-cli'. (in 0s 754ms)
Code language: plaintext (plaintext)
But tests then still fail. But what did work for me is to reset the tests DB as follows:
npm run wp-env run tests-cli wp db reset
Code language: Shell Session (shell)
Then tests start working again.
I guess there’s a way to achieve this by adding the WP_ALLOW_REPAIR
constant in the test environment’s wp-config.php
but for some reason the test environment doesn’t seem to be using the config located at:
$(npm run --silent wp-env install-path 2>/dev/null)/tests-WordPress/wp-config.php
Code language: Shell Session (shell)
I hope this is helpful to you (and me in the future)!