Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SnapshotManager: use QueryBuilder #1446

Merged
merged 10 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"require": {
"php": "^7.4 || ^8.0",
"cocur/slugify": "^3.0 || ^4.0",
"doctrine/doctrine-bundle": "^1.12.3 || ^2.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
Hanmac marked this conversation as resolved.
Show resolved Hide resolved
"doctrine/doctrine-bundle": "^2.5",
"doctrine/persistence": "^2.1",
"sonata-project/admin-bundle": "^3.99",
"sonata-project/block-bundle": "^3.20",
"sonata-project/datagrid-bundle": "^2.5 || ^3.0",
Expand Down Expand Up @@ -73,6 +73,7 @@
"vimeo/psalm": "^4.7.2"
},
"conflict": {
"doctrine/orm": "<2.10",
"friendsofsymfony/rest-bundle": "<2.1",
"jms/serializer": "<0.13",
"sonata-project/cache": "<2.0",
Expand Down
98 changes: 38 additions & 60 deletions src/Entity/SnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\PageBundle\Entity;

use Doctrine\Common\Collections\Criteria;
use Doctrine\Persistence\ManagerRegistry;
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
Expand Down Expand Up @@ -99,16 +100,17 @@ public function enableSnapshots(array $snapshots, ?\DateTime $date = null)
}

$this->getEntityManager()->flush();
// @todo: strange sql and low-level pdo usage: use dql or qb
$sql = sprintf(
"UPDATE %s SET publication_date_end = '%s' WHERE id NOT IN(%s) AND page_id IN (%s) and publication_date_end IS NULL",
$this->getTableName(),
$date->format('Y-m-d H:i:s'),
implode(',', $snapshotIds),
implode(',', $pageIds)
);

$this->getConnection()->query($sql);
$qb = $this->getRepository()->createQueryBuilder('s');
$q = $qb->update()
->set('s.publicationDateEnd', ':date_end')
->where($qb->expr()->notIn('s.id', $snapshotIds))
->andWhere($qb->expr()->in('s.page', $pageIds))
->andWhere($qb->expr()->isNull('s.publicationDateEnd'))
->setParameter('date_end', $date, 'datetime')
->getQuery();

$q->execute();
}

public function findEnableSnapshot(array $criteria)
Expand Down Expand Up @@ -231,60 +233,36 @@ public function cleanup(PageInterface $page, $keep)
throw new \RuntimeException(sprintf('Please provide an integer value, %s given', \gettype($keep)));
}

$tableName = $this->getTableName();
$platform = $this->getConnection()->getDatabasePlatform()->getName();

if ('mysql' === $platform) {
return $this->getConnection()->exec(sprintf(
'DELETE FROM %s
WHERE
page_id = %d
AND id NOT IN (
SELECT id
FROM (
SELECT id, publication_date_end
FROM %s
WHERE
page_id = %d
ORDER BY
publication_date_end IS NULL DESC,
publication_date_end DESC
LIMIT %d
) AS table_alias
)',
$tableName,
$page->getId(),
$tableName,
$page->getId(),
$keep
));
}
$innerQb = $this->getRepository()->createQueryBuilder('i');
$expr = $innerQb->expr();

if ('oracle' === $platform) {
return $this->getConnection()->exec(sprintf(
'DELETE FROM %s
WHERE
page_id = %d
AND id NOT IN (
SELECT id
FROM (
SELECT id, publication_date_end
FROM %s
WHERE
page_id = %d
AND rownum <= %d
ORDER BY publication_date_end DESC
) table_alias
)',
$tableName,
$page->getId(),
$tableName,
$page->getId(),
$keep
// try a better Function expression for this?
$ifNullExpr = sprintf(
'CASE WHEN %s THEN 1 ELSE 0 END',
$expr->isNull('i.publicationDateEnd')
);

// Subquery DQL doesn't support Limit
$innerQb
->select('i.id')
->where($expr->eq('i.page', $page->getId()))
->orderBy($ifNullExpr, Criteria::DESC)
->addOrderBy('i.publicationDateEnd', Criteria::DESC)
->setMaxResults($keep);

$query = $innerQb->getQuery();
$innerArray = $query->getSingleColumnResult();

$qb = $this->getRepository()->createQueryBuilder('s');
$expr = $qb->expr();
$qb->delete()
->where($expr->eq('s.page', $page->getId()))
->andWhere($expr->notIn(
's.id',
$innerArray
Hanmac marked this conversation as resolved.
Show resolved Hide resolved
));
}

throw new \RuntimeException(sprintf('The %s database platform has not been tested yet. Please report us if it works and feel free to create a pull request to handle it ;-)', $platform));
return $qb->getQuery()->execute();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Entity/SonataPageBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @ORM\Table(name="page__block")
* @ORM\HasLifecycleCallbacks
*/
final class SonataPageBlock extends BaseBlock
class SonataPageBlock extends BaseBlock
{
/**
* @ORM\Id
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Entity/SonataPagePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @ORM\Table(name="page__page")
* @ORM\HasLifecycleCallbacks
*/
final class SonataPagePage extends BasePage
class SonataPagePage extends BasePage
{
/**
* @ORM\Id
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Entity/SonataPageSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @ORM\Table(name="page__site")
* @ORM\HasLifecycleCallbacks
*/
final class SonataPageSite extends BaseSite
class SonataPageSite extends BaseSite
{
/**
* @ORM\Id
Expand Down
7 changes: 6 additions & 1 deletion tests/App/Entity/SonataPageSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* })
* })
*/
final class SonataPageSnapshot extends BaseSnapshot
class SonataPageSnapshot extends BaseSnapshot
{
/**
* @ORM\Id
Expand Down Expand Up @@ -61,4 +61,9 @@ public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}
}
70 changes: 0 additions & 70 deletions tests/Entity/BlockManagerTest.php

This file was deleted.

Loading