Initialize long URL hash to empty string so that it's not necessary to set it as non-nullable afterwards

This commit is contained in:
Alejandro Celaya committed 2026-06-10 12:06:39 +02:00
1 parent b099166ac8
commit 1ae2ce9e01
5 files changed
+13 -35

No files matched your search

@@ -26,7 +26,7 @@ final class Version20260524105410 extends AbstractMigration
$shortUrls->addColumn(self::COLUMN_NAME, Types::BINARY, [
'length' => 32,
'notnull' => false, // Temporarily nullable until values have been filled
'default' => '', // Temporary value until they have been filled by next migration
]);
$shortUrls->addIndex([self::COLUMN_NAME], self::INDEX_NAME);
}
@@ -20,15 +20,19 @@ final class Version20260607082210 extends AbstractMigration
$qb = $this->connection->createQueryBuilder();
$qb
->select('id', 'original_url')
->from('short_urls');
->from('short_urls')
// If this migration times out, this will ensure it can be rerun, and it will continue where it was left, so
// it can be run multiple times until all short URLs have been processed
->where($qb->expr()->eq('long_url_hash', ':longUrlHash'))
->setParameters(['longUrlHash' => '']);
$shortUrlsResult = $qb->executeQuery();
$iteration = 0;
$iteration = 1;
$this->connection->beginTransaction();
while ($row = $shortUrlsResult->fetchAssociative()) {
// Every few updates, commit the transaction and begin a new one
if (($iteration % 2000) === 0) {
if (($iteration % 10_000) === 0) {
$this->connection->commit();
$this->connection->beginTransaction();
}
@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Makes long_url_hash non-nullable, now that previous migration has set the values for all existing entries
*/
final class Version20260609093926 extends AbstractMigration
{
public function up(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$shortUrls->getColumn('long_url_hash')->setNotnull(true);
}
public function isTransactional(): bool
{
return !$this->connection->getDatabasePlatform() instanceof MySQLPlatform;
}
}