forked from callmez/yii2-wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleMigration.php
117 lines (97 loc) · 2.62 KB
/
ModuleMigration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace callmez\wechat\components;
use Yii;
use yii\db\Migration;
/**
* 微信扩展模块迁移基类
* Class ModuleMigration
* @package callmez\wechat\components
*/
class ModuleMigration extends Migration
{
/**
* @var callmez\wechat\models\Module
*/
public $module;
final public function up()
{
$transaction = $this->db->beginTransaction();
try {
if ($this->install() === false) {
$transaction->rollBack();
return false;
}
$transaction->commit();
} catch (\Exception $e) {
Yii::warning("Exception: " . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")", __METHOD__);
$transaction->rollBack();
return false;
}
return null;
}
final public function safeDown()
{
$transaction = $this->db->beginTransaction();
try {
if ($this->uninstall() === false) {
$transaction->rollBack();
return false;
}
$transaction->commit();
} catch (\Exception $e) {
Yii::warning("Exception: " . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")", __METHOD__);
$transaction->rollBack();
return false;
}
return null;
}
/**
* 升级到指定版本
*/
final public function to($fromVersion, $toVersion)
{
$transaction = $this->db->beginTransaction();
try {
if ($this->upgrade($fromVersion, $toVersion) === false) {
$transaction->rollBack();
return false;
}
$transaction->commit();
} catch (\Exception $e) {
Yii::warning("Exception: " . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")", __METHOD__);
$transaction->rollBack();
return false;
}
return null;
}
/**
* 扩展模块被安装时会执行此函数
* @return mixed
*/
public function install()
{
}
/**
* 扩展模块被卸载时会执行此函数
* @return mixed
*/
public function uninstall()
{
}
/**
* 扩展模块被升级时会执行此函数
* @return mixed
*/
public function ungrade($fromVersion, $toVersion)
{
}
/**
* 判断表是否存在(预防表数据安装失败)
* @param $tableName
* @return bool
*/
public function tableExists($tableName)
{
return $this->db->schema->getTableSchema($tableName, true) !== null;
}
}