Skip to content

Commit

Permalink
perf(upgrade): speeds up migrating remember me codes
Browse files Browse the repository at this point in the history
The Remember me cookie implementation has been improved by moving codes
to a separate table. This performs that migration in one query.

Fixes #6204
  • Loading branch information
mrclay committed Feb 6, 2014
1 parent 0b4dafd commit 52f9fa4
Showing 1 changed file with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,26 @@

// create remember me table
$query1 = <<<SQL
CREATE TABLE IF NOT EXISTS `{$db_prefix}users_remember_me_cookies` (
`code` varchar(32) NOT NULL,
`guid` bigint(20) unsigned NOT NULL,
`timestamp` int(11) unsigned NOT NULL,
PRIMARY KEY (`code`),
KEY `timestamp` (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `{$db_prefix}users_remember_me_cookies` (
`code` varchar(32) NOT NULL,
`guid` bigint(20) unsigned NOT NULL,
`timestamp` int(11) unsigned NOT NULL,
PRIMARY KEY (`code`),
KEY `timestamp` (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SQL;
update_data($query1);

// move codes
$ia = elgg_set_ignore_access(true);
$options = array(
'type' => 'user',
'limit' => 0,
'selects' => array("u.code as code"),
'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid = u.guid"),
);
$batch = new ElggBatch('elgg_get_entities', $options);
foreach ($batch as $entity) {
$code = $entity->getVolatileData('select:code');
if ($code) {
_elgg_add_remember_me_cookie($entity, $code);
}
}
elgg_set_ignore_access($ia);
$time = time();
$query2 = <<<SQL
INSERT INTO {$db_prefix}users_remember_me_cookies (`code`, `guid`, `timestamp`)
SELECT `code`, `guid`, $time
FROM {$db_prefix}users_entity
WHERE `code` != ''
SQL;
update_data($query2);

// drop code from users table
$query2 = "ALTER TABLE {$db_prefix}users_entity DROP code";
update_data($query2);
$query3 = "ALTER TABLE {$db_prefix}users_entity DROP `code`";
update_data($query3);

0 comments on commit 52f9fa4

Please sign in to comment.