mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Create new orphan_visits_counts table
This commit is contained in:
parent
401046fbe5
commit
b50547d868
2 changed files with 101 additions and 0 deletions
56
module/Core/migrations/Version20240331111103.php
Normal file
56
module/Core/migrations/Version20240331111103.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Create a new orphan_visits_counts that will work similarly to the short_url_visits_counts
|
||||
*/
|
||||
final class Version20240331111103 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
45
module/Core/migrations/Version20240331111447.php
Normal file
45
module/Core/migrations/Version20240331111447.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20240331111447 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$visitsQb = $this->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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue