Skip to content

Commit 4c8da6c

Browse files
swissspidyclaude
andcommitted
Leave the whole value unchanged when a --recurse-keys rename fails
With --recurse-keys, renaming keys or property names is now all or nothing per value: if reading, staging, removing or writing a property or offset throws (an Error or any Exception, at any nesting level), run() returns the original value unchanged, so the cell is neither updated nor counted, and the log entries collected for it are discarded. This replaces the best-effort rollback in restore_object(), which could stop part way (for example when an ArrayAccess implementation throws again during rollback, or rejects an immutable offset) and could not undo renames already made in nested objects, letting a partly modified value be saved. Without --recurse-keys nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 2e533fb commit 4c8da6c

2 files changed

Lines changed: 307 additions & 92 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 221 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,7 @@ Feature: Do global search/replace
21692169
"""
21702170

21712171
@require-mysql
2172-
Scenario: Restore an object when removing a property throws an exception with --recurse-keys
2172+
Scenario: Leave the stored value unchanged when removing a property throws an exception with --recurse-keys
21732173
Given a WP install
21742174
And a wp-content/mu-plugins/recurse-keys-throwing-unset.php file:
21752175
"""
@@ -2220,3 +2220,223 @@ Feature: Do global search/replace
22202220
"""
22212221
s:8:"oldtok_a";s:7:"value-a";s:4:"keep";s:10:"value-keep";s:8:"oldtok_b";s:7:"value-b";
22222222
"""
2223+
2224+
@require-mysql
2225+
Scenario: Leave the stored value unchanged when an immutable offset sits between renamed offsets with --recurse-keys
2226+
Given a WP install
2227+
And a wp-content/mu-plugins/recurse-keys-immutable-offset.php file:
2228+
"""
2229+
<?php
2230+
class Recurse_Keys_Immutable_Offset_Test extends ArrayObject {
2231+
#[ReturnTypeWillChange]
2232+
public function offsetSet( $key, $value ) {
2233+
if ( 'keep' === $key ) {
2234+
throw new Exception( 'keep is immutable' );
2235+
}
2236+
parent::offsetSet( $key, $value );
2237+
}
2238+
#[ReturnTypeWillChange]
2239+
public function offsetUnset( $key ) {
2240+
if ( 'keep' === $key ) {
2241+
throw new Exception( 'keep is immutable' );
2242+
}
2243+
parent::offsetUnset( $key );
2244+
}
2245+
}
2246+
if ( class_exists( 'WP_CLI' ) ) {
2247+
WP_CLI::add_hook(
2248+
'search_replace_unserialize_options',
2249+
function () {
2250+
return [ 'allowed_classes' => [ 'stdClass', 'Recurse_Keys_Immutable_Offset_Test' ] ];
2251+
}
2252+
);
2253+
}
2254+
"""
2255+
And a setup-recurse-keys-immutable-offset.php file:
2256+
"""
2257+
<?php
2258+
$object = new Recurse_Keys_Immutable_Offset_Test( array( 'oldtok_a' => 'value-a', 'keep' => 'value-keep', 'oldtok_b' => 'value-b' ) );
2259+
update_option( 'recurse_keys_immutable_offset_test', array( 'item' => $object, 'other' => 'oldtok-other' ), false );
2260+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_immutable_offset_test'" );
2261+
"""
2262+
And a check-recurse-keys-immutable-offset.php file:
2263+
"""
2264+
<?php
2265+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_immutable_offset_test'" );
2266+
"""
2267+
And I run `wp eval-file setup-recurse-keys-immutable-offset.php`
2268+
And save STDOUT as {ORIGINAL}
2269+
2270+
When I try `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys`
2271+
Then the return code should be 0
2272+
And STDERR should contain:
2273+
"""
2274+
the whole value was left unchanged
2275+
"""
2276+
2277+
When I run `wp eval-file check-recurse-keys-immutable-offset.php`
2278+
Then STDOUT should be:
2279+
"""
2280+
{ORIGINAL}
2281+
"""
2282+
And STDOUT should contain:
2283+
"""
2284+
s:8:"oldtok_b";s:7:"value-b";
2285+
"""
2286+
2287+
@require-mysql
2288+
Scenario: Leave the stored value unchanged when removing an offset throws after removing it with --recurse-keys
2289+
Given a WP install
2290+
And a wp-content/mu-plugins/recurse-keys-unset-then-throw.php file:
2291+
"""
2292+
<?php
2293+
class Recurse_Keys_Unset_Then_Throw_Test extends ArrayObject {
2294+
#[ReturnTypeWillChange]
2295+
public function offsetUnset( $key ) {
2296+
parent::offsetUnset( $key );
2297+
if ( 'oldtok_b' === $key ) {
2298+
throw new Exception( 'removed oldtok_b, then failed' );
2299+
}
2300+
}
2301+
}
2302+
if ( class_exists( 'WP_CLI' ) ) {
2303+
WP_CLI::add_hook(
2304+
'search_replace_unserialize_options',
2305+
function () {
2306+
return [ 'allowed_classes' => [ 'stdClass', 'Recurse_Keys_Unset_Then_Throw_Test' ] ];
2307+
}
2308+
);
2309+
}
2310+
"""
2311+
And a setup-recurse-keys-unset-then-throw.php file:
2312+
"""
2313+
<?php
2314+
$object = new Recurse_Keys_Unset_Then_Throw_Test( array( 'oldtok_a' => 'value-a', 'keep' => 'value-keep', 'oldtok_b' => 'value-b' ) );
2315+
update_option( 'recurse_keys_unset_then_throw_test', array( 'item' => $object, 'other' => 'oldtok-other' ), false );
2316+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_unset_then_throw_test'" );
2317+
"""
2318+
And a check-recurse-keys-unset-then-throw.php file:
2319+
"""
2320+
<?php
2321+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_unset_then_throw_test'" );
2322+
"""
2323+
And I run `wp eval-file setup-recurse-keys-unset-then-throw.php`
2324+
And save STDOUT as {ORIGINAL}
2325+
2326+
When I try `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys`
2327+
Then the return code should be 0
2328+
And STDERR should contain:
2329+
"""
2330+
the whole value was left unchanged
2331+
"""
2332+
2333+
When I run `wp eval-file check-recurse-keys-unset-then-throw.php`
2334+
Then STDOUT should be:
2335+
"""
2336+
{ORIGINAL}
2337+
"""
2338+
And STDOUT should contain:
2339+
"""
2340+
s:8:"oldtok_b";s:7:"value-b";
2341+
"""
2342+
2343+
@require-mysql @require-php-8.1
2344+
Scenario: Leave nested objects unchanged when a readonly property of their parent cannot be updated with --recurse-keys
2345+
Given a WP install
2346+
And a wp-content/mu-plugins/recurse-keys-readonly-parent.php file:
2347+
"""
2348+
<?php
2349+
class Recurse_Keys_Readonly_Parent_Test {
2350+
public readonly string $label;
2351+
public $child;
2352+
public function __construct( $label, $child ) {
2353+
$this->label = $label;
2354+
$this->child = $child;
2355+
}
2356+
}
2357+
if ( class_exists( 'WP_CLI' ) ) {
2358+
WP_CLI::add_hook(
2359+
'search_replace_unserialize_options',
2360+
function () {
2361+
return [ 'allowed_classes' => [ 'stdClass', 'Recurse_Keys_Readonly_Parent_Test' ] ];
2362+
}
2363+
);
2364+
}
2365+
"""
2366+
And a setup-recurse-keys-readonly-parent.php file:
2367+
"""
2368+
<?php
2369+
$child = new stdClass();
2370+
$child->oldtok_child = 'value-child';
2371+
update_option( 'recurse_keys_readonly_parent_test', array( 'item' => new Recurse_Keys_Readonly_Parent_Test( 'oldtok-label', $child ) ), false );
2372+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_readonly_parent_test'" );
2373+
"""
2374+
And a check-recurse-keys-readonly-parent.php file:
2375+
"""
2376+
<?php
2377+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_readonly_parent_test'" );
2378+
"""
2379+
And I run `wp eval-file setup-recurse-keys-readonly-parent.php`
2380+
And save STDOUT as {ORIGINAL}
2381+
2382+
When I try `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys`
2383+
Then the return code should be 0
2384+
And STDERR should contain:
2385+
"""
2386+
Cannot modify readonly property
2387+
"""
2388+
2389+
When I run `wp eval-file check-recurse-keys-readonly-parent.php`
2390+
Then STDOUT should be:
2391+
"""
2392+
{ORIGINAL}
2393+
"""
2394+
And STDOUT should contain:
2395+
"""
2396+
s:12:"oldtok_child";s:11:"value-child";
2397+
"""
2398+
2399+
@require-mysql
2400+
Scenario: Do not log a value that is left unchanged after a failed key replacement with --recurse-keys --log
2401+
Given a WP install
2402+
And a wp-content/mu-plugins/recurse-keys-failing-log.php file:
2403+
"""
2404+
<?php
2405+
class Recurse_Keys_Failing_Log_Test extends ArrayObject {
2406+
#[ReturnTypeWillChange]
2407+
public function offsetUnset( $key ) {
2408+
throw new Exception( 'removal failed' );
2409+
}
2410+
}
2411+
if ( class_exists( 'WP_CLI' ) ) {
2412+
WP_CLI::add_hook(
2413+
'search_replace_unserialize_options',
2414+
function () {
2415+
return [ 'allowed_classes' => [ 'stdClass', 'Recurse_Keys_Failing_Log_Test' ] ];
2416+
}
2417+
);
2418+
}
2419+
"""
2420+
And a setup-recurse-keys-failing-log.php file:
2421+
"""
2422+
<?php
2423+
$object = new Recurse_Keys_Failing_Log_Test( array( 'oldtok_a' => 'oldtok-discarded-value' ) );
2424+
update_option( 'recurse_keys_failing_log_a', array( 'item' => $object, 'other' => 'oldtok-discarded-sibling' ), false );
2425+
update_option( 'recurse_keys_failing_log_b', array( 'other' => 'oldtok/next-row' ), false );
2426+
"""
2427+
And I run `wp eval-file setup-recurse-keys-failing-log.php`
2428+
2429+
When I try `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys --log`
2430+
Then the return code should be 0
2431+
And STDOUT should contain:
2432+
"""
2433+
< oldtok/next-row
2434+
"""
2435+
And STDOUT should not contain:
2436+
"""
2437+
oldtok-discarded
2438+
"""
2439+
And STDOUT should contain:
2440+
"""
2441+
Success: Made 1 replacement.
2442+
"""

0 commit comments

Comments
 (0)