1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Component\Migration\Metro; |
6
|
|
|
|
7
|
|
|
use App\Component\Model\Orm\Orm; |
8
|
|
|
use App\Component\Db\Migration; |
9
|
|
|
use App\Model\Repository\Metro\Stations as StationsRepository; |
10
|
|
|
use Nymfonya\Component\Container; |
11
|
|
|
use SplFileObject; |
12
|
|
|
|
13
|
|
|
class Stations extends Migration |
14
|
|
|
{ |
15
|
|
|
const MIG_FIELDS = [ |
16
|
|
|
'id', 'lon', 'lat', 'name', 'h' |
17
|
|
|
]; |
18
|
|
|
const CSV_FIXTURE = '/../../../../../assets/model/metro/stations.csv'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* repository |
22
|
|
|
* |
23
|
|
|
* @var Orm |
24
|
|
|
*/ |
25
|
|
|
protected $repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* instanciate |
29
|
|
|
* |
30
|
|
|
* @param Container $container |
31
|
|
|
*/ |
32
|
8 |
|
public function __construct(Container $container) |
33
|
|
|
{ |
34
|
8 |
|
parent::__construct($container); |
35
|
8 |
|
$this->repository = new StationsRepository($container); |
36
|
8 |
|
$this->fromOrm($this->repository); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* check if migration can run |
41
|
|
|
* |
42
|
|
|
* @return boolean |
43
|
|
|
*/ |
44
|
1 |
|
protected function canMigrate(): bool |
45
|
|
|
{ |
46
|
1 |
|
return (!$this->tableExists()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* create table |
51
|
|
|
* |
52
|
|
|
* @return Migration |
53
|
|
|
*/ |
54
|
1 |
|
protected function runCreate(): Migration |
55
|
|
|
{ |
56
|
1 |
|
if (!$this->tableExists()) { |
57
|
1 |
|
$sql = sprintf( |
58
|
1 |
|
'CREATE TABLE `%s` ( |
59
|
|
|
`id` bigint(20) NOT NULL, |
60
|
|
|
`lon` double NOT NULL, |
61
|
|
|
`lat` double NOT NULL, |
62
|
|
|
`name` varchar(150) NOT NULL, |
63
|
|
|
`h` varchar(16) 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 |
|
$stream = new SplFileObject( |
81
|
1 |
|
__DIR__ . self::CSV_FIXTURE |
82
|
|
|
); |
83
|
1 |
|
$stream->setFlags( |
84
|
|
|
SplFileObject::READ_CSV | |
85
|
|
|
SplFileObject::SKIP_EMPTY | |
86
|
|
|
SplFileObject::READ_AHEAD | |
87
|
1 |
|
SplFileObject::DROP_NEW_LINE |
88
|
|
|
); |
89
|
1 |
|
while (false === $stream->eof()) { |
90
|
1 |
|
$csvData = $stream->fgetcsv(); |
91
|
1 |
|
if (!empty($csvData)) { |
92
|
1 |
|
$data = array_combine(self::MIG_FIELDS, $csvData); |
93
|
1 |
|
if (false !== $data) { |
94
|
1 |
|
$this->repository->resetBuilder()->insert($data); |
95
|
1 |
|
$this->run( |
96
|
1 |
|
$this->repository->getSql(), |
97
|
1 |
|
$this->repository->getBuilderValues() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
1 |
|
unset($csvData); |
102
|
|
|
} |
103
|
1 |
|
$stream = null; |
104
|
1 |
|
unset($stream); |
105
|
|
|
} |
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* index table |
111
|
|
|
* |
112
|
|
|
* @return Migration |
113
|
|
|
*/ |
114
|
1 |
|
protected function runIndex(): Migration |
115
|
|
|
{ |
116
|
1 |
|
$pkey = $this->repository->getPrimary(); |
117
|
1 |
|
$sqlIndex = sprintf( |
118
|
1 |
|
'ALTER TABLE `%s`' |
119
|
|
|
. 'ADD PRIMARY KEY (`%s`),' |
120
|
|
|
. 'ADD KEY `%s` (`%s`),' |
121
|
|
|
. 'ADD KEY `name` (`name`),' |
122
|
1 |
|
. 'ADD KEY `h` (`h`)', |
123
|
1 |
|
$this->repository->getTable(), |
124
|
|
|
$pkey, |
125
|
|
|
$pkey, |
126
|
|
|
$pkey |
127
|
|
|
); |
128
|
1 |
|
$this->run($sqlIndex); |
129
|
1 |
|
$sqlAutoinc = sprintf('ALTER TABLE `%s`' |
130
|
|
|
. 'MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, ' |
131
|
1 |
|
. 'AUTO_INCREMENT=1', $this->repository->getTable()); |
132
|
1 |
|
$this->run($sqlAutoinc); |
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* return true if table exists |
138
|
|
|
* |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
1 |
|
protected function tableExists(): bool |
142
|
|
|
{ |
143
|
1 |
|
$sqlWhere = " WHERE table_schema = '%s' AND table_name = '%s';"; |
144
|
1 |
|
$sql = sprintf( |
145
|
1 |
|
"SELECT count(*) as counter FROM %s " . $sqlWhere, |
146
|
1 |
|
'information_schema.tables', |
147
|
1 |
|
$this->repository->getDatabase(), |
148
|
1 |
|
$this->repository->getTable() |
149
|
|
|
); |
150
|
1 |
|
$this->run($sql)->hydrate(); |
151
|
1 |
|
$result = $this->getRowset()[0]; |
152
|
1 |
|
$counter = (int) $result['counter']; |
153
|
1 |
|
return ($counter > 0); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* drop table if exists |
158
|
|
|
* |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
2 |
|
protected function dropTable(): bool |
162
|
|
|
{ |
163
|
2 |
|
if (!$this->tableExists()) { |
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
1 |
|
$sql = 'DROP TABLE ' . $this->repository->getTable() . ';'; |
167
|
1 |
|
$this->run($sql); |
168
|
1 |
|
return true; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|