core-command icon indicating copy to clipboard operation
core-command copied to clipboard

`wp core is-installed` returns `0` even if there are no tables

Open dingo-d opened this issue 3 years ago • 5 comments

Bug Report

Describe the current, buggy behavior

I am working on automatic WP deploy using deployer and GH Actions. The first task in my deploy process is to setup the wp-config.php, then build the theme, upload it, symlink the current release, and at the end I'm making a check for plugin and core updates using WP-CLI.

In the last part of the deployer task that checks the core and plugins, I added

$isInstalled = run('~/bin/wp core is-installed | echo $?');

This will set the variable $isInstalled to 0 if the core is installed, and 1 if not. In the case it's 1 I do

	if ($isInstalled === '1') {
		$host = get('hostname');
		run("~/bin/wp core install --url={$host} --title={$appName} --admin_user={$adminUser} --admin_email={$adminEmail}");
	}

Then after that, I have

	$currentCoreVersion = run('~/bin/wp core version');

	writeln('<comment>Checking WordPress core version</comment>');

	if ($coreVersion !== $currentCoreVersion) {
		run("~/bin/wp core update --version='{$coreVersion}' --force");
		writeln('<info>WordPress core updated</info>');
	}

This will check the current core version, and compare it to the version I have set during my deployment process ($coreVersion variable). In my case, the $coreVersion was set to 5.7, and the $currentCoreVersion was 5.6, so the conditional was true and I tried to update my WP core. But I got the error that WP is not installed, because the tables weren't set yet in the DB.

So in the end the command wp core is-installed doesn't do a good job really. Not sure why tho, because the is_blog_installed() function should return false (or maybe it doesn't because of all the error suppressions 🤷🏼‍♂️ ).

Describe how other contributors can replicate this bug

  • Locally download core (wp core download --version=5.6)
  • Set up wp-config.php that will point to empty DB (wp config create)
  • Try to run wp core update and see the error

More so, even after just downloading the core the command

wp core is-installed | echo $?

Will return

0
Error: 'wp-config.php' not found.
Either create one manually or use `wp config create`.

So it says that it's installed but the config is missing, after creating the config it returns 0, but there are still no DB tables.

Describe what you would expect as the correct outcome

It would be good to throw 1 as long as there are no DB tables present. The command should be more robust. That way it will be easier to use them in CI/CD pipelines.

Let us know what environment you are running this on

Local:

OS:	Darwin 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64
Shell:	/bin/zsh
PHP binary:	/usr/local/Cellar/[email protected]/7.4.16/bin/php
PHP version:	7.4.16
php.ini used:	/usr/local/etc/php/7.4/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/Users/denis.zoljom/Desktop/example
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version:	2.4.0

Ubuntu:

OS:	Linux 5.4.0-1037-aws #39-Ubuntu SMP Thu Jan 14 02:56:06 UTC 2021 x86_64
Shell:	/bin/bash
PHP binary:	/usr/bin/php7.4
PHP version:	7.4.13
php.ini used:	/etc/php/7.4/cli/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/home/denisjezakon
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version:	2.4.0

dingo-d avatar Apr 09 '21 09:04 dingo-d

I can look into it :slightly_smiling_face:

Luc45 avatar Jun 02 '22 09:06 Luc45

I think the issue might be on the chaining the echo $? using a pipe.

Instead of: wp core is-installed | echo $?

Try:

  • wp core is-installed
  • echo $?

You can also test this with:

  • ls /nonexisting_dir &> /dev/null | echo $?
  • And then using separate commands: ls /nonexisting_dir &> /dev/null
  • echo $?

Luc45 avatar Jun 02 '22 13:06 Luc45

The issue is not in the output of the WP-CLI command, but more about the behavior of the command:

It would be good to throw 1 as long as there are no DB tables present. The command should be more robust. That way it will be easier to use them in CI/CD pipelines.

I 'solved' the issue by:

  1. Checking the database tables from the information_schema table
  2. Checking the wp core is-installed | echo $? command output

What I'd think that command would have to do is combine the table check as well. As that is the part that is kinda failing.

dingo-d avatar Jun 04 '22 12:06 dingo-d

@dingo-d I've replicated the behaviour. It seems that you are using the Symfony command library, you can get the return code of the command from the $outpout variable you pass to run() https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Console/Command/Command.php#LL278C60-L278C60

And then on this you can get the return code, as in exec the 3rd argument return the code :

<?php

exec('wp core is-installed', $out, $code);

var_dump($out,$code);

Will print :

array(0) {
}
int(1)

As intended, the output is empty, the return code is 0.

Rahe avatar Jun 08 '23 09:06 Rahe

@Rahe I'm using deployer, not 100% sure what they are using under the hood, I guess it's Symfony command lib, but I'm using the abstractions provided by deployer, so I might not have the ability to access the command lib directly.

dingo-d avatar Jun 12 '23 08:06 dingo-d