WordPress.org

Ready to get started?Download WordPress

Forums

WordPress plugin tables become corrupt (1 post)

  1. fixedpoint
    Member
    Posted 9 months ago #

    I have written a plugin that creates and uses 3 database tables. On rare occasion, all 3 database tables become corrupt but no other tables in the database exhibit similar conditions. It is pretty hard to pinpoint the precise cause because it happens infrequently but once it occurs on an installation it appears to happen more frequently there. We don't yet know what is the root cause, so we have reviewed all of the code.

    I make an assumption that database reads can't corrupt the database tables. This leaves us with a handful of operations that write to the various tables. But any explanation would need to account for the fact that all three tables are corrupt. No single operation writes to all three tables except the installation function which runs when the plugin is activated.

    This has led me to believe that perhaps the installations that see corrupt tables are reactivating plugins by rebooting the wordpress server or somehow triggering the activation code.

    Also of note is that since some of the data that is stored comes from user input, it may contain unicode characters.

    Here is our activation code:

    register_activation_hook(__FILE__, 'plugin_install');
    add_action('plugins_loaded', 'plugin_install');
    
    function plugin_install() {
        global $wpdb;
    
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
        $foo_table_name = $wpdb->prefix . "plugin_foo";
        $foo_sql = "CREATE TABLE $foo_table_name (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            fred TEXT NOT NULL,
            george TEXT NOT NULL,
            tom TEXT,
            bill BIGINT(20) UNSIGNED,
            rick TEXT NOT NULL,
            kyle TIMESTAMP DEFAULT NOW() NOT NULL,
            wallace BIGINT(20) UNSIGNED,
            UNIQUE KEY id (id)
            );";
    
        dbDelta($foo_sql);
    
        $bar_table_name = $wpdb->prefix . "plugin_bar";
        $bar_sql = "CREATE TABLE $bar_table_name (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            sally TEXT NOT NULL,
            judy TEXT NOT NULL,
            UNIQUE KEY id (id)
            );";
    
        dbDelta($bar_sql);
    
        $baz_table_name = $wpdb->prefix . "plugin_baz";
        $baz_sql = "CREATE TABLE $baz_table_name (
            id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            bear BIGINT(20) NOT NULL,
            deer TIMESTAMP DEFAULT NOW() NOT NULL,
            UNIQUE KEY id (id)
            );";
    
        dbDelta($baz_sql);
    
        $bar_row = $wpdb->get_row("SELECT * FROM $bar_table_name WHERE sally='qux'");
        if (empty($bar_row)) {
            $default_bar = '42';
    
            $bar_data = array(
                "sally" => "qux",
                "judy" => $default_bar
                );
    
            $wpdb->insert($bar_table_name, $bar_data);
        }
    }

Reply

You must log in to post.

About this Topic