Skip to content

Commit 97be3cd

Browse files
committed
Add correct spelling for removeOldestIfMoreThan and removeOlderThan functions.
Fixed issue with said functions not working when using mongodb.
1 parent b07495b commit 97be3cd

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ No indexes are added per default, so if you fetch a lot of log results based on
214214
#### Log Cleanup
215215
There is a helper function to remove the oldest log events and keep a specified number
216216
```php
217-
LogToDB::removeOldestIfMoreThen(100);
217+
LogToDB::removeOldestIfMoreThan(100);
218218
```
219219
Or based on date (most be valid date/datetime supported by strtotime())
220220
http://php.net/manual/en/function.strtotime.php
221221

222222
```php
223-
LogToDB::model()->removeOlderThen('2019-01-01');
224-
LogToDB::model()->removeOlderThen('2019-01-01 23:00:00');
223+
LogToDB::model()->removeOlderThan('2019-01-01');
224+
LogToDB::model()->removeOlderThan('2019-01-01 23:00:00');
225225
```
226226

227227
#### Processors

src/Models/LogToDbCreateObject.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,29 @@ private function jsonDecodeIfNotEmpty($value, $arraymode = true)
156156
return $value;
157157
}
158158

159+
/**
160+
* Delete the oldest records based on unix_time, silly spelling version.
161+
*
162+
* @param int $max amount of records to keep
163+
* @return bool
164+
*/
165+
public function removeOldestIfMoreThen(int $max)
166+
{
167+
return $this->removeOldestIfMoreThan($max);
168+
}
169+
159170
/**
160171
* Delete the oldest records based on unix_time
161172
*
162-
* @param int $max
173+
* @param int $max amount of records to keep
163174
* @return bool success
164175
*/
165-
public function removeOldestIfMoreThen(int $max)
176+
public function removeOldestIfMoreThan(int $max)
166177
{
167178
$current = $this->count();
168179
if ($current > $max) {
169-
$keepers = $this->orderBy('unix_time', 'DESC')->take($max)->pluck('id')->toArray();
170-
if ($this->whereNotIn('id', $keepers)->get()->each->delete()) {
180+
$keepers = $this->orderBy('unix_time', 'DESC')->take($max)->pluck($this->primaryKey)->toArray();
181+
if ($this->whereNotIn($this->primaryKey, $keepers)->get()->each->delete()) {
171182
return true;
172183
}
173184
}
@@ -176,17 +187,28 @@ public function removeOldestIfMoreThen(int $max)
176187
}
177188

178189
/**
179-
* Delete records based on date.
190+
* Delete records based on date, silly spelling version.
180191
*
181192
* @param string $datetime date supported by strtotime: http://php.net/manual/en/function.strtotime.php
182193
* @return bool success
183194
*/
184195
public function removeOlderThen(string $datetime)
196+
{
197+
return $this->removeOlderThan($datetime);
198+
}
199+
200+
/**
201+
* Delete records based on date.
202+
*
203+
* @param string $datetime date supported by strtotime: http://php.net/manual/en/function.strtotime.php
204+
* @return bool success
205+
*/
206+
public function removeOlderThan(string $datetime)
185207
{
186208
$unixtime = strtotime($datetime);
187209

188-
$keepers = $this->where('unix_time', '>=', $unixtime)->pluck('id')->toArray();
189-
$deletes = $this->whereNotIn('id', $keepers)->get();
210+
$keepers = $this->where('unix_time', '>=', $unixtime)->pluck($this->primaryKey)->toArray();
211+
$deletes = $this->whereNotIn($this->primaryKey, $keepers)->get();
190212
if (!$deletes->isEmpty()){
191213
if ($deletes->each->delete()) {
192214
return true;

tests/LogToDbTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,44 @@ public function testStandAloneModels() {
322322
* @group cleanup
323323
*/
324324
public function testRemoves() {
325-
$this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(1));
326-
$this->assertFalse(LogToDB::model()->removeOlderThen(date('Y-m-d')));
325+
Log::debug("This is an test DEBUG log event");
326+
Log::info("This is an test INFO log event");
327+
Log::notice("This is an test NOTICE log event");
328+
329+
//sleep to pass time for record cleanup testing based on time next.
330+
sleep(1);
331+
332+
$this->assertTrue(LogToDB::model()->removeOldestIfMoreThan(2));
333+
$this->assertEquals(2, LogToDB::model()->count());
334+
$this->assertTrue(LogToDB::model()->removeOlderThan(date('Y-m-d H:i:s')));
335+
$this->assertEquals(0, LogToDB::model()->count());
336+
337+
//Same tests on mongodb
338+
$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThan(2));
339+
$this->assertEquals(2, LogToDB::model('mongodb')->count());
340+
$this->assertTrue(LogToDB::model('mongodb')->removeOlderThan(date('Y-m-d H:i:s')));
341+
$this->assertEquals(0, LogToDB::model('mongodb')->count());
342+
343+
344+
//test wrappers for silly spelling
345+
Log::debug("This is an test DEBUG log event");
346+
Log::info("This is an test INFO log event");
347+
Log::notice("This is an test NOTICE log event");
348+
349+
//sleep to pass time for record cleanup testing based on time next.
350+
sleep(1);
351+
352+
$this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(2));
353+
$this->assertEquals(2, LogToDB::model()->count());
354+
$this->assertTrue(LogToDB::model()->removeOlderThen(date('Y-m-d H:i:s')));
355+
$this->assertEquals(0, LogToDB::model()->count());
356+
357+
//Same tests on mongodb
358+
$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThen(2));
359+
$this->assertEquals(2, LogToDB::model('mongodb')->count());
360+
$this->assertTrue(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d H:i:s')));
361+
$this->assertEquals(0, LogToDB::model('mongodb')->count());
327362

328-
$this->assertTrue(LogToDB::model('mongodb')->removeOldestIfMoreThen(1));
329-
$this->assertFalse(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d')));
330363
}
331364

332365
/**

0 commit comments

Comments
 (0)