Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
dingo-d opened this issue Apr 9, 2021 · 5 comments
Open
2 tasks done

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

dingo-d opened this issue Apr 9, 2021 · 5 comments

Comments

@dingo-d
Copy link

dingo-d commented Apr 9, 2021

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
@schlessera schlessera changed the title wp core is-installed returns 0 even if there are no tables wp core is-installed returns 0 even if there are no tables Jun 2, 2022
@Luc45
Copy link

Luc45 commented Jun 2, 2022

I can look into it 🙂

@Luc45
Copy link

Luc45 commented Jun 2, 2022

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 $?

@dingo-d
Copy link
Author

dingo-d commented Jun 4, 2022

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.

@Rahe
Copy link

Rahe commented Jun 8, 2023

@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.

@dingo-d
Copy link
Author

dingo-d commented Jun 12, 2023

@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.

@Luc45 Luc45 removed their assignment Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants