Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/doctrine-mapping/entities/MediaVersion.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<entity name="Softspring\MediaBundle\Entity\MediaVersion" table="media_version">
<cache usage="NONSTRICT_READ_WRITE" region="default" />

<unique-constraints>
<unique-constraint name="uniq_media_version_media_id_version" columns="media_id,version" />
</unique-constraints>

<id name="id" type="string" length="36" column="id">
<options><option name="fixed">true</option></options>
<generator strategy="CUSTOM" />
Expand Down
8 changes: 8 additions & 0 deletions src/Command/TypesMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class TypesMigrationCommand extends Command
{
Expand Down Expand Up @@ -45,6 +46,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('');
} catch (InvalidTypeException $e) {
$output->writeln(sprintf('<error>Media "%s" has an error. Type "%s" is invalid</error>', $media->getName(), $media->getType()));
} catch (Throwable $e) {
$output->writeln(sprintf(
'<error>Media "%s" of type "%s" could not be migrated: %s</error>',
$media->getName(),
$media->getType(),
$e->getMessage(),
));
}
}

Expand Down
81 changes: 81 additions & 0 deletions src/Migrations/Version20260728113500.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Softspring\MediaBundle\Migrations;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260728113500 extends AbstractMigration
{
private const UNIQUE_INDEX = 'uniq_media_version_media_id_version';

public function getDescription(): string
{
return 'Remove duplicate media versions and enforce one version per media';
}

public function up(Schema $schema): void
{
if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$this->addSql(
<<<'SQL'
DELETE FROM media_version AS mv_duplicate
USING media_version AS keeper
WHERE keeper.media_id = mv_duplicate.media_id
AND keeper.version = mv_duplicate.version
AND (
(mv_duplicate.url IS NULL AND keeper.url IS NOT NULL)
OR (
(mv_duplicate.url IS NULL) = (keeper.url IS NULL)
AND mv_duplicate.id < keeper.id
)
)
SQL,
);
$this->addSql(sprintf(
'CREATE UNIQUE INDEX %s ON media_version (media_id, version)',
self::UNIQUE_INDEX,
));

return;
}

$this->addSql(
<<<'SQL'
DELETE mv_duplicate
FROM media_version AS mv_duplicate
INNER JOIN media_version AS keeper
ON keeper.media_id = mv_duplicate.media_id
AND keeper.version = mv_duplicate.version
AND (
(mv_duplicate.url IS NULL AND keeper.url IS NOT NULL)
OR (
(mv_duplicate.url IS NULL) = (keeper.url IS NULL)
AND mv_duplicate.id < keeper.id
)
)
SQL,
);
$this->addSql(sprintf(
'CREATE UNIQUE INDEX %s ON media_version (media_id, version)',
self::UNIQUE_INDEX,
));
}

public function down(Schema $schema): void
{
if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$this->addSql(sprintf('DROP INDEX %s', self::UNIQUE_INDEX));

return;
}

$this->addSql(sprintf(
'DROP INDEX %s ON media_version',
self::UNIQUE_INDEX,
));
}
}
Loading