1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Component\Migration; |
4
|
|
|
|
5
|
|
|
use App\Component\Model\Orm\Orm; |
6
|
|
|
use App\Component\Db\Migration; |
7
|
|
|
use App\Model\Accounts; |
8
|
|
|
use App\Model\Repository\Users as UsersRepository; |
9
|
|
|
use Nymfonya\Component\Container; |
10
|
|
|
|
11
|
|
|
class Users extends Migration |
12
|
|
|
{ |
13
|
|
|
const MIG_FIELDS = [ |
14
|
|
|
'id', 'name', 'email', 'password', 'status', 'role' |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* repository |
19
|
|
|
* |
20
|
|
|
* @var Orm |
21
|
|
|
*/ |
22
|
|
|
protected $repository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* instanciate |
26
|
|
|
* |
27
|
|
|
* @param Container $container |
28
|
|
|
*/ |
29
|
9 |
|
public function __construct(Container $container) |
30
|
|
|
{ |
31
|
9 |
|
parent::__construct($container); |
32
|
9 |
|
$this->repository = new UsersRepository($container); |
33
|
9 |
|
$this->fromOrm($this->repository); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* check if migration can run |
38
|
|
|
* |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
1 |
|
protected function canMigrate(): bool |
42
|
|
|
{ |
43
|
1 |
|
return (!$this->tableExists()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* create table |
48
|
|
|
* |
49
|
|
|
* @return Migration |
50
|
|
|
*/ |
51
|
1 |
|
protected function runCreate(): Migration |
52
|
|
|
{ |
53
|
1 |
|
if (!$this->tableExists()) { |
54
|
1 |
|
$sql = sprintf( |
55
|
1 |
|
"CREATE TABLE `%s` ( |
56
|
|
|
`id` bigint(20) NOT NULL , |
57
|
|
|
`name` varchar(255) NOT NULL, |
58
|
|
|
`email` varchar(255) NOT NULL, |
59
|
|
|
`password` varchar(255) NOT NULL, |
60
|
|
|
`status` varchar(255) NOT NULL, |
61
|
|
|
`role` varchar(255) NOT NULL |
62
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8", |
63
|
1 |
|
$this->repository->getTable() |
64
|
|
|
); |
65
|
1 |
|
$this->run($sql); |
66
|
|
|
} |
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* insert into table |
72
|
|
|
* |
73
|
|
|
* @return Migration |
74
|
|
|
*/ |
75
|
1 |
|
protected function runInsert(): Migration |
76
|
|
|
{ |
77
|
1 |
|
if ($this->tableExists()) { |
78
|
1 |
|
$insertDatas = $this->getInsertDatas(); |
79
|
1 |
|
foreach ($insertDatas as $data) { |
80
|
1 |
|
$this->repository->resetBuilder(); |
81
|
1 |
|
$this->repository->insert($data); |
82
|
1 |
|
$this->run( |
83
|
1 |
|
$this->repository->getSql(), |
84
|
1 |
|
$this->repository->getBuilderValues() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* index table |
93
|
|
|
* |
94
|
|
|
* @return Migration |
95
|
|
|
*/ |
96
|
1 |
|
protected function runIndex(): Migration |
97
|
|
|
{ |
98
|
1 |
|
$pkey = $this->repository->getPrimary(); |
99
|
1 |
|
$sqlIndex = sprintf( |
100
|
|
|
'ALTER TABLE `%s`' |
101
|
|
|
. 'ADD PRIMARY KEY (`%s`),' |
102
|
|
|
. 'ADD KEY `%s` (`%s`),' |
103
|
1 |
|
. 'ADD KEY `email` (`email`)', |
104
|
1 |
|
$this->repository->getTable(), |
105
|
1 |
|
$pkey, |
106
|
1 |
|
$pkey, |
107
|
1 |
|
$pkey |
108
|
|
|
); |
109
|
1 |
|
$this->run($sqlIndex); |
110
|
|
|
$sqlAutoinc = 'ALTER TABLE `users`' |
111
|
1 |
|
. 'MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1'; |
112
|
1 |
|
$this->run($sqlAutoinc); |
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* prepare to insert datas as array |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
1 |
|
protected function getInsertDatas(): array |
122
|
|
|
{ |
123
|
1 |
|
$csvArray = (new Accounts($this->container))->toArray(); |
124
|
1 |
|
$fields = self::MIG_FIELDS; |
125
|
1 |
|
$insertDatas = array_map(function ($values) use ($fields) { |
126
|
1 |
|
return array_combine($fields, $values); |
127
|
1 |
|
}, $csvArray); |
128
|
1 |
|
return $insertDatas; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* return true if table exists |
133
|
|
|
* |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
1 |
|
protected function tableExists(): bool |
137
|
|
|
{ |
138
|
1 |
|
$sqlWhere = " WHERE table_schema = '%s' AND table_name = '%s';"; |
139
|
1 |
|
$sql = sprintf( |
140
|
1 |
|
"SELECT count(*) as counter FROM %s " . $sqlWhere, |
141
|
1 |
|
'information_schema.tables', |
142
|
1 |
|
$this->repository->getDatabase(), |
143
|
1 |
|
$this->repository->getTable() |
144
|
|
|
); |
145
|
1 |
|
$this->run($sql)->hydrate(); |
146
|
1 |
|
$result = $this->getRowset()[0]; |
147
|
1 |
|
$counter = (int) $result['counter']; |
148
|
1 |
|
return ($counter > 0); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* drop table if exists |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
2 |
|
protected function dropTable(): bool |
157
|
|
|
{ |
158
|
2 |
|
if (!$this->tableExists()) { |
159
|
1 |
|
return false; |
160
|
|
|
} |
161
|
1 |
|
$sql = 'DROP TABLE ' . $this->repository->getTable() . ';'; |
162
|
1 |
|
$this->run($sql); |
163
|
1 |
|
return true; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|