From b50547d868004e3bedc9ad4bd87deffe8b331782 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Mar 2024 13:18:44 +0200 Subject: [PATCH] Create new orphan_visits_counts table --- .../Core/migrations/Version20240331111103.php | 56 +++++++++++++++++++ .../Core/migrations/Version20240331111447.php | 45 +++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 module/Core/migrations/Version20240331111103.php create mode 100644 module/Core/migrations/Version20240331111447.php diff --git a/module/Core/migrations/Version20240331111103.php b/module/Core/migrations/Version20240331111103.php new file mode 100644 index 00000000..ec0a5087 --- /dev/null +++ b/module/Core/migrations/Version20240331111103.php @@ -0,0 +1,56 @@ +skipIf($schema->hasTable('orphan_visits_counts')); + + $table = $schema->createTable('orphan_visits_counts'); + $table->addColumn('id', Types::BIGINT, [ + 'unsigned' => true, + 'autoincrement' => true, + 'notnull' => true, + ]); + $table->setPrimaryKey(['id']); + + $table->addColumn('potential_bot', Types::BOOLEAN, ['default' => false]); + + $table->addColumn('slot_id', Types::INTEGER, [ + 'unsigned' => true, + 'notnull' => true, + 'default' => 1, + ]); + + $table->addColumn('count', Types::BIGINT, [ + 'unsigned' => true, + 'notnull' => true, + 'default' => 1, + ]); + + $table->addUniqueIndex(['potential_bot', 'slot_id'], 'UQ_slot'); + } + + public function down(Schema $schema): void + { + $this->skipIf(! $schema->hasTable('orphan_visits_counts')); + $schema->dropTable('orphan_visits_counts'); + } + + public function isTransactional(): bool + { + return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform); + } +} diff --git a/module/Core/migrations/Version20240331111447.php b/module/Core/migrations/Version20240331111447.php new file mode 100644 index 00000000..333656c4 --- /dev/null +++ b/module/Core/migrations/Version20240331111447.php @@ -0,0 +1,45 @@ +connection->createQueryBuilder(); + $visitsQb->select('COUNT(id)') + ->from('visits') + ->where($visitsQb->expr()->isNull('short_url_id')) + ->andWhere($visitsQb->expr()->eq('potential_bot', ':potential_bot')); + + $botsCount = $visitsQb->setParameter('potential_bot', '1')->executeQuery()->fetchOne(); + $nonBotsCount = $visitsQb->setParameter('potential_bot', '0')->executeQuery()->fetchOne(); + + if ($botsCount > 0) { + $this->insertCount($botsCount, potentialBot: true); + } + if ($nonBotsCount > 0) { + $this->insertCount($nonBotsCount, potentialBot: false); + } + } + + private function insertCount(string|int $count, bool $potentialBot): void + { + $this->connection->createQueryBuilder() + ->insert('orphan_visits_counts') + ->values([ + 'count' => ':count', + 'potential_bot' => ':potential_bot', + ]) + ->setParameters([ + 'count' => $count, + 'potential_bot' => $potentialBot ? '1' : '0', + ]) + ->executeStatement(); + } +}