AbstractElementMetricMigration::safeDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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
     * The record class
23
     */
24
    const RECORD_CLASS = ElementMetric::class;
25
26
    /**
27
     * @return string
28
     */
29
    protected static function tableName(): string
30
    {
31
        /** @var ElementMetric $recordClass */
32
        $recordClass = static::RECORD_CLASS;
33
34
        return $recordClass::tableName();
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function safeUp()
41
    {
42
        $this->createTables();
43
        $this->addForeignKeys();
44
45
        return true;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function safeDown()
52
    {
53
        $this->dropTableIfExists(static::tableName());
54
55
        return true;
56
    }
57
58
    /**
59
     * Creates the table(s).
60
     *
61
     * @return void
62
     */
63
    protected function createTables()
64
    {
65
        $this->createTable(static::tableName(), $this->tableAttributes());
66
    }
67
68
    /**
69
     * The table attributes
70
     *
71
     * @return array
72
     */
73
    protected function tableAttributes(): array
74
    {
75
        return [
76
            'id' => $this->primaryKey(),
77
            'parentId' => $this->integer(),
78
            'elementId' => $this->integer()->notNull(),
79
            'class' => $this->string()->notNull(),
80
            'score' => $this->float(4),
81
            'weight' => $this->float(4)->notNull(),
82
            'version' => $this->string(50)->notNull(),
83
            'settings' => $this->text(),
84
            'dateCalculated' => $this->dateTime()->notNull(),
85
            'dateUpdated' => $this->dateTime()->notNull(),
86
            'dateCreated' => $this->dateTime()->notNull(),
87
            'uid' => $this->uid()
88
        ];
89
    }
90
91
    /**
92
     * Adds the foreign keys.
93
     *
94
     * @return void
95
     */
96
    protected function addForeignKeys()
97
    {
98
        $this->addForeignKey(
99
            $this->db->getForeignKeyName(
100
                static::tableName(),
101
                'elementId'
102
            ),
103
            static::tableName(),
104
            'elementId',
105
            ElementRecord::tableName(),
106
            'id',
107
            'CASCADE'
108
        );
109
110
        $this->addForeignKey(
111
            $this->db->getForeignKeyName(
112
                static::tableName(),
113
                'parentId'
114
            ),
115
            static::tableName(),
116
            'parentId',
117
            static::tableName(),
118
            'id',
119
            'CASCADE'
120
        );
121
    }
122
}
123