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