Skip to content

Commit 4d1300b

Browse files
committed
Clear tables in the correct order.
The table definitions have constraints between requests and panels. Clearing the requests table first will violate those constraints. Refs cakephp/docs#3163
1 parent 0ddfe1b commit 4d1300b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Model/Table/RequestsTable.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ public function gc()
7979
if (time() % 100 !== 0) {
8080
return;
8181
}
82-
$query = $this->query()
82+
$purge = $this->find()
83+
->select(['id'])
84+
->hydrate(false)
85+
->where(['requested_at <=' => new \DateTime('-2 weeks')])
86+
->extract('id')
87+
->toArray();
88+
89+
$query = $this->Panels->query()
8390
->delete()
84-
->where(['requested_at <=' => new \DateTime('-2 weeks')]);
91+
->where(['request_id IN' => $purge]);
8592
$statement = $query->execute();
8693
$statement->closeCursor();
8794

88-
$existing = $this->find()->select(['id']);
89-
90-
$query = $this->Panels->query()
95+
$query = $this->query()
9196
->delete()
92-
->where(['request_id NOT IN' => $existing]);
97+
->where(['id IN' => $purge]);
98+
9399
$statement = $query->execute();
94100
$statement->closeCursor();
95101
}

0 commit comments

Comments
 (0)