Version20160820191203::down()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkMigrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Types\Types;
9
use Doctrine\Migrations\AbstractMigration;
10
11
/**
12
 * Auto-generated Migration: Please modify to your needs!
13
 */
14
class Version20160820191203 extends AbstractMigration
15
{
16
    public function up(Schema $schema): void
17
    {
18
        // Check if the tables already exist
19
        $tables = $schema->getTables();
20
        foreach ($tables as $table) {
21
            if ($table->getName() === 'tags') {
22
                return;
23
            }
24
        }
25
26
        $this->createTagsTable($schema);
27
        $this->createShortUrlsInTagsTable($schema);
28
    }
29
30
    private function createTagsTable(Schema $schema): void
31
    {
32
        $table = $schema->createTable('tags');
33
        $table->addColumn('id', Types::BIGINT, [
34
            'unsigned' => true,
35
            'autoincrement' => true,
36
            'notnull' => true,
37
        ]);
38
        $table->addColumn('name', Types::STRING, [
39
            'length' => 255,
40
            'notnull' => true,
41
        ]);
42
        $table->addUniqueIndex(['name']);
43
44
        $table->setPrimaryKey(['id']);
45
    }
46
47
    private function createShortUrlsInTagsTable(Schema $schema): void
48
    {
49
        $table = $schema->createTable('short_urls_in_tags');
50
        $table->addColumn('short_url_id', Types::BIGINT, [
51
            'unsigned' => true,
52
            'notnull' => true,
53
        ]);
54
        $table->addColumn('tag_id', Types::BIGINT, [
55
            'unsigned' => true,
56
            'notnull' => true,
57
        ]);
58
59
        $table->addForeignKeyConstraint('tags', ['tag_id'], ['id'], [
60
            'onDelete' => 'CASCADE',
61
            'onUpdate' => 'RESTRICT',
62
        ]);
63
        $table->addForeignKeyConstraint('short_urls', ['short_url_id'], ['id'], [
64
            'onDelete' => 'CASCADE',
65
            'onUpdate' => 'RESTRICT',
66
        ]);
67
68
        $table->setPrimaryKey(['short_url_id', 'tag_id']);
69
    }
70
71
    public function down(Schema $schema): void
72
    {
73
        $schema->dropTable('short_urls_in_tags');
74
        $schema->dropTable('tags');
75
    }
76
}
77