Version20160820191203.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\Migrations\AbstractMigration;
  7. /**
  8. * Auto-generated Migration: Please modify to your needs!
  9. */
  10. class Version20160820191203 extends AbstractMigration
  11. {
  12. public function up(Schema $schema): void
  13. {
  14. // Check if the tables already exist
  15. $tables = $schema->getTables();
  16. foreach ($tables as $table) {
  17. if ($table->getName() === 'tags') {
  18. return;
  19. }
  20. }
  21. $this->createTagsTable($schema);
  22. $this->createShortUrlsInTagsTable($schema);
  23. }
  24. private function createTagsTable(Schema $schema): void
  25. {
  26. $table = $schema->createTable('tags');
  27. $table->addColumn('id', Types::BIGINT, [
  28. 'unsigned' => true,
  29. 'autoincrement' => true,
  30. 'notnull' => true,
  31. ]);
  32. $table->addColumn('name', Types::STRING, [
  33. 'length' => 255,
  34. 'notnull' => true,
  35. ]);
  36. $table->addUniqueIndex(['name']);
  37. $table->setPrimaryKey(['id']);
  38. }
  39. private function createShortUrlsInTagsTable(Schema $schema): void
  40. {
  41. $table = $schema->createTable('short_urls_in_tags');
  42. $table->addColumn('short_url_id', Types::BIGINT, [
  43. 'unsigned' => true,
  44. 'notnull' => true,
  45. ]);
  46. $table->addColumn('tag_id', Types::BIGINT, [
  47. 'unsigned' => true,
  48. 'notnull' => true,
  49. ]);
  50. $table->addForeignKeyConstraint('tags', ['tag_id'], ['id'], [
  51. 'onDelete' => 'CASCADE',
  52. 'onUpdate' => 'RESTRICT',
  53. ]);
  54. $table->addForeignKeyConstraint('short_urls', ['short_url_id'], ['id'], [
  55. 'onDelete' => 'CASCADE',
  56. 'onUpdate' => 'RESTRICT',
  57. ]);
  58. $table->setPrimaryKey(['short_url_id', 'tag_id']);
  59. }
  60. public function down(Schema $schema): void
  61. {
  62. $schema->dropTable('short_urls_in_tags');
  63. $schema->dropTable('tags');
  64. }
  65. /**
  66. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  67. */
  68. public function isTransactional(): bool
  69. {
  70. return false;
  71. }
  72. }