Version20191001201532.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkMigrations;
  4. use Doctrine\DBAL\Schema\Index;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\DBAL\Schema\SchemaException;
  7. use Doctrine\Migrations\AbstractMigration;
  8. use function array_reduce;
  9. final class Version20191001201532 extends AbstractMigration
  10. {
  11. /**
  12. * @throws SchemaException
  13. */
  14. public function up(Schema $schema): void
  15. {
  16. $shortUrls = $schema->getTable('short_urls');
  17. if ($shortUrls->hasIndex('unique_short_code_plus_domain')) {
  18. return;
  19. }
  20. /** @var Index|null $shortCodesIndex */
  21. $shortCodesIndex = array_reduce($shortUrls->getIndexes(), function (?Index $found, Index $current) {
  22. [$column] = $current->getColumns();
  23. return $column === 'short_code' ? $current : $found;
  24. });
  25. if ($shortCodesIndex === null) {
  26. return;
  27. }
  28. $shortUrls->dropIndex($shortCodesIndex->getName());
  29. $shortUrls->addUniqueIndex(['short_code', 'domain_id'], 'unique_short_code_plus_domain');
  30. }
  31. /**
  32. * @throws SchemaException
  33. */
  34. public function down(Schema $schema): void
  35. {
  36. $shortUrls = $schema->getTable('short_urls');
  37. $shortUrls->dropIndex('unique_short_code_plus_domain');
  38. $shortUrls->addUniqueIndex(['short_code']);
  39. }
  40. /**
  41. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  42. */
  43. public function isTransactional(): bool
  44. {
  45. return false;
  46. }
  47. }