Version20190930165521.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Schema\SchemaException;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\Migrations\AbstractMigration;
  8. final class Version20190930165521 extends AbstractMigration
  9. {
  10. /**
  11. * @throws SchemaException
  12. */
  13. public function up(Schema $schema): void
  14. {
  15. $shortUrls = $schema->getTable('short_urls');
  16. if ($shortUrls->hasColumn('domain_id')) {
  17. return;
  18. }
  19. $domains = $schema->createTable('domains');
  20. $domains->addColumn('id', Types::BIGINT, [
  21. 'unsigned' => true,
  22. 'autoincrement' => true,
  23. 'notnull' => true,
  24. ]);
  25. $domains->addColumn('authority', Types::STRING, [
  26. 'length' => 512,
  27. 'notnull' => true,
  28. ]);
  29. $domains->addUniqueIndex(['authority']);
  30. $domains->setPrimaryKey(['id']);
  31. $shortUrls->addColumn('domain_id', Types::BIGINT, [
  32. 'unsigned' => true,
  33. 'notnull' => false,
  34. ]);
  35. $shortUrls->addForeignKeyConstraint('domains', ['domain_id'], ['id'], [
  36. 'onDelete' => 'RESTRICT',
  37. 'onUpdate' => 'RESTRICT',
  38. ]);
  39. }
  40. /**
  41. * @throws SchemaException
  42. */
  43. public function down(Schema $schema): void
  44. {
  45. $schema->getTable('short_urls')->dropColumn('domain_id');
  46. $schema->dropTable('domains');
  47. }
  48. /**
  49. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  50. */
  51. public function isTransactional(): bool
  52. {
  53. return false;
  54. }
  55. }