Add a calculated and indexed short URL hash to ShortUrl entity

This commit is contained in:
Alejandro Celaya committed 2026-06-08 17:42:45 +02:00
1 parent 2563b516cb
commit 0db1a19ae9
5 files changed
+122 -1

No files matched your search

@@ -28,6 +28,14 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->length(2048)
->build();
$builder
->createField('longUrlHash', Types::BINARY)
->columnName('long_url_hash')
->length(32)
->build();
$builder->addIndex(['long_url_hash'], 'IDX_long_url_hash');
$shortCodeField = fieldWithUtf8Charset(
$builder->createField('shortCode', Types::STRING),
$emConfig,
+11
View File
@@ -22,6 +22,8 @@ use function array_map;
use function array_pad;
use function array_reduce;
use function explode;
use function hash;
use function hex2bin;
use function implode;
use function is_array;
use function mb_strtolower;
@@ -263,3 +265,12 @@ function geolocationFromRequest(ServerRequestInterface $request): Location|null
return $geolocation;
}
/**
* Generate a SHA256 hash of a string and then convert it to binary
*/
function stringToBinHash(string $value): string
{
// @phpstan-ignore-next-line return.type
return hex2bin(hash('sha256', $value));
}
@@ -0,0 +1,47 @@
<?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;
/**
* Creates a new long_url_hash column, which is temporarily empty and nullable
*/
final class Version20260524105410 extends AbstractMigration
{
private const string COLUMN_NAME = 'long_url_hash';
private const string INDEX_NAME = 'IDX_' . self::COLUMN_NAME;
public function up(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
if ($shortUrls->hasColumn(self::COLUMN_NAME)) {
return;
}
$shortUrls->addColumn(self::COLUMN_NAME, Types::BINARY, [
'length' => 32,
'notnull' => false, // Temporarily nullable until values have been filled
]);
$shortUrls->addIndex([self::COLUMN_NAME], self::INDEX_NAME);
}
public function down(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$this->skipIf(!$shortUrls->hasColumn(self::COLUMN_NAME));
$shortUrls->dropIndex(self::INDEX_NAME);
$shortUrls->dropColumn(self::COLUMN_NAME);
}
public function isTransactional(): bool
{
return !$this->connection->getDatabasePlatform() instanceof MySQLPlatform;
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use function hash;
use function hex2bin;
/**
* Populates the long_url_hash column for every short URL, based on their original_url column
*/
final class Version20260607082210 extends AbstractMigration
{
public function up(Schema $schema): void
{
$qb = $this->connection->createQueryBuilder();
$qb
->select('id', 'original_url')
->from('short_urls');
$result = $qb->executeQuery();
while ($row = $result->fetchAssociative()) {
$updateQb = $this->connection->createQueryBuilder();
$updateQb
->update('short_urls')
->set('long_url_hash', ':binHash')
->where($updateQb->expr()->eq('id', ':id'))
->setParameters([
'id' => $row['id'],
'binHash' => hex2bin(hash('sha256', $row['original_url'])),
])
->setMaxResults(1)
->executeStatement();
}
}
public function isTransactional(): bool
{
return !$this->connection->getDatabasePlatform() instanceof MySQLPlatform;
}
}
+10 -1
View File
@@ -30,10 +30,13 @@ use function array_map;
use function count;
use function Shlinkio\Shlink\Common\normalizeDate;
use function Shlinkio\Shlink\Core\generateRandomShortCode;
use function Shlinkio\Shlink\Core\stringToBinHash;
use function sprintf;
class ShortUrl extends AbstractEntity
{
private(set) string $longUrlHash;
/**
* @param Collection<int, Tag> $tags
* @param Collection<int, Visit>&Selectable<int, Visit> $visits
@@ -41,7 +44,13 @@ class ShortUrl extends AbstractEntity
* @param Collection<int, ShortUrlRedirectRule> $redirectRules
*/
private function __construct(
private(set) string $longUrl,
private(set) string $longUrl {
get => $this->longUrl;
set(string $value) {
$this->longUrl = $value;
$this->longUrlHash = stringToBinHash($value);
}
},
private(set) string $shortCode,
private(set) Chronos $dateCreated = new Chronos(),
private(set) Collection $tags = new ArrayCollection(),