Skip to content

Commit

Permalink
Apply suggestions from Joas
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Mar 7, 2022
1 parent fcff4e6 commit 630cbab
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/Migration/Version23000Date2022022912000001.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,25 @@ class Version23000Date2022022912000001 extends SimpleMigrationStep {
protected $connection;

public function __construct(IDBConnection $connection) {
$this->connection = \OC::$server->get(IDBConnection::class);
$this->connection = $connection;
}

public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
$query = $this->connection->getQueryBuilder();

$query->select($query->func()->count('*', 'num_entry'), 'user_id')
->from('notification_tracker');
// delete all but one entries for each user_id
$query->select($query->func()->max('id', 'newest_entry'), 'user_id')
->from('notification_tracker')
->groupBy('user_id');

$result = $query->executeQuery();
while ($row = $result->fetch()) {
$userId = $row['user_id'];
$entriesToDelete = (int)$row['num_entry'] - 1;
if ($entriesToDelete > 0) {
$delete = $this->connection->getQueryBuilder();
$delete->delete('notification_tracker')
->where($delete->expr()->eq('user_id', $delete->createNamedParameter($userId)))
->setMaxResults($entriesToDelete);

}
$rows = $result->fetchAll();
foreach ($rows as $row)
$delete = $this->connection->getQueryBuilder();
$delete->delete('notification_tracker')
->where($delete->expr()->eq('user_id', $delete->createNamedParameter($row['user_id'])))
->andWhere($delete->expr()->neq('id', $delete->createNamedParameter($row['newest_entry'])));
$delete->execute();
}
}

Expand Down

0 comments on commit 630cbab

Please sign in to comment.