|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace inblank\activeuser\migrations; |
|
4
|
|
|
|
|
5
|
|
|
use yii; |
|
6
|
|
|
use yii\helpers\Console; |
|
7
|
|
|
|
|
8
|
|
|
class Migration extends \yii\db\Migration |
|
9
|
|
|
{ |
|
10
|
|
|
const TAB_USERS = 'users'; |
|
11
|
|
|
const TAB_PROFILES = 'profiles'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $tableOptions; |
|
17
|
|
|
|
|
18
|
|
|
protected $tableGroup = 'activeuser_'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritdoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function init() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::init(); |
|
26
|
|
|
|
|
27
|
|
|
switch (Yii::$app->db->driverName) { |
|
28
|
|
|
case 'mysql': |
|
29
|
|
|
$this->tableOptions = "ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 DEFAULT COLLATE=utf8_general_ci"; |
|
30
|
|
|
break; |
|
31
|
|
|
case 'pgsql': |
|
32
|
|
|
$this->tableOptions = null; |
|
33
|
|
|
break; |
|
34
|
|
|
default: |
|
35
|
|
|
throw new \RuntimeException('Your database is not supported!'); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function stderr($string) |
|
40
|
|
|
{ |
|
41
|
|
|
if (Console::streamSupportsAnsiColors(\STDOUT)) { |
|
42
|
|
|
$string = Console::ansiFormat(" Error: " . $string, [Console::FG_RED]); |
|
43
|
|
|
} |
|
44
|
|
|
return fwrite(\STDERR, $string); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Real table name builder |
|
49
|
|
|
* @param string $name table name |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function tn($name) |
|
53
|
|
|
{ |
|
54
|
|
|
return '{{%' . $this->tableGroup . $name . '}}'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Foreign key relation names generator |
|
59
|
|
|
* @param string $table1 first table in relation |
|
60
|
|
|
* @param string $table2 second table in relation |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function fk($table1, $table2) |
|
64
|
|
|
{ |
|
65
|
|
|
return 'fk__' . $this->tableGroup . $table1 . '__' . $table2; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Primary key names generator |
|
70
|
|
|
* @param string $table table name |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function pk($table) |
|
74
|
|
|
{ |
|
75
|
|
|
return 'pk_' . $this->tableGroup . $table; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|