Skip to content

Commit 5702dfc

Browse files
swissspidyclaude
andcommitted
Hold the --where rows in a temporary table instead of in PHP
The primary keys of the rows that --where selects were kept in PHP as chunks of `pk IN (...)` predicates: memory grew with the number of matching rows, a numeric-looking text key like '01' was written as the number 1 and also matched '1', a failed page query ended the selection silently with a partial key list, and prefiltering by the search string could miss a match that a trigger adds to a later column. Copy the keys of the matching rows database-side, with their exact types, into an indexed temporary table per set of conditions, and restrict each column's replacement and dry-run count to it. Stop with an error before any column of the table is replaced if the table cannot be created, with a hint about the CREATE TEMPORARY TABLES privilege, and error out on SQLite, whose drop-in does not support CREATE TABLE ... SELECT. Also stop when a page query of the PHP handler or a dry-run count fails, instead of reporting a partial count. Never replace the primary key columns of a table with --where, so a row stays addressable by its key, and compare text primary keys as strings when the PHP handler fetches a row or pages by key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 8b1b0bd commit 5702dfc

2 files changed

Lines changed: 285 additions & 120 deletions

File tree

‎features/search-replace-where.feature‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,166 @@ Feature: Test search-replace --where option
513513
| posts:post_title:post_content LIKE "%foo%";posts:post_content:post_title LIKE "%foo%" | |
514514
| posts:post_title:post_content LIKE "%foo%";posts:post_content:post_title LIKE "%foo%" | --precise |
515515

516+
@require-mysql
517+
Scenario Outline: Where condition on a text primary key replaces only the rows it selects
518+
Given a WP install
519+
And I run `wp db query "CREATE TABLE wp_slugs ( slug VARCHAR(20) NOT NULL, title TEXT, body TEXT, PRIMARY KEY (slug) )"`
520+
And I run `wp db query "INSERT INTO wp_slugs VALUES ('01', 'zero one foo', 'zero one foo'), ('1', 'one foo', 'one foo')"`
521+
522+
When I run `wp search-replace foo bar --all-tables --where="wp_slugs::slug = '1'" --dry-run --format=count <flags>`
523+
Then STDOUT should be:
524+
"""
525+
2
526+
"""
527+
528+
When I run `wp search-replace foo bar --all-tables --where="wp_slugs::slug = '1'" --format=count <flags>`
529+
Then STDOUT should be:
530+
"""
531+
2
532+
"""
533+
534+
When I run `wp db query "SELECT CONCAT_WS('|', slug, title, body) FROM wp_slugs ORDER BY slug" --skip-column-names`
535+
Then STDOUT should be:
536+
"""
537+
01|zero one foo|zero one foo
538+
1|one bar|one bar
539+
"""
540+
541+
Examples:
542+
| flags |
543+
| |
544+
| --precise |
545+
546+
@require-mysql
547+
Scenario Outline: Where never replaces a primary key column
548+
Given a WP install
549+
And I run `wp db query "CREATE TABLE wp_keyed ( slug VARCHAR(20) NOT NULL, title TEXT, body TEXT, PRIMARY KEY (slug) )"`
550+
And I run `wp db query "INSERT INTO wp_keyed VALUES ('foo', 'foo', 'foo'), ('other', 'foo', 'foo')"`
551+
552+
When I run `wp search-replace foo bar --all-tables --where="wp_keyed::slug = 'foo'" --dry-run --format=count <flags>`
553+
Then STDOUT should be:
554+
"""
555+
2
556+
"""
557+
558+
When I run `wp search-replace foo bar --all-tables --where="wp_keyed::slug = 'foo'" --format=count <flags>`
559+
Then STDOUT should be:
560+
"""
561+
2
562+
"""
563+
564+
When I run `wp db query "SELECT CONCAT_WS('|', slug, title, body) FROM wp_keyed ORDER BY slug" --skip-column-names`
565+
Then STDOUT should be:
566+
"""
567+
foo|bar|bar
568+
other|foo|foo
569+
"""
570+
571+
Examples:
572+
| flags |
573+
| |
574+
| --precise |
575+
576+
@require-mysql
577+
Scenario Outline: A failed row selection stops before any column is replaced
578+
Given a WP install
579+
And I run `wp post create --post_title='Title foo' --post_content='Content foo' --porcelain`
580+
And save STDOUT as {POST_ID}
581+
582+
When I try `wp search-replace foo bar --where='posts:post_content,post_title:no_such_column = 1' <flags>`
583+
Then STDERR should contain:
584+
"""
585+
Error: Could not select the rows of table 'wp_posts' that --where matches:
586+
"""
587+
And STDERR should contain:
588+
"""
589+
no_such_column
590+
"""
591+
And the return code should be 1
592+
593+
When I run `wp post get {POST_ID} --field=title`
594+
Then STDOUT should be:
595+
"""
596+
Title foo
597+
"""
598+
599+
When I run `wp post get {POST_ID} --field=content`
600+
Then STDOUT should be:
601+
"""
602+
Content foo
603+
"""
604+
605+
Examples:
606+
| flags |
607+
| |
608+
| --dry-run |
609+
| --precise |
610+
611+
@require-mysql
612+
Scenario: A missing privilege for the temporary table gives a clear error
613+
Given a WP install
614+
And a wp-content/mu-plugins/deny-temporary-tables.php file:
615+
"""
616+
<?php
617+
// A temporary table in information_schema is always denied, like a missing CREATE TEMPORARY TABLES privilege.
618+
add_filter(
619+
'query',
620+
function ( $query ) {
621+
return preg_replace( '/^CREATE TEMPORARY TABLE /', 'CREATE TEMPORARY TABLE information_schema.', $query );
622+
}
623+
);
624+
"""
625+
And I run `wp post create --post_title='Title foo' --post_content='Content foo' --porcelain`
626+
And save STDOUT as {POST_ID}
627+
628+
When I try `wp search-replace foo bar --where='posts::post_status = "publish" OR post_status = "draft"'`
629+
Then STDERR should contain:
630+
"""
631+
Error: Could not create a temporary table to hold the rows of table 'wp_posts' that --where matches:
632+
"""
633+
And STDERR should contain:
634+
"""
635+
needs the CREATE TEMPORARY TABLES privilege; without it, replace one column per run
636+
"""
637+
And the return code should be 1
638+
639+
When I run `wp post get {POST_ID} --field=title`
640+
Then STDOUT should be:
641+
"""
642+
Title foo
643+
"""
644+
645+
@require-sqlite
646+
Scenario: Where conditions for several columns of a table are rejected on SQLite
647+
Given a WP install
648+
And I run `wp post create --post_title='Title foo' --post_content='Content foo' --porcelain`
649+
And save STDOUT as {POST_ID}
650+
651+
When I try `wp search-replace foo bar --where='posts:post_content,post_title:post_content LIKE "%foo%"'`
652+
Then STDERR should contain:
653+
"""
654+
Error: Replacing several columns of table 'wp_posts' with --where is not supported on SQLite.
655+
"""
656+
And the return code should be 1
657+
658+
When I run `wp post get {POST_ID} --field=title`
659+
Then STDOUT should be:
660+
"""
661+
Title foo
662+
"""
663+
664+
When I run `wp search-replace foo bar --where='posts:post_title:post_content LIKE "%foo%"'`
665+
Then STDOUT should contain:
666+
"""
667+
Success: Made 1 replacement.
668+
"""
669+
670+
When I run `wp post get {POST_ID} --field=title`
671+
Then STDOUT should be:
672+
"""
673+
Title bar
674+
"""
675+
516676
Scenario: Where clause cannot be combined with an export
517677
Given a WP install
518678

0 commit comments

Comments
 (0)