Completed
Push — master ( b25c9d...3773b3 )
by Nate
23:52 queued 08:54
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\craft\scorecard\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Element as ElementRecord;
13
use flipbox\craft\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
            'dateCalculated' => $this->dateTime()->notNull(),
69
            'dateUpdated' => $this->dateTime()->notNull(),
70
            'dateCreated' => $this->dateTime()->notNull(),
71
            'uid' => $this->uid()
72
        ];
73
    }
74
75
    /**
76
     * Adds the foreign keys.
77
     *
78
     * @return void
79
     */
80
    protected function addForeignKeys()
81
    {
82
        $this->addForeignKey(
83
            $this->db->getForeignKeyName(
84
                ElementMetric::tableName(),
85
                'elementId'
86
            ),
87
            ElementMetric::tableName(),
88
            'elementId',
89
            ElementRecord::tableName(),
90
            'id',
91
            'CASCADE'
92
        );
93
94
        $this->addForeignKey(
95
            $this->db->getForeignKeyName(
96
                ElementMetric::tableName(),
97
                'parentId'
98
            ),
99
            ElementMetric::tableName(),
100
            'parentId',
101
            ElementMetric::tableName(),
102
            'id',
103
            'CASCADE'
104
        );
105
    }
106
}
107