Skip to content

Commit b517efb

Browse files
swissspidyclaude
andcommitted
Fix --exclude_revisions test and apply the filter to any posts table
- The scenario lacked 'Given a WP install', so it failed on every CI job. Use --format=count/ids on wp_posts so the test asserts the revision is gone. - Build the revision filter once per table, for any *posts table that has a post_type column (covers other sites' tables in a network), and use it in both the LIKE and --regex queries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent fdfae59 commit b517efb

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

‎features/db-search.feature‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -983,30 +983,36 @@ Feature: Search through the database
983983
And STDERR should be empty
984984

985985
Scenario: Search with exclude revisions option
986-
And I run `wp post create --post_content="This is the original post content." --post_title="Original Post" --porcelain`
987-
Then save STDOUT as {POST_ID}
988-
# Create a revision
986+
Given a WP install
987+
And I run `wp post create --post_content="This is the original post content." --post_title="Original Post" --post_status=publish --porcelain`
988+
And save STDOUT as {POST_ID}
989989
And I run `wp post update {POST_ID} --post_content="This is the updated post content."`
990-
When I run `wp post list --post_parent={POST_ID} --post_type=revision --field=ID`
991-
Then save STDOUT as {REVISION_ID}
992990

993-
When I run `wp db search "updated post content"`
994-
Then STDOUT should contain:
991+
When I run `wp db search "updated post content" wp_posts --format=count`
992+
Then STDOUT should be:
995993
"""
996-
wp_posts:post_content
997-
{POST_ID}:This is the updated post content.
998-
wp_posts:post_content
999-
{REVISION_ID}:This is the updated post content.
994+
2
995+
"""
996+
997+
When I run `wp db search "updated post content" wp_posts --exclude_revisions --format=ids`
998+
Then STDOUT should be:
999+
"""
1000+
{POST_ID}
10001001
"""
10011002

10021003
When I run `wp db search "updated post content" --exclude_revisions`
10031004
Then STDOUT should contain:
10041005
"""
1005-
wp_posts:post_content
10061006
{POST_ID}:This is the updated post content.
10071007
"""
10081008
And STDERR should be empty
10091009

1010+
When I run `wp db search "updated post content" wp_posts --exclude_revisions --regex --format=count`
1011+
Then STDOUT should be:
1012+
"""
1013+
1
1014+
"""
1015+
10101016
@skip-windows
10111017
Scenario: Search with custom colors
10121018
Given a WP install

‎src/DB_Command.php‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,23 +1807,22 @@ public function search( $args, $assoc_args ) {
18071807
$primary_key_sql = self::esc_sql_ident( $primary_key ) . ', ';
18081808
}
18091809

1810+
// Skip revisions in any posts table (including other sites' tables in a multisite network).
1811+
$revisions_sql = '';
1812+
if ( $exclude_revisions && preg_match( '/posts$/', $table ) && in_array( 'post_type', $all_columns, true ) ) {
1813+
$revisions_sql = ' ' . self::esc_sql_ident( 'post_type' ) . " <> 'revision'";
1814+
}
1815+
18101816
foreach ( $text_columns as $column ) {
1811-
$column_sql = self::esc_sql_ident( $column );
1812-
$post_type_sql = self::esc_sql_ident( 'post_type' );
1817+
$column_sql = self::esc_sql_ident( $column );
18131818
if ( $regex ) {
1814-
if ( $exclude_revisions && $wpdb->posts === $table ) {
1815-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
1816-
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$post_type_sql} NOT IN ( 'revision' )" );
1817-
} else {
1818-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
1819-
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" );
1820-
}
1821-
} elseif ( $exclude_revisions && $wpdb->posts === $table ) {
1819+
$where_sql = $revisions_sql ? " WHERE{$revisions_sql}" : '';
18221820
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
1823-
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s AND {$post_type_sql} NOT IN ( 'revision' )", $esc_like_search ) );
1821+
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}{$where_sql}" );
18241822
} else {
1823+
$where_sql = $revisions_sql ? " AND{$revisions_sql}" : '';
18251824
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
1826-
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s;", $esc_like_search ) );
1825+
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s{$where_sql};", $esc_like_search ) );
18271826
}
18281827
if ( $results ) {
18291828
$row_count += count( $results );

0 commit comments

Comments
 (0)