Completed
Push — master ( a81ac8...05e307 )
by Alejandro
25s queued 13s
created

Version20191001201532   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

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