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