Version20180915110857.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Migrations\AbstractMigration;
  7. /**
  8. * Auto-generated Migration: Please modify to your needs!
  9. */
  10. final class Version20180915110857 extends AbstractMigration
  11. {
  12. private const ON_DELETE_MAP = [
  13. 'visit_locations' => 'SET NULL',
  14. 'short_urls' => 'CASCADE',
  15. ];
  16. /**
  17. * @throws SchemaException
  18. */
  19. public function up(Schema $schema): void
  20. {
  21. $visits = $schema->getTable('visits');
  22. $foreignKeys = $visits->getForeignKeys();
  23. // Remove all existing foreign keys and add them again with CASCADE delete
  24. foreach ($foreignKeys as $foreignKey) {
  25. $visits->removeForeignKey($foreignKey->getName());
  26. $foreignTable = $foreignKey->getForeignTableName();
  27. $visits->addForeignKeyConstraint(
  28. $foreignTable,
  29. $foreignKey->getLocalColumns(),
  30. $foreignKey->getForeignColumns(),
  31. [
  32. 'onDelete' => self::ON_DELETE_MAP[$foreignTable],
  33. 'onUpdate' => 'RESTRICT',
  34. ],
  35. );
  36. }
  37. }
  38. public function down(Schema $schema): void
  39. {
  40. // Nothing to run
  41. }
  42. /**
  43. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  44. */
  45. public function isTransactional(): bool
  46. {
  47. return false;
  48. }
  49. }