Skip to content

Commit

Permalink
cleanupOrphanAttachments fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Apr 15, 2020
1 parent f4c8931 commit d6b992e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
26 changes: 9 additions & 17 deletions application/Espo/Jobs/Cleanup.php
Expand Up @@ -209,25 +209,17 @@ protected function cleanupAttachments()
}

if ($this->getConfig()->get('cleanupOrphanAttachments')) {
$collection = $this->getEntityManager()->getRepository('Attachment')->where([
[
'role' => 'Attachment',
],
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null,
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null,
]
],
$selectManager = $this->getContainer()->get('selectManagerFactory')->create('Attachment');

$selectParams = $selectManager->getEmptySelectParams();
$selectManager->applyFilter('orphan', $selectParams);

$selectParams['whereClause'][] = [
'createdAt<' => $datetime->format('Y-m-d H:i:s'),
'createdAt>' => '2018-01-01 00:00:00',
])->limit(0, 5000)->find();
];

$collection = $this->getEntityManager()->getRepository('Attachment')->limit(0, 5000)->find($selectParams);

foreach ($collection as $e) {
$this->getEntityManager()->removeEntity($e);
Expand Down
30 changes: 19 additions & 11 deletions application/Espo/SelectManagers/Attachment.php
Expand Up @@ -35,19 +35,27 @@ protected function filterOrphan(&$result)
{
$result['whereClause'][] = [
'role' => ['Attachment', 'Inline Attachment'],
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null
[
'OR' => [
[
'parentId' => null,
'parentType!=' => null,
'relatedType=' => null,
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null,
],
],
[
'parentType' => null,
'relatedId' => null,
'relatedType!=' => null
]
],
'attachmentChild.id' => null
[
'OR' => [
'relatedType!=' => 'Settings',
'relatedType=' => null,
],
],
'attachmentChild.id' => null,
];

$this->addLeftJoin(['Attachment', 'attachmentChild', [
Expand Down

0 comments on commit d6b992e

Please sign in to comment.