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

getCreationTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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