From 2765b587e866c7761bcf08a0860423e330e47b60 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Thu, 3 Dec 2026 16:09:14 -0500 Subject: [PATCH] Use the SQL fast path for search-replace on SQLite The SQLite Database Integration drop-in registers REGEXP as a PDO function, so the column stats and serialized-data detection work there as they do on MySQL. Skipping them sent every column through the row-by-row PHP path. --- features/search-replace.feature | 12 ++++++++++++ src/Search_Replace_Command.php | 17 ++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/features/search-replace.feature b/features/search-replace.feature index 05f176f7..9e753e48 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -194,6 +194,18 @@ Feature: Do global search/replace bar """ + @require-sqlite + Scenario: Use SQL for plain columns and PHP for serialized data on SQLite + Given a WP install + And I run `wp post create --post_title=sqlite-search-target` + And I run `wp option add sr_serialized_match '{"value":"sqlite-search-target"}' --format=json` + + When I run `wp search-replace sqlite-search-target replacement wp_posts wp_options --dry-run` + Then STDOUT should be a table containing rows: + | Table | Column | Replacements | Type | + | wp_posts | post_title | 1 | SQL | + | wp_options | option_value | 1 | PHP | + @require-mysql Scenario: Quiet search/replace Given a WP install diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index d6e73804..d84fa8fa 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -562,7 +562,7 @@ function ( $col ) use ( $table ) { // Gather what the per-column handling needs for all columns in a single table scan, // instead of scanning the table several times for every column. $column_stats = null; - if ( ! $this->regex && ! $this->verbose && 'sqlite' !== Utils\get_db_type() ) { + if ( ! $this->regex && ! $this->verbose ) { $column_stats = self::get_column_stats( $table, $columns, $old ); } @@ -595,17 +595,12 @@ function ( $col ) use ( $table ) { $col_sql = self::esc_sql_ident( $col ); $wpdb->last_error = ''; - if ( 'sqlite' === Utils\get_db_type() ) { - // SQLite does not support REGEXP by default, fall back to PHP processing. - $serial_row = true; - } else { - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident - $serial_row = $wpdb->get_row( "SELECT * FROM $table_sql WHERE $col_sql REGEXP '^[aiO]:[1-9]' LIMIT 1" ); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident + $serial_row = $wpdb->get_row( "SELECT * FROM $table_sql WHERE $col_sql REGEXP '^[aiO]:[1-9]' LIMIT 1" ); - // When the regex triggers an error, we should fall back to PHP - if ( false !== strpos( $wpdb->last_error, 'ERROR 1139' ) ) { - $serial_row = true; - } + // When the regex triggers an error, we should fall back to PHP + if ( false !== strpos( $wpdb->last_error, 'ERROR 1139' ) ) { + $serial_row = true; } }