Version20191001201532   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 18 4
A down() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkMigrations;
6
7
use Doctrine\DBAL\Schema\Index;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\SchemaException;
10
use Doctrine\Migrations\AbstractMigration;
11
12
use function array_reduce;
13
14
final class Version20191001201532 extends AbstractMigration
15
{
16
    /**
17
     * @throws SchemaException
18
     */
19
    public function up(Schema $schema): void
20
    {
21
        $shortUrls = $schema->getTable('short_urls');
22
        if ($shortUrls->hasIndex('unique_short_code_plus_domain')) {
23
            return;
24
        }
25
26
        /** @var Index|null $shortCodesIndex */
27
        $shortCodesIndex = array_reduce($shortUrls->getIndexes(), function (?Index $found, Index $current) {
28
            [$column] = $current->getColumns();
29
            return $column === 'short_code' ? $current : $found;
30
        });
31
        if ($shortCodesIndex === null) {
32
            return;
33
        }
34
35
        $shortUrls->dropIndex($shortCodesIndex->getName());
36
        $shortUrls->addUniqueIndex(['short_code', 'domain_id'], 'unique_short_code_plus_domain');
37
    }
38
39
    /**
40
     * @throws SchemaException
41
     */
42
    public function down(Schema $schema): void
43
    {
44
        $shortUrls = $schema->getTable('short_urls');
45
46
        $shortUrls->dropIndex('unique_short_code_plus_domain');
47
        $shortUrls->addUniqueIndex(['short_code']);
48
    }
49
}
50