Completed
Push — master ( bc2149...df2af6 )
by Nate
42:38 queued 31:51
created

AbstractElementMetricMigration::safeUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/scorecard/license
6
 * @link       https://www.flipboxfactory.com/software/scorecard/
7
 */
8
9
namespace flipbox\scorecard\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Element as ElementRecord;
13
use flipbox\scorecard\records\ElementMetric;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
abstract class AbstractElementMetricMigration extends Migration
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function safeUp()
25
    {
26
        $this->createTables();
27
        $this->addForeignKeys();
28
29
        return true;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function safeDown()
36
    {
37
        $this->dropTableIfExists(ElementMetric::tableName());
38
39
        return true;
40
    }
41
42
    /**
43
     * Creates the table(s).
44
     *
45
     * @return void
46
     */
47
    protected function createTables()
48
    {
49
        $this->createTable(ElementMetric::tableName(), $this->tableAttributes());
50
    }
51
52
    /**
53
     * The table attributes
54
     *
55
     * @return array
56
     */
57
    protected function tableAttributes(): array
58
    {
59
        return [
60
            'id' => $this->primaryKey(),
61
            'parentId' => $this->integer(),
62
            'elementId' => $this->integer()->notNull(),
63
            'class' => $this->string()->notNull(),
64
            'score' => $this->float(4),
65
            'weight' => $this->float(4)->notNull(),
66
            'version' => $this->string(50)->notNull(),
67
            'settings' => $this->text(),
68
            'dateUpdated' => $this->dateTime()->notNull(),
69
            'dateCreated' => $this->dateTime()->notNull(),
70
            'uid' => $this->uid()
71
        ];
72
    }
73
74
    /**
75
     * Adds the foreign keys.
76
     *
77
     * @return void
78
     */
79
    protected function addForeignKeys()
80
    {
81
        $this->addForeignKey(
82
            $this->db->getForeignKeyName(
83
                ElementMetric::tableName(),
84
                'elementId'
85
            ),
86
            ElementMetric::tableName(),
87
            'elementId',
88
            ElementRecord::tableName(),
89
            'id',
90
            'CASCADE'
91
        );
92
93
        $this->addForeignKey(
94
            $this->db->getForeignKeyName(
95
                ElementMetric::tableName(),
96
                'parentId'
97
            ),
98
            ElementMetric::tableName(),
99
            'parentId',
100
            ElementMetric::tableName(),
101
            'id',
102
            'CASCADE'
103
        );
104
    }
105
}
106