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

Version20191001201532::up()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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