Passed
Pull Request — master (#500)
by Alejandro
06:23
created

Version20190930165521::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 19
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 27
rs 9.6333
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkMigrations;
5
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Schema\SchemaException;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\Migrations\AbstractMigration;
10
11
final class Version20190930165521 extends AbstractMigration
12
{
13
    /**
14
     * @throws SchemaException
15
     */
16
    public function up(Schema $schema): void
17
    {
18
        $shortUrls = $schema->getTable('short_urls');
19
        if ($shortUrls->hasColumn('domain_id')) {
20
            return;
21
        }
22
23
        $domains = $schema->createTable('domains');
24
        $domains->addColumn('id', Type::BIGINT, [
25
            'unsigned' => true,
26
            'autoincrement' => true,
27
            'notnull' => true,
28
        ]);
29
        $domains->addColumn('authority', Type::STRING, [
30
            'length' => 512,
31
            'notnull' => true,
32
        ]);
33
        $domains->addUniqueIndex(['authority']);
34
        $domains->setPrimaryKey(['id']);
35
36
        $shortUrls->addColumn('domain_id', Type::BIGINT, [
37
            'unsigned' => true,
38
            'notnull' => false,
39
        ]);
40
        $shortUrls->addForeignKeyConstraint('domains', ['domain_id'], ['id'], [
41
            'onDelete' => 'RESTRICT',
42
            'onUpdate' => 'RESTRICT',
43
        ]);
44
    }
45
46
    /**
47
     * @throws SchemaException
48
     */
49
    public function down(Schema $schema): void
50
    {
51
        $schema->getTable('short_urls')->dropColumn('domain_id');
52
        $schema->dropTable('domains');
53
    }
54
}
55