m171119_140800_create_email_template_entities   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 47
dl 0
loc 89
rs 10
c 3
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeDown() 0 10 1
A safeUp() 0 58 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017-2019 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\migrations;
9
10
use yii\db\Migration;
11
12
/**
13
 * Handles the creation of tables `email_template` and `email_template_translation`.
14
 *
15
 * @author Volodymyr Kupriienko <[email protected]>
16
 * @since 3.0
17
 */
18
class m171119_140800_create_email_template_entities extends Migration
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $primaryTableName = '{{%email_template}}';
24
    /**
25
     * @var string
26
     */
27
    protected $translationTableName = '{{%email_template_translation}}';
28
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function safeUp()
34
    {
35
        $tableOptions = null;
36
37
        if ('mysql' === $this->db->driverName) {
38
            /* @link http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci */
39
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
40
        }
41
42
        // Tables
43
        $this->createTable(
44
            $this->primaryTableName,
45
            [
46
                'id'    => $this->primaryKey()->unsigned(),
47
                'key'   => $this->string()->notNull()->unique(),
48
            ],
49
            $tableOptions
50
        );
51
        $this->createTable(
52
            $this->translationTableName,
53
            [
54
                'id'            => $this->primaryKey()->unsigned(),
55
                'templateId'    => $this->integer()->unsigned()->notNull(),
56
                'language'      => $this->string(16)->notNull(),
57
                'subject'       => $this->string()->notNull(),
58
                'body'          => $this->text()->notNull(),
59
                'hint'          => $this->string(500),
60
            ],
61
            $tableOptions
62
        );
63
64
        // Indexes
65
        $this->createIndex(
66
            'idx-email_template-key',
67
            $this->primaryTableName,
68
            'key',
69
            true
70
        );
71
        $this->createIndex(
72
            'idx-email_template_translation-templateId',
73
            $this->translationTableName,
74
            'templateId'
75
        );
76
        $this->createIndex(
77
            'idx-email_template_translation-language',
78
            $this->translationTableName,
79
            'language'
80
        );
81
82
        // Foreign keys
83
        $this->addForeignKey(
84
            'fk-email_template_translation-email_template',
85
            $this->translationTableName,
86
            'templateId',
87
            $this->primaryTableName,
88
            'id',
89
            'CASCADE',
90
            'CASCADE'
91
        );
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function safeDown()
98
    {
99
        $this->dropForeignKey('fk-email_template_translation-email_template', $this->translationTableName);
100
101
        $this->dropIndex('idx-email_template_translation-language', $this->translationTableName);
102
        $this->dropIndex('idx-email_template_translation-templateId', $this->translationTableName);
103
        $this->dropIndex('idx-email_template-key', $this->primaryTableName);
104
105
        $this->dropTable($this->translationTableName);
106
        $this->dropTable($this->primaryTableName);
107
    }
108
}
109