Version20210102174433.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. final class Version20210102174433 extends AbstractMigration
  8. {
  9. private const TABLE_NAME = 'api_key_roles';
  10. public function up(Schema $schema): void
  11. {
  12. $this->skipIf($schema->hasTable(self::TABLE_NAME));
  13. $table = $schema->createTable(self::TABLE_NAME);
  14. $table->addColumn('id', Types::BIGINT, [
  15. 'unsigned' => true,
  16. 'autoincrement' => true,
  17. 'notnull' => true,
  18. ]);
  19. $table->setPrimaryKey(['id']);
  20. $table->addColumn('role_name', Types::STRING, [
  21. 'length' => 255,
  22. 'notnull' => true,
  23. ]);
  24. $table->addColumn('meta', Types::JSON, [
  25. 'notnull' => true,
  26. ]);
  27. $table->addColumn('api_key_id', Types::BIGINT, [
  28. 'unsigned' => true,
  29. 'notnull' => true,
  30. ]);
  31. $table->addForeignKeyConstraint('api_keys', ['api_key_id'], ['id'], [
  32. 'onDelete' => 'CASCADE',
  33. 'onUpdate' => 'RESTRICT',
  34. ]);
  35. $table->addUniqueIndex(['role_name', 'api_key_id'], 'UQ_role_plus_api_key');
  36. }
  37. public function down(Schema $schema): void
  38. {
  39. $this->skipIf(! $schema->hasTable(self::TABLE_NAME));
  40. $schema->getTable(self::TABLE_NAME)->dropIndex('UQ_role_plus_api_key');
  41. $schema->dropTable(self::TABLE_NAME);
  42. }
  43. /**
  44. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  45. */
  46. public function isTransactional(): bool
  47. {
  48. return false;
  49. }
  50. }