Completed
Push — master ( 67a857...350314 )
by Gino
01:33
created

CreateRelatedSeriesTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 6 2
A down() 0 6 2
A dropRelation() 0 4 1
A createRelation() 0 15 1
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Updates;
4
5
use Schema;
6
use System\Classes\PluginManager;
7
use GinoPane\BlogTaxonomy\Models\Series;
8
use October\Rain\Database\Updates\Migration;
9
10
/**
11
 * Class CreateRelatedSeriesTable
12
 *
13
 * @package GinoPane\BlogTaxonomy\Updates
14
 */
15
class CreateRelatedSeriesTable extends Migration
16
{
17
    /**
18
     * Execute migrations
19
     */
20
    public function up()
21
    {
22
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
23
            $this->createRelation();
24
        }
25
    }
26
27
    /**
28
     * Rollback migrations
29
     */
30
    public function down()
31
    {
32
        if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
33
            $this->dropRelation();
34
        }
35
    }
36
37
    /**
38
     * Rollback relation migration
39
     */
40
    private function dropRelation()
41
    {
42
        Schema::dropIfExists(Series::RELATED_SERIES_TABLE_NAME);
43
    }
44
45
    /**
46
     * Create relation table
47
     */
48
    private function createRelation()
49
    {
50
        Schema::create(
51
            Series::RELATED_SERIES_TABLE_NAME,
52
            function ($table) {
53
                $table->engine = 'InnoDB';
54
55
                $table->integer('series_id')->unsigned();
56
                $table->integer('related_series_id')->unsigned();
57
                $table->index(['series_id', 'related_series_id']);
58
                $table->foreign('series_id', 'Series reference')->references('id')->on(Series::TABLE_NAME)->onDelete('cascade');
59
                $table->foreign('related_series_id', 'Related series reference')->references('id')->on(Series::TABLE_NAME)->onDelete('cascade');
60
            }
61
        );
62
    }
63
}
64