diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-10-03 02:14:23 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-10-03 02:14:23 +0000 |
commit | f45d9a2892bbb27e70d4be78afdbdd31c4058cca (patch) | |
tree | 88468fa24c4da543c1cb25b014acc4a72c0e0561 /modules/dblog | |
parent | 33695f71f3ac6b2a88419874799d89c75e4fcaab (diff) | |
download | brdo-f45d9a2892bbb27e70d4be78afdbdd31c4058cca.tar.gz brdo-f45d9a2892bbb27e70d4be78afdbdd31c4058cca.tar.bz2 |
- Patch #917134 by hbergin: dblog_cron() deletes too many records when auto_increment() is larger than 1.
Diffstat (limited to 'modules/dblog')
-rw-r--r-- | modules/dblog/dblog.module | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/modules/dblog/dblog.module b/modules/dblog/dblog.module index dac5387c4..590844786 100644 --- a/modules/dblog/dblog.module +++ b/modules/dblog/dblog.module @@ -100,12 +100,25 @@ function dblog_init() { * Remove expired log messages and flood control events. */ function dblog_cron() { - // Cleanup the watchdog table - if (variable_get('dblog_row_limit', 1000) > 0) { - $max = Database::getConnection('default', 'default')->query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); - Database::getConnection('default', 'default')->delete('watchdog') - ->condition('wid', $max - variable_get('dblog_row_limit', 1000), '<=') - ->execute(); + // Cleanup the watchdog table. + $row_limit = variable_get('dblog_row_limit', 1000); + + // For row limit n, get the wid of the nth row in descending wid order. + // Counting the most recent n rows avoids issues with wid number sequences, + // e.g. auto_increment value > 1 or rows deleted directly from the table. + if ($row_limit > 0) { + $min_row = db_select('watchdog', 'w') + ->fields('w', array('wid')) + ->orderBy('wid', 'DESC') + ->range($row_limit - 1, 1) + ->execute()->fetchField(); + + // Delete all table entries older than the nth row, if nth row was found. + if ($min_row) { + db_delete('watchdog') + ->condition('wid', $min_row, '<') + ->execute(); + } } } |