From a892f72425c4f0082e8d59daae2f01c77672baca Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 2 Oct 2019 20:01:15 +0200 Subject: [PATCH] Added migration to make the combination of slug+domain unique --- data/migrations/Version20191001201532.php | 48 +++++++++++++++++++ .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 3 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 data/migrations/Version20191001201532.php diff --git a/data/migrations/Version20191001201532.php b/data/migrations/Version20191001201532.php new file mode 100644 index 00000000..ff4aebbf --- /dev/null +++ b/data/migrations/Version20191001201532.php @@ -0,0 +1,48 @@ +getTable('short_urls'); + if ($shortUrls->hasIndex('unique_short_code_plus_domain')) { + return; + } + + /** @var Index|null $shortCodesIndex */ + $shortCodesIndex = array_reduce($shortUrls->getIndexes(), function (?Index $found, Index $current) { + [$column] = $current->getColumns(); + return $column === 'short_code' ? $current : $found; + }); + if ($shortCodesIndex === null) { + return; + } + + $shortUrls->dropIndex($shortCodesIndex->getName()); + $shortUrls->addUniqueIndex(['short_code', 'domain_id'], 'unique_short_code_plus_domain'); + } + + /** + * @throws SchemaException + */ + public function down(Schema $schema): void + { + $shortUrls = $schema->getTable('short_urls'); + + $shortUrls->dropIndex('unique_short_code_plus_domain'); + $shortUrls->addUniqueIndex(['short_code']); + } +} diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index c6db2003..47bc9d10 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -28,7 +28,6 @@ $builder->createField('longUrl', Type::STRING) $builder->createField('shortCode', Type::STRING) ->columnName('short_code') - ->unique() ->length(255) ->build(); @@ -66,3 +65,5 @@ $builder->createManyToOne('domain', Entity\Domain::class) ->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT') ->cascadePersist() ->build(); + +$builder->addUniqueConstraint(['short_code', 'domain_id'], 'unique_short_code_plus_domain');