Skip to content

Commit f779b17

Browse files
Copilotswissspidy
andcommitted
Skip views in search-replace command, report as skipped (view)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 64593b7 commit f779b17

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ 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+
// 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( $args, $views_args );
472+
468473
foreach ( $tables as $table ) {
469474

470475
foreach ( $this->skip_tables as $skip_table ) {
@@ -473,6 +478,13 @@ public function __invoke( $args, $assoc_args ) {
473478
}
474479
}
475480

481+
if ( in_array( $table, $views, true ) ) {
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)