Skip to content

Commit 69f1ed3

Browse files
CopilotswissspidyCopilot
authored
Skip database views during search-replace (#215)
* Initial plan * Skip views in search-replace command, report as skipped (view) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Don't pass args * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent d8ed8e5 commit 69f1ed3

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

features/search-replace.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ Feature: Do global search/replace
6868
wp_awesome
6969
"""
7070

71+
@require-mysql
72+
Scenario: Skip views during search/replace
73+
Given a WP install
74+
And I run `wp db query "CREATE VIEW wp_posts_view AS SELECT ID, post_title FROM wp_posts;"`
75+
76+
When I run `wp search-replace foo bar --all-tables-with-prefix`
77+
Then STDOUT should contain:
78+
"""
79+
wp_posts_view
80+
"""
81+
And STDOUT should contain:
82+
"""
83+
skipped (view)
84+
"""
85+
86+
When I run `wp search-replace foo bar --all-tables`
87+
Then STDOUT should contain:
88+
"""
89+
wp_posts_view
90+
"""
91+
And STDOUT should contain:
92+
"""
93+
skipped (view)
94+
"""
95+
7196
@require-mysql
7297
Scenario: Run on unregistered, unprefixed tables with --all-tables flag
7398
Given a WP install

src/Search_Replace_Command.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,26 @@ public function __invoke( $args, $assoc_args ) {
465465
// Get table names based on leftover $args or supplied $assoc_args
466466
$tables = Utils\wp_get_table_names( $args, $assoc_args );
467467

468-
foreach ( $tables as $table ) {
468+
// Identify views so they can be skipped; views are dynamic and cannot be directly modified.
469+
$views_args = $assoc_args;
470+
$views_args['views-only'] = true;
471+
$views = Utils\wp_get_table_names( [], $views_args );
472+
$view_set = array_flip( array_intersect( $views, $tables ) );
469473

474+
foreach ( $tables as $table ) {
470475
foreach ( $this->skip_tables as $skip_table ) {
471476
if ( fnmatch( $skip_table, $table ) ) {
472477
continue 2;
473478
}
474479
}
475480

481+
if ( isset( $view_set[ $table ] ) ) {
482+
if ( $this->report && ! $this->report_changed_only ) {
483+
$report[] = array( $table, '', 'skipped (view)', '' );
484+
}
485+
continue;
486+
}
487+
476488
$table_sql = self::esc_sql_ident( $table );
477489

478490
if ( $this->export_handle ) {

0 commit comments

Comments
 (0)