Passed
Push — master ( 33b245...1c8e6e )
by Christian
12:09 queued 12s
created

Migration1604502151AddThemePreviewMediaConstraint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreationTimestamp() 0 3 1
A updateDestructive() 0 2 1
A update() 0 15 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Migration;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\Migration\MigrationStep;
7
8
class Migration1604502151AddThemePreviewMediaConstraint extends MigrationStep
9
{
10
    public function getCreationTimestamp(): int
11
    {
12
        return 1604502151;
13
    }
14
15
    public function update(Connection $connection): void
16
    {
17
        // get all themes where preview_media_id is invalid
18
        $themeIdsWithInvalidMediaId = $connection->executeQuery('
19
            SELECT `theme`.`id` FROM `theme`
20
            LEFT OUTER JOIN `media` ON `theme`.`preview_media_id` = `media`.`id`
21
            WHERE `media`.`id` IS null
22
        ')->fetchColumn();
23
24
        $connection->executeUpdate('
25
            UPDATE `theme` SET `theme`.`preview_media_id` = null
26
            WHERE `theme`.`id` IN (:theme_ids)
27
        ', ['theme_ids' => $themeIdsWithInvalidMediaId]);
28
29
        $connection->exec('
30
            ALTER TABLE `theme`
31
            ADD FOREIGN KEY `fk.theme.preview_media_id`(preview_media_id) REFERENCES media(id)
32
                ON UPDATE CASCADE
33
                ON DELETE SET NULL;
34
       ');
35
    }
36
37
    public function updateDestructive(Connection $connection): void
38
    {
39
        // nth
40
    }
41
}
42