terminus icon indicating copy to clipboard operation
terminus copied to clipboard

Exit 255 on wp search-replace with regex

Open zohar opened this issue 5 years ago • 6 comments

Expected behavior

I'm trying to sanitize users' emails on Test/Dev environments.

I ran the command: terminus wp -vvv myproject.test -- search-replace "^([^@]*)@(.*)$" '\[email protected]' wp_users --include-columns=user_email --regex --regex-flags='i' --url=test.example.com --network --dry-run --debug

Running the wp-cli command on a local environment works just fine: all emails are sanitized to avoid embarrassing mistakes.

Actual behavior

The command fails with error message:

[notice] Command: myproject.test -- wp search-replace ^([^@]*)@(.*)$ \[email protected] wp_users [Exit: 255]
 [error]   

Steps to reproduce the behavior

Run the above command

 ------------------------- --------------------------------------------------------------------------------------------------- 
  PHP binary                /usr/bin/php                                                                                       
  PHP version               7.1.23                                                                                             
  php.ini used                                                                                                                 
  Terminus project config                                                                                                      
  Terminus root dir         /Applications/Terminus/vendor/pantheon-systems/terminus                                            
  Terminus version          2.2.0                                                                                              
  Operating system          Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64  
 ------------------------- --------------------------------------------------------------------------------------------------- ```


zohar avatar Nov 19 '19 11:11 zohar

The following command also returns 255 and fails. I don't think it would be time-out since the error came back very quickly: terminus wp my.site.on.pantheon.dev -- db query "UPDATE wp_users SET user_email = CONCAT(user_email, '.localhost') where ID NOT IN (select u.ID from (select * from wp_users ) u INNER JOIN wp_usermeta m ON m.user_id = u.ID WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%');" --url=my.site.on.pantheon

zohar avatar Nov 20 '19 21:11 zohar

Hi Zohar. As I just mentioned in your support ticket, I suspect these issues might be related to the escaping of some of these special characters. I believe that storing your query in a file and then adding that like this might be a valid workaround for the time being: terminus wp my.site.on.pantheon.dev -- db query < query.sql

SqyD avatar Nov 21 '19 16:11 SqyD

@SqyD Thanks Paul. So the terminus command which uses wp db query does run when the query is being pulled from a file. As for the search-replace command - I can probably use an alternative sql query, but the fact is that there is a problem with how terminus interprets the command and IMO it could probably be fixed.

zohar avatar Nov 21 '19 20:11 zohar

In the support ticket we discovered that removing the ";" character from the sql query allows it to execute the sql query from the commandline directly. What remains is the regex issue. I've tried a number of variation on it but haven't been able to pinpoint what's making that fail.

SqyD avatar Nov 22 '19 09:11 SqyD

Hi, I'm also experiencing this issue with regex commands failing with exit code 255.

Workaround: Double Quotes

Unsure why, but when I switched the quotes around my expressions from ' single quotes to " double quotes the command ran successfully. (In the example below, I'm replacing URLs with a trailing .php to end with /.)

- terminus wp -vvv myproject.test -- search-replace '(\/path\/?.*)\.php' '$1/' wp_posts wp_postmeta --regex --dry-run
+ terminus wp -vvv myproject.test -- search-replace "(\/path\/?.*)\.php" "$1/" wp_posts wp_postmeta --regex --dry-run

Not sure if this solves the issue for the original example, but hopefully this helps others debug / workaround the problem.

P.S. SSH Timeouts

In case others run into timeout issues, my search-replace timed out once I got it working (our database is pretty large). According to the docs, SSH closes after 10 minutes of idle time.

To try and workaround that, I've broken up my command to run 1 column at a time (e.g., --include-columns="post_content").

terminus wp -vvv myproject.test -- search-replace "(\/path\/?.*)\.php" "$1/" wp_posts --regex --include-columns="post_content"

I'm sure there's a better way to do this, but this at least got things working for me for now.

Again, hopefully this helps.

tannerhodges avatar Jun 04 '20 19:06 tannerhodges

I'm having an issue with search-replace as well. I'm trying to replace all http urls on 22 domains with the same url on https. Here's the query I'm running: terminus wp site-id.dev -- search-replace 'http://www.domain.com/' 'https://www.domain.com/' wp_posts --include-columns=post_content --verbose --skip-themes --skip-plugins --regex-flags='i'

I've added a test entry into the db in the wp_posts table in the post_content column with the domain spelled 'http://WWW.domain.com' but unfortunately it isn't respecting the --regex-flags='i'. The result is that the matching domain will not be replaced with its' https counterpart. If I change the WWW to www it will replace it just fine.

I've tried --regex-flags="i" as well which also does not do the replacement. Using ` also doesn't work because it becomes an invalid command

mcpat1993 avatar Apr 07 '23 12:04 mcpat1993