Make WordPress Core

Opened 3 years ago

Last modified 3 weeks ago

#55329 new defect (bug)

add_clean_index function throws PHP notice error

Reported by: gvgvgvijayan's profile gvgvgvijayan Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Upgrade/Install Keywords: has-patch
Focuses: administration Cc:

Description

When I tried to add index for newly created column my error log filled with php notice 25 times because of the function drop_index which is called inside the function add_clean_index.

Sample snippet:

<?php
function add_country_iso_code_column() {
        if ( ! function_exists( 'maybe_add_column' ) ) {
                require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }

        global $wpdb;

        $table_name  = $wpdb->base_prefix . 'defender_lockout_log';
        $column_name = 'country_iso_code';
        $create_ddl  = "ALTER TABLE {$table_name} ADD {$column_name} CHAR(2) DEFAULT NULL";

        maybe_add_column( $table_name, $column_name, $create_ddl );
        add_clean_index( $table_name, $column_name );
}

Error:

[Mon Mar 07 18:45:40.809008 2022] [php:notice] [pid 1167] [client 127.0.0.1:48886] WordPress database error Can't DROP 'country_iso_code_1'; check that column/key exists for query ALTER TABLE `wp_defender_lockout_log` DROP INDEX `country_iso_code_1` made by require_once('wp-admin/network/admin.php'), require_once('wp-admin/admin.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, WP_Defender\\Upgrader->run, WP_Defender\\Upgrader->upgrade_2_8_1, WP_Defender\\Upgrader->add_country_iso_code_column, add_clean_index, drop_index, referer: http://multi.test/wp-admin/network/update.php?action=upload-plugin&package=55&overwrite=update-plugin&_wpnonce=c8b371f5b8

Error log screenshot:
https://i.postimg.cc/2jqsb3Z4/bug-report.png

Change History (3)

This ticket was mentioned in Slack in #core-auto-updates by pbiron. View the logs.


3 years ago

This ticket was mentioned in PR #7737 on WordPress/wordpress-develop by @debarghyabanerjee.


5 weeks ago
#2

  • Keywords has-patch added

Trac Ticket: Core-55329

## Summary

This pull request introduces a new utility function, has_index(), which checks if an index exists for a specific column in a given database table. Additionally, the add_clean_index() function has been refactored to use this new helper function, ensuring that indexes are only added when they don't already exist and preventing unnecessary index drops or duplicate indexes.

## Changes

  • Added has_index() Function
  • This function checks if an index exists for a specified column in a table.
  • It performs a database query using the SHOW INDEX command to determine if an index is associated with the given column.
  • Returns true if an index exists for the column, otherwise returns false.
  • Refactored add_clean_index() Function
  • The add_clean_index() function now uses has_index() to verify whether an index already exists on the specified column before attempting to add it.
  • If the index exists, the function returns early and skips adding the index, avoiding redundant operations.

## Benefits:

  • Improved Performance: By ensuring that indexes are only added if they don't already exist, we avoid unnecessary database operations, reducing execution time and resource usage.
  • Increased Code Reusability: The has_index() function is a reusable utility, simplifying future database operations that involve index checks.
  • Cleaner Logic: The logic for adding indexes is now more streamlined and easier to understand, with the check for existing indexes encapsulated in a dedicated function.

@hemak commented on PR #7737:


5 weeks ago
#3

Thank you !

Note: See TracTickets for help on using tickets.