Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

Version20160820191203::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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