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

Limit search-replace in DB --wp_db_id="wp_posts:1234" (New command parameter) #142

Open
camya opened this issue Jun 17, 2020 · 5 comments
Open

Comments

@camya
Copy link

camya commented Jun 17, 2020

I often want to apply the wp search-replace command to only one single database record. (Sometimes for testing, sometimes, because I know, that I only have to replace data in one post.) Most often it's a single post ID in wp_posts.

Current situation:

The following command makes a dry run to replace "each and every" match of class="old". It's not possible to limit it to one id of a specific WordPress table.

wp search-replace 'class="old"' 'class="new"' --log --dry-run

Feature request

Add a new command parameter (--wp_db_id="") to set the database table name and the id of a record in order to limit the search-replace command.

--wp_db_id="wp_posts:123"

The correct id field for each WordPress entity is mapped automatically internally. See example below.

Mapping example

  • --wp_db_id="wp_posts:123" (translates internally into wp_posts.ID=123)
  • --wp_db_id="wp_options:789" (translates into wp_options.option_id=789)
  • --wp_db_id="wp_terms:1823" (translates into wp_terms.term_id=1823)

--wp_db_id="{WordPress table name}:{id}"

New command demo

The new command looks like this. It only replaces the record with wp_options.option_id=789.

wp search-replace 'class="old"' 'class="new"' --wp_db_id="wp_options:789" --log --dry-run

Exra feature: Comma separated list of ids

May it's also possible, to add more than one id.

--wp_db_id="wp_terms:1823,1233,2399"

@schlessera
Copy link
Member

I think the idea has merit, but I'm not too sure about the naming.

We can already limit the search&replace operation to a single (or a set of) table(s) by just appending the table name as a positional argument.

So all we'd need would be to add a --rows=<ids...> field instead.

Sample use case:

# Your example above with the new syntax:
wp search-replace 'class="old"' 'class="new"' wp_options --row=789 --log --dry-run

# Multiple tables and multiple rows possible:
wp search-replace "old" "new" wp_posts wp_post_meta --rows=1,2,3

What do you think about that syntax?

@camya
Copy link
Author

camya commented Jun 18, 2020

Hi Alain. Nice to meet you.

For sure, the syntax is strange. I think, the entiy and id have to be connected together in one command parameter in order to avoid strage side effects.

In your 2nd example, this means that the 6 records will be updated (if there is a match.)

Updates these records.

  • wp_posts.ID=1
  • wp_posts.ID=2
  • wp_posts.ID=3
  • wp_postmeta.meta_id=1
  • wp_postmeta.meta_id=2
  • wp_postmeta.meta_id=3

I guess, this could easily update the wrong records.

But I like the --rows idea nevertheless.

@camya
Copy link
Author

camya commented Jun 18, 2020

Idea: --rows supports only one table

The --rows=<ids...> parameter is only allowed in connection with ONE table.

If the user ads more than one table to the command, it throws an error message.

"--rows=<ids...> only works with one table. Too many tables set in command."

@fancsali
Copy link

fancsali commented Jul 4, 2021

I just want to say, I just spent hours with doing search and replace by hand, so I can only change the latest revision of one of my huge posts.

So, this feature would be highly appreciated...

drzraf added a commit to drzraf/search-replace-command that referenced this issue Jun 12, 2023
@danielbachhuber
Copy link
Member

Open to considering this if we can figure out the naming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants