Skip to:
Content

BuddyPress.org

Changeset 8333


Ignore:
Timestamp:
04/30/2014 07:22:59 PM (10 years ago)
Author:
boonebgorges
Message:

When migrating last_activity data, clear all data to avoid duplicates

Previously, duplicates were avoided with a NOT IN subquery. But this caused
load problems on large installations, where the subquery would return
many thousands of results.

Fixes #5572

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-functions.php

    r8324 r8333  
    10371037    $bp = buddypress();
    10381038
    1039     // The "NOT IN" clause prevents duplicates
     1039    // Wipe out existing last_activity data in the activity table -
     1040    // this helps to prevent duplicates when pulling from the usermeta
     1041    // table
     1042    $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity'", $bp->members->id ) );
     1043
    10401044    $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) (
    10411045          SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, '' as action, '' as content, '' as primary_link, 0 as item_id, meta_value AS date_recorded
     
    10431047          WHERE
    10441048            meta_key = 'last_activity'
    1045             AND
    1046             user_id NOT IN (
    1047               SELECT user_id
    1048               FROM {$bp->members->table_name_last_activity}
    1049               WHERE component = '{$bp->members->id}' AND type = 'last_activity'
    1050             )
    10511049    );";
    10521050
  • trunk/tests/testcases/members/functions.php

    r8330 r8333  
    354354        $this->assertSame( $u_obj->user_email, $found_email );
    355355    }
     356
     357    /**
     358     * @group bp_last_activity_migrate
     359     * @expectedIncorrectUsage update_user_meta( $user_id, 'last_activity' )
     360     * @expectedIncorrectUsage get_user_meta( $user_id, 'last_activity' )
     361     */
     362    public function test_bp_last_activity_migrate() {
     363        // We explicitly do not want last_activity created, so use the
     364        // WP factory methods
     365        $u1 = $this->factory->user->create();
     366        $u2 = $this->factory->user->create();
     367        $u3 = $this->factory->user->create();
     368
     369        $time = time();
     370        $t1 = date( 'Y-m-d H:i:s', $time - 50 );
     371        $t2 = date( 'Y-m-d H:i:s', $time - 500 );
     372        $t3 = date( 'Y-m-d H:i:s', $time - 5000 );
     373
     374        update_user_meta( $u1, 'last_activity', $t1 );
     375        update_user_meta( $u2, 'last_activity', $t2 );
     376        update_user_meta( $u3, 'last_activity', $t3 );
     377
     378        // Create an existing entry in last_activity to test no dupes
     379        global $wpdb, $bp;
     380        $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) VALUES ( %d, %s, %s, %s, %s, %s, %d, %s )", $u2, $bp->members->id, 'last_activity', '', '', 0, $t1 ) );
     381
     382        bp_last_activity_migrate();
     383
     384        $expected = array(
     385            $u1 => $t1,
     386            $u2 => $t2,
     387            $u3 => $t3,
     388        );
     389
     390        $found = array(
     391            $u1 => '',
     392            $u2 => '',
     393            $u3 => '',
     394        );
     395
     396        foreach ( $found as $uid => $v ) {
     397            $found[ $uid ] = bp_get_user_last_activity( $uid );
     398        }
     399
     400        $this->assertSame( $expected, $found );
     401    }
    356402}
Note: See TracChangeset for help on using the changeset viewer.