From 9d10c8627a3cf2938d42ac132c77dff9c53635c2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 15 Sep 2018 13:20:13 +0200 Subject: [PATCH] Created migration fixing cascade delete on visits table --- data/migrations/Version20180915110857.php | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 data/migrations/Version20180915110857.php diff --git a/data/migrations/Version20180915110857.php b/data/migrations/Version20180915110857.php new file mode 100644 index 00000000..983ecd6d --- /dev/null +++ b/data/migrations/Version20180915110857.php @@ -0,0 +1,50 @@ + 'SET NULL', + 'short_urls' => 'CASCADE', + ]; + + /** + * @param Schema $schema + * @throws SchemaException + */ + public function up(Schema $schema): void + { + $visits = $schema->getTable('visits'); + $foreignKeys = $visits->getForeignKeys(); + + // Remove all existing foreign keys and add them again with CASCADE delete + foreach ($foreignKeys as $foreignKey) { + $visits->removeForeignKey($foreignKey->getName()); + $foreignTable = $foreignKey->getForeignTableName(); + + $visits->addForeignKeyConstraint( + $foreignTable, + $foreignKey->getLocalColumns(), + $foreignKey->getForeignColumns(), + [ + 'onDelete' => self::ON_DELETE_MAP[$foreignTable], + 'onUpdate' => 'RESTRICT', + ] + ); + } + } + + public function down(Schema $schema): void + { + // Nothing to run + } +}