Completed
Push — master ( 5b9784...ff8441 )
by Alejandro
12s
created

Version20180915110857::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkMigrations;
5
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Schema\SchemaException;
8
use Doctrine\Migrations\AbstractMigration;
9
10
/**
11
 * Auto-generated Migration: Please modify to your needs!
12
 */
13
final class Version20180915110857 extends AbstractMigration
14
{
15
    private const ON_DELETE_MAP = [
16
        'visit_locations' => 'SET NULL',
17
        'short_urls' => 'CASCADE',
18
    ];
19
20
    /**
21
     * @param Schema $schema
22
     * @throws SchemaException
23
     */
24
    public function up(Schema $schema): void
25
    {
26
        $visits = $schema->getTable('visits');
27
        $foreignKeys = $visits->getForeignKeys();
28
29
        // Remove all existing foreign keys and add them again with CASCADE delete
30
        foreach ($foreignKeys as $foreignKey) {
31
            $visits->removeForeignKey($foreignKey->getName());
32
            $foreignTable = $foreignKey->getForeignTableName();
33
34
            $visits->addForeignKeyConstraint(
35
                $foreignTable,
36
                $foreignKey->getLocalColumns(),
37
                $foreignKey->getForeignColumns(),
38
                [
39
                    'onDelete' => self::ON_DELETE_MAP[$foreignTable],
40
                    'onUpdate' => 'RESTRICT',
41
                ]
42
            );
43
        }
44
    }
45
46
    public function down(Schema $schema): void
47
    {
48
        // Nothing to run
49
    }
50
}
51