Completed
Push — master ( eac2a3...9c0950 )
by Vladimir
02:16
created

m171119_140800_create_email_template_entities   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 88
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeDown() 0 10 1
A safeUp() 0 57 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017 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 Vladimir Kuprienko <[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
        if ($this->db->driverName === 'mysql') {
37
            /* @link http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci */
38
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
39
        }
40
41
        // Tables
42
        $this->createTable(
43
            $this->primaryTableName,
44
            [
45
                'id'    => $this->primaryKey()->unsigned(),
46
                'key'   => $this->string()->notNull()->unique(),
47
            ],
48
            $tableOptions
49
        );
50
        $this->createTable(
51
            $this->translationTableName,
52
            [
53
                'id'            => $this->primaryKey()->unsigned(),
54
                'templateId'    => $this->integer()->unsigned()->notNull(),
55
                'language'      => $this->string(16)->notNull(),
56
                'subject'       => $this->string()->notNull(),
57
                'body'          => $this->text()->notNull(),
58
                'hint'          => $this->string(500),
59
            ],
60
            $tableOptions
61
        );
62
63
        // Indexes
64
        $this->createIndex(
65
            'idx-email_template-key',
66
            $this->primaryTableName,
67
            'key',
68
            true
69
        );
70
        $this->createIndex(
71
            'idx-email_template_translation-templateId',
72
            $this->primaryTableName,
73
            'templateId'
74
        );
75
        $this->createIndex(
76
            'idx-email_template_translation-language',
77
            $this->translationTableName,
78
            'language'
79
        );
80
81
        // Foreign keys
82
        $this->addForeignKey(
83
            'fk-email_template_translation-email_template',
84
            $this->primaryTableName,
85
            'templateId',
86
            $this->translationTableName,
87
            'id',
88
            'CASCADE',
89
            'CASCADE'
90
        );
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function safeDown()
97
    {
98
        $this->dropForeignKey('fk-email_template_translation-email_template', $this->primaryTableName);
99
100
        $this->dropIndex('idx-email_template_translation-language', $this->translationTableName);
101
        $this->dropIndex('idx-email_template_translation-templateId', $this->primaryTableName);
102
        $this->dropIndex('idx-email_template-key', $this->primaryTableName);
103
104
        $this->dropTable($this->translationTableName);
105
        $this->dropTable($this->primaryTableName);
106
    }
107
}
108