Skip to content

Commit

Permalink
fix: Delete inactive sessions in one query
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jun 21, 2023
1 parent 55f5a07 commit 6488bbf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- **💾 Open format:** Files are saved as [Markdown](https://en.wikipedia.org/wiki/Markdown), so you can edit them from any other text app too.
- **✊ Strong foundation:** We use [🐈 tiptap](https://tiptap.scrumpy.io) which is based on [🦉 ProseMirror](https://prosemirror.net) – huge thanks to them!
]]></description>
<version>3.9.0</version>
<version>3.9.1</version>
<licence>agpl</licence>
<author mail="[email protected]">Julius Härtl</author>
<namespace>Text</namespace>
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'OCA\\Text\\Migration\\Version030201Date20201116123153' => $baseDir . '/../lib/Migration/Version030201Date20201116123153.php',
'OCA\\Text\\Migration\\Version030501Date20220202101853' => $baseDir . '/../lib/Migration/Version030501Date20220202101853.php',
'OCA\\Text\\Migration\\Version030701Date20230207131313' => $baseDir . '/../lib/Migration/Version030701Date20230207131313.php',
'OCA\\Text\\Migration\\Version030901Date20230615085512' => $baseDir . '/../lib/Migration/Version030901Date20230615085512.php',
'OCA\\Text\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
'OCA\\Text\\Service\\ApiService' => $baseDir . '/../lib/Service/ApiService.php',
'OCA\\Text\\Service\\AttachmentService' => $baseDir . '/../lib/Service/AttachmentService.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ComposerStaticInitText
'OCA\\Text\\Migration\\Version030201Date20201116123153' => __DIR__ . '/..' . '/../lib/Migration/Version030201Date20201116123153.php',
'OCA\\Text\\Migration\\Version030501Date20220202101853' => __DIR__ . '/..' . '/../lib/Migration/Version030501Date20220202101853.php',
'OCA\\Text\\Migration\\Version030701Date20230207131313' => __DIR__ . '/..' . '/../lib/Migration/Version030701Date20230207131313.php',
'OCA\\Text\\Migration\\Version030901Date20230615085512' => __DIR__ . '/..' . '/../lib/Migration/Version030901Date20230615085512.php',
'OCA\\Text\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
'OCA\\Text\\Service\\ApiService' => __DIR__ . '/..' . '/../lib/Service/ApiService.php',
'OCA\\Text\\Service\\AttachmentService' => __DIR__ . '/..' . '/../lib/Service/AttachmentService.php',
Expand Down
33 changes: 16 additions & 17 deletions lib/Db/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,29 @@ public function findAllInactive() {
return $this->findEntities($qb);
}

public function deleteInactiveWithoutSteps(?int $documentId = null) {
$qb = $this->db->getQueryBuilder();
$qb->select('session_id')
->from('text_steps');
public function deleteInactiveWithoutSteps(?int $documentId = null): int {
$selectSubQuery = $this->db->getQueryBuilder();
$selectSubQuery->select('s.id')
->from('text_sessions', 's')
->leftJoin('s', 'text_steps', 'st', $selectSubQuery->expr()->eq('st.session_id', 'st.id'))
->where($selectSubQuery->expr()->lt('last_contact', $selectSubQuery->createParameter('lastContact')))
->andWhere($selectSubQuery->expr()->isNull('st.id'));
if ($documentId !== null) {
$qb->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
$selectSubQuery->andWhere($selectSubQuery->expr()->eq('s.document_id', $selectSubQuery->createParameter('documentId')));
}
$result = $qb
->groupBy('session_id')
->executeQuery();
$activeSessions = $result->fetchAll(\PDO::FETCH_COLUMN);
$result->closeCursor();

$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName());
$qb->where($qb->expr()->lt('last_contact', $qb->createNamedParameter(time() - SessionService::SESSION_VALID_TIME)));
if ($documentId !== null) {
$qb->andWhere($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
}
$qb->andWhere($qb->expr()->notIn('id', $qb->createNamedParameter($activeSessions, IQueryBuilder::PARAM_INT_ARRAY)));
$qb->delete($this->getTableName())
->where($qb->expr()->in('id', $qb->createFunction('(' . $selectSubQuery->getSQL() . ')')));
$qb->setParameters([
'lastContact' => time() - SessionService::SESSION_VALID_TIME,
'documentId' => $documentId,
]);

return $qb->executeStatement();
}

public function deleteByDocumentId($documentId) {
public function deleteByDocumentId($documentId): int {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
Expand Down
32 changes: 32 additions & 0 deletions lib/Migration/Version030901Date20230615085512.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace OCA\Text\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version030901Date20230615085512 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('text_steps');
if (!$table->hasIndex('ts_session')) {
$table->addIndex(['session_id'], 'ts_session');
return $schema;
}

return null;
}
}

0 comments on commit 6488bbf

Please sign in to comment.