@@ -33,39 +33,39 @@ |
||
33 | 33 | */ |
34 | 34 | class ResultAdapter implements IResult { |
35 | 35 | |
36 | - /** @var Result */ |
|
37 | - private $inner; |
|
36 | + /** @var Result */ |
|
37 | + private $inner; |
|
38 | 38 | |
39 | - public function __construct(Result $inner) { |
|
40 | - $this->inner = $inner; |
|
41 | - } |
|
39 | + public function __construct(Result $inner) { |
|
40 | + $this->inner = $inner; |
|
41 | + } |
|
42 | 42 | |
43 | - public function closeCursor(): bool { |
|
44 | - $this->inner->free(); |
|
43 | + public function closeCursor(): bool { |
|
44 | + $this->inner->free(); |
|
45 | 45 | |
46 | - return true; |
|
47 | - } |
|
46 | + return true; |
|
47 | + } |
|
48 | 48 | |
49 | - public function fetch(int $fetchMode = PDO::FETCH_ASSOC) { |
|
50 | - return $this->inner->fetch($fetchMode); |
|
51 | - } |
|
49 | + public function fetch(int $fetchMode = PDO::FETCH_ASSOC) { |
|
50 | + return $this->inner->fetch($fetchMode); |
|
51 | + } |
|
52 | 52 | |
53 | - public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array { |
|
54 | - if ($fetchMode !== PDO::FETCH_ASSOC && $fetchMode !== PDO::FETCH_NUM && $fetchMode !== PDO::FETCH_COLUMN) { |
|
55 | - throw new \Exception('Fetch mode needs to be assoc, num or column.'); |
|
56 | - } |
|
57 | - return $this->inner->fetchAll($fetchMode); |
|
58 | - } |
|
53 | + public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array { |
|
54 | + if ($fetchMode !== PDO::FETCH_ASSOC && $fetchMode !== PDO::FETCH_NUM && $fetchMode !== PDO::FETCH_COLUMN) { |
|
55 | + throw new \Exception('Fetch mode needs to be assoc, num or column.'); |
|
56 | + } |
|
57 | + return $this->inner->fetchAll($fetchMode); |
|
58 | + } |
|
59 | 59 | |
60 | - public function fetchColumn($columnIndex = 0) { |
|
61 | - return $this->inner->fetchOne(); |
|
62 | - } |
|
60 | + public function fetchColumn($columnIndex = 0) { |
|
61 | + return $this->inner->fetchOne(); |
|
62 | + } |
|
63 | 63 | |
64 | - public function fetchOne() { |
|
65 | - return $this->inner->fetchOne(); |
|
66 | - } |
|
64 | + public function fetchOne() { |
|
65 | + return $this->inner->fetchOne(); |
|
66 | + } |
|
67 | 67 | |
68 | - public function rowCount(): int { |
|
69 | - return $this->inner->rowCount(); |
|
70 | - } |
|
68 | + public function rowCount(): int { |
|
69 | + return $this->inner->rowCount(); |
|
70 | + } |
|
71 | 71 | } |
@@ -32,26 +32,26 @@ |
||
32 | 32 | |
33 | 33 | class SQLiteMigrator extends Migrator { |
34 | 34 | |
35 | - /** |
|
36 | - * @param Schema $targetSchema |
|
37 | - * @param \Doctrine\DBAL\Connection $connection |
|
38 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
39 | - */ |
|
40 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
41 | - $platform = $connection->getDatabasePlatform(); |
|
42 | - $platform->registerDoctrineTypeMapping('tinyint unsigned', 'integer'); |
|
43 | - $platform->registerDoctrineTypeMapping('smallint unsigned', 'integer'); |
|
44 | - $platform->registerDoctrineTypeMapping('varchar ', 'string'); |
|
35 | + /** |
|
36 | + * @param Schema $targetSchema |
|
37 | + * @param \Doctrine\DBAL\Connection $connection |
|
38 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
39 | + */ |
|
40 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
41 | + $platform = $connection->getDatabasePlatform(); |
|
42 | + $platform->registerDoctrineTypeMapping('tinyint unsigned', 'integer'); |
|
43 | + $platform->registerDoctrineTypeMapping('smallint unsigned', 'integer'); |
|
44 | + $platform->registerDoctrineTypeMapping('varchar ', 'string'); |
|
45 | 45 | |
46 | - // with sqlite autoincrement columns is of type integer |
|
47 | - foreach ($targetSchema->getTables() as $table) { |
|
48 | - foreach ($table->getColumns() as $column) { |
|
49 | - if ($column->getType() instanceof BigIntType && $column->getAutoincrement()) { |
|
50 | - $column->setType(Type::getType('integer')); |
|
51 | - } |
|
52 | - } |
|
53 | - } |
|
46 | + // with sqlite autoincrement columns is of type integer |
|
47 | + foreach ($targetSchema->getTables() as $table) { |
|
48 | + foreach ($table->getColumns() as $column) { |
|
49 | + if ($column->getType() instanceof BigIntType && $column->getAutoincrement()) { |
|
50 | + $column->setType(Type::getType('integer')); |
|
51 | + } |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - return parent::getDiff($targetSchema, $connection); |
|
56 | - } |
|
55 | + return parent::getDiff($targetSchema, $connection); |
|
56 | + } |
|
57 | 57 | } |
@@ -47,583 +47,583 @@ |
||
47 | 47 | |
48 | 48 | class MigrationService { |
49 | 49 | |
50 | - /** @var boolean */ |
|
51 | - private $migrationTableCreated; |
|
52 | - /** @var array */ |
|
53 | - private $migrations; |
|
54 | - /** @var IOutput */ |
|
55 | - private $output; |
|
56 | - /** @var Connection */ |
|
57 | - private $connection; |
|
58 | - /** @var string */ |
|
59 | - private $appName; |
|
60 | - /** @var bool */ |
|
61 | - private $checkOracle; |
|
62 | - |
|
63 | - /** |
|
64 | - * MigrationService constructor. |
|
65 | - * |
|
66 | - * @param $appName |
|
67 | - * @param Connection $connection |
|
68 | - * @param AppLocator $appLocator |
|
69 | - * @param IOutput|null $output |
|
70 | - * @throws \Exception |
|
71 | - */ |
|
72 | - public function __construct($appName, Connection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
73 | - $this->appName = $appName; |
|
74 | - $this->connection = $connection; |
|
75 | - $this->output = $output; |
|
76 | - if (null === $this->output) { |
|
77 | - $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
78 | - } |
|
79 | - |
|
80 | - if ($appName === 'core') { |
|
81 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
82 | - $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
83 | - $this->checkOracle = true; |
|
84 | - } else { |
|
85 | - if (null === $appLocator) { |
|
86 | - $appLocator = new AppLocator(); |
|
87 | - } |
|
88 | - $appPath = $appLocator->getAppPath($appName); |
|
89 | - $namespace = App::buildAppNamespace($appName); |
|
90 | - $this->migrationsPath = "$appPath/lib/Migration"; |
|
91 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
92 | - |
|
93 | - $infoParser = new InfoParser(); |
|
94 | - $info = $infoParser->parse($appPath . '/appinfo/info.xml'); |
|
95 | - if (!isset($info['dependencies']['database'])) { |
|
96 | - $this->checkOracle = true; |
|
97 | - } else { |
|
98 | - $this->checkOracle = false; |
|
99 | - foreach ($info['dependencies']['database'] as $database) { |
|
100 | - if (\is_string($database) && $database === 'oci') { |
|
101 | - $this->checkOracle = true; |
|
102 | - } elseif (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') { |
|
103 | - $this->checkOracle = true; |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns the name of the app for which this migration is executed |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function getApp() { |
|
116 | - return $this->appName; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @return bool |
|
121 | - * @codeCoverageIgnore - this will implicitly tested on installation |
|
122 | - */ |
|
123 | - private function createMigrationTable() { |
|
124 | - if ($this->migrationTableCreated) { |
|
125 | - return false; |
|
126 | - } |
|
127 | - |
|
128 | - if ($this->connection->tableExists('migrations') && \OC::$server->getConfig()->getAppValue('core', 'vendor', '') !== 'owncloud') { |
|
129 | - $this->migrationTableCreated = true; |
|
130 | - return false; |
|
131 | - } |
|
132 | - |
|
133 | - $schema = new SchemaWrapper($this->connection); |
|
134 | - |
|
135 | - /** |
|
136 | - * We drop the table when it has different columns or the definition does not |
|
137 | - * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
138 | - */ |
|
139 | - try { |
|
140 | - $table = $schema->getTable('migrations'); |
|
141 | - $columns = $table->getColumns(); |
|
142 | - |
|
143 | - if (count($columns) === 2) { |
|
144 | - try { |
|
145 | - $column = $table->getColumn('app'); |
|
146 | - $schemaMismatch = $column->getLength() !== 255; |
|
147 | - |
|
148 | - if (!$schemaMismatch) { |
|
149 | - $column = $table->getColumn('version'); |
|
150 | - $schemaMismatch = $column->getLength() !== 255; |
|
151 | - } |
|
152 | - } catch (SchemaException $e) { |
|
153 | - // One of the columns is missing |
|
154 | - $schemaMismatch = true; |
|
155 | - } |
|
156 | - |
|
157 | - if (!$schemaMismatch) { |
|
158 | - // Table exists and schema matches: return back! |
|
159 | - $this->migrationTableCreated = true; |
|
160 | - return false; |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - // Drop the table, when it didn't match our expectations. |
|
165 | - $this->connection->dropTable('migrations'); |
|
166 | - |
|
167 | - // Recreate the schema after the table was dropped. |
|
168 | - $schema = new SchemaWrapper($this->connection); |
|
169 | - } catch (SchemaException $e) { |
|
170 | - // Table not found, no need to panic, we will create it. |
|
171 | - } |
|
172 | - |
|
173 | - $table = $schema->createTable('migrations'); |
|
174 | - $table->addColumn('app', Types::STRING, ['length' => 255]); |
|
175 | - $table->addColumn('version', Types::STRING, ['length' => 255]); |
|
176 | - $table->setPrimaryKey(['app', 'version']); |
|
177 | - |
|
178 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
179 | - |
|
180 | - $this->migrationTableCreated = true; |
|
181 | - |
|
182 | - return true; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Returns all versions which have already been applied |
|
187 | - * |
|
188 | - * @return string[] |
|
189 | - * @codeCoverageIgnore - no need to test this |
|
190 | - */ |
|
191 | - public function getMigratedVersions() { |
|
192 | - $this->createMigrationTable(); |
|
193 | - $qb = $this->connection->getQueryBuilder(); |
|
194 | - |
|
195 | - $qb->select('version') |
|
196 | - ->from('migrations') |
|
197 | - ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
198 | - ->orderBy('version'); |
|
199 | - |
|
200 | - $result = $qb->execute(); |
|
201 | - $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
202 | - $result->closeCursor(); |
|
203 | - |
|
204 | - return $rows; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Returns all versions which are available in the migration folder |
|
209 | - * |
|
210 | - * @return array |
|
211 | - */ |
|
212 | - public function getAvailableVersions() { |
|
213 | - $this->ensureMigrationsAreLoaded(); |
|
214 | - return array_map('strval', array_keys($this->migrations)); |
|
215 | - } |
|
216 | - |
|
217 | - protected function findMigrations() { |
|
218 | - $directory = realpath($this->migrationsPath); |
|
219 | - if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
220 | - return []; |
|
221 | - } |
|
222 | - |
|
223 | - $iterator = new \RegexIterator( |
|
224 | - new \RecursiveIteratorIterator( |
|
225 | - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
226 | - \RecursiveIteratorIterator::LEAVES_ONLY |
|
227 | - ), |
|
228 | - '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
229 | - \RegexIterator::GET_MATCH); |
|
230 | - |
|
231 | - $files = array_keys(iterator_to_array($iterator)); |
|
232 | - uasort($files, function ($a, $b) { |
|
233 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
234 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
235 | - if (!empty($matchA) && !empty($matchB)) { |
|
236 | - if ($matchA[1] !== $matchB[1]) { |
|
237 | - return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
238 | - } |
|
239 | - return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
240 | - } |
|
241 | - return (basename($a) < basename($b)) ? -1 : 1; |
|
242 | - }); |
|
243 | - |
|
244 | - $migrations = []; |
|
245 | - |
|
246 | - foreach ($files as $file) { |
|
247 | - $className = basename($file, '.php'); |
|
248 | - $version = (string) substr($className, 7); |
|
249 | - if ($version === '0') { |
|
250 | - throw new \InvalidArgumentException( |
|
251 | - "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
252 | - ); |
|
253 | - } |
|
254 | - $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
255 | - } |
|
256 | - |
|
257 | - return $migrations; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * @param string $to |
|
262 | - * @return string[] |
|
263 | - */ |
|
264 | - private function getMigrationsToExecute($to) { |
|
265 | - $knownMigrations = $this->getMigratedVersions(); |
|
266 | - $availableMigrations = $this->getAvailableVersions(); |
|
267 | - |
|
268 | - $toBeExecuted = []; |
|
269 | - foreach ($availableMigrations as $v) { |
|
270 | - if ($to !== 'latest' && $v > $to) { |
|
271 | - continue; |
|
272 | - } |
|
273 | - if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
274 | - $toBeExecuted[] = $v; |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - return $toBeExecuted; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param string $m |
|
283 | - * @param string[] $knownMigrations |
|
284 | - * @return bool |
|
285 | - */ |
|
286 | - private function shallBeExecuted($m, $knownMigrations) { |
|
287 | - if (in_array($m, $knownMigrations)) { |
|
288 | - return false; |
|
289 | - } |
|
290 | - |
|
291 | - return true; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * @param string $version |
|
296 | - */ |
|
297 | - private function markAsExecuted($version) { |
|
298 | - $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
299 | - 'app' => $this->appName, |
|
300 | - 'version' => $version |
|
301 | - ]); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Returns the name of the table which holds the already applied versions |
|
306 | - * |
|
307 | - * @return string |
|
308 | - */ |
|
309 | - public function getMigrationsTableName() { |
|
310 | - return $this->connection->getPrefix() . 'migrations'; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Returns the namespace of the version classes |
|
315 | - * |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - public function getMigrationsNamespace() { |
|
319 | - return $this->migrationsNamespace; |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Returns the directory which holds the versions |
|
324 | - * |
|
325 | - * @return string |
|
326 | - */ |
|
327 | - public function getMigrationsDirectory() { |
|
328 | - return $this->migrationsPath; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Return the explicit version for the aliases; current, next, prev, latest |
|
333 | - * |
|
334 | - * @param string $alias |
|
335 | - * @return mixed|null|string |
|
336 | - */ |
|
337 | - public function getMigration($alias) { |
|
338 | - switch ($alias) { |
|
339 | - case 'current': |
|
340 | - return $this->getCurrentVersion(); |
|
341 | - case 'next': |
|
342 | - return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
343 | - case 'prev': |
|
344 | - return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
345 | - case 'latest': |
|
346 | - $this->ensureMigrationsAreLoaded(); |
|
347 | - |
|
348 | - $migrations = $this->getAvailableVersions(); |
|
349 | - return @end($migrations); |
|
350 | - } |
|
351 | - return '0'; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * @param string $version |
|
356 | - * @param int $delta |
|
357 | - * @return null|string |
|
358 | - */ |
|
359 | - private function getRelativeVersion($version, $delta) { |
|
360 | - $this->ensureMigrationsAreLoaded(); |
|
361 | - |
|
362 | - $versions = $this->getAvailableVersions(); |
|
363 | - array_unshift($versions, 0); |
|
364 | - $offset = array_search($version, $versions, true); |
|
365 | - if ($offset === false || !isset($versions[$offset + $delta])) { |
|
366 | - // Unknown version or delta out of bounds. |
|
367 | - return null; |
|
368 | - } |
|
369 | - |
|
370 | - return (string) $versions[$offset + $delta]; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * @return string |
|
375 | - */ |
|
376 | - private function getCurrentVersion() { |
|
377 | - $m = $this->getMigratedVersions(); |
|
378 | - if (count($m) === 0) { |
|
379 | - return '0'; |
|
380 | - } |
|
381 | - $migrations = array_values($m); |
|
382 | - return @end($migrations); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * @param string $version |
|
387 | - * @return string |
|
388 | - * @throws \InvalidArgumentException |
|
389 | - */ |
|
390 | - private function getClass($version) { |
|
391 | - $this->ensureMigrationsAreLoaded(); |
|
392 | - |
|
393 | - if (isset($this->migrations[$version])) { |
|
394 | - return $this->migrations[$version]; |
|
395 | - } |
|
396 | - |
|
397 | - throw new \InvalidArgumentException("Version $version is unknown."); |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Allows to set an IOutput implementation which is used for logging progress and messages |
|
402 | - * |
|
403 | - * @param IOutput $output |
|
404 | - */ |
|
405 | - public function setOutput(IOutput $output) { |
|
406 | - $this->output = $output; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Applies all not yet applied versions up to $to |
|
411 | - * |
|
412 | - * @param string $to |
|
413 | - * @param bool $schemaOnly |
|
414 | - * @throws \InvalidArgumentException |
|
415 | - */ |
|
416 | - public function migrate($to = 'latest', $schemaOnly = false) { |
|
417 | - if ($schemaOnly) { |
|
418 | - $this->migrateSchemaOnly($to); |
|
419 | - return; |
|
420 | - } |
|
421 | - |
|
422 | - // read known migrations |
|
423 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
424 | - foreach ($toBeExecuted as $version) { |
|
425 | - try { |
|
426 | - $this->executeStep($version, $schemaOnly); |
|
427 | - } catch (DriverException $e) { |
|
428 | - // The exception itself does not contain the name of the migration, |
|
429 | - // so we wrap it here, to make debugging easier. |
|
430 | - throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); |
|
431 | - } |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Applies all not yet applied versions up to $to |
|
437 | - * |
|
438 | - * @param string $to |
|
439 | - * @throws \InvalidArgumentException |
|
440 | - */ |
|
441 | - public function migrateSchemaOnly($to = 'latest') { |
|
442 | - // read known migrations |
|
443 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
444 | - |
|
445 | - if (empty($toBeExecuted)) { |
|
446 | - return; |
|
447 | - } |
|
448 | - |
|
449 | - $toSchema = null; |
|
450 | - foreach ($toBeExecuted as $version) { |
|
451 | - $instance = $this->createInstance($version); |
|
452 | - |
|
453 | - $toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { |
|
454 | - return $toSchema ?: new SchemaWrapper($this->connection); |
|
455 | - }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema; |
|
456 | - |
|
457 | - $this->markAsExecuted($version); |
|
458 | - } |
|
459 | - |
|
460 | - if ($toSchema instanceof SchemaWrapper) { |
|
461 | - $targetSchema = $toSchema->getWrappedSchema(); |
|
462 | - if ($this->checkOracle) { |
|
463 | - $beforeSchema = $this->connection->createSchema(); |
|
464 | - $this->ensureOracleIdentifierLengthLimit($beforeSchema, $targetSchema, strlen($this->connection->getPrefix())); |
|
465 | - } |
|
466 | - $this->connection->migrateToSchema($targetSchema); |
|
467 | - $toSchema->performDropTableCalls(); |
|
468 | - } |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Get the human readable descriptions for the migration steps to run |
|
473 | - * |
|
474 | - * @param string $to |
|
475 | - * @return string[] [$name => $description] |
|
476 | - */ |
|
477 | - public function describeMigrationStep($to = 'latest') { |
|
478 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
479 | - $description = []; |
|
480 | - foreach ($toBeExecuted as $version) { |
|
481 | - $migration = $this->createInstance($version); |
|
482 | - if ($migration->name()) { |
|
483 | - $description[$migration->name()] = $migration->description(); |
|
484 | - } |
|
485 | - } |
|
486 | - return $description; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * @param string $version |
|
491 | - * @return IMigrationStep |
|
492 | - * @throws \InvalidArgumentException |
|
493 | - */ |
|
494 | - protected function createInstance($version) { |
|
495 | - $class = $this->getClass($version); |
|
496 | - try { |
|
497 | - $s = \OC::$server->query($class); |
|
498 | - |
|
499 | - if (!$s instanceof IMigrationStep) { |
|
500 | - throw new \InvalidArgumentException('Not a valid migration'); |
|
501 | - } |
|
502 | - } catch (QueryException $e) { |
|
503 | - if (class_exists($class)) { |
|
504 | - $s = new $class(); |
|
505 | - } else { |
|
506 | - throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
507 | - } |
|
508 | - } |
|
509 | - |
|
510 | - return $s; |
|
511 | - } |
|
512 | - |
|
513 | - /** |
|
514 | - * Executes one explicit version |
|
515 | - * |
|
516 | - * @param string $version |
|
517 | - * @param bool $schemaOnly |
|
518 | - * @throws \InvalidArgumentException |
|
519 | - */ |
|
520 | - public function executeStep($version, $schemaOnly = false) { |
|
521 | - $instance = $this->createInstance($version); |
|
522 | - |
|
523 | - if (!$schemaOnly) { |
|
524 | - $instance->preSchemaChange($this->output, function () { |
|
525 | - return new SchemaWrapper($this->connection); |
|
526 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
527 | - } |
|
528 | - |
|
529 | - $toSchema = $instance->changeSchema($this->output, function () { |
|
530 | - return new SchemaWrapper($this->connection); |
|
531 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
532 | - |
|
533 | - if ($toSchema instanceof SchemaWrapper) { |
|
534 | - $targetSchema = $toSchema->getWrappedSchema(); |
|
535 | - if ($this->checkOracle) { |
|
536 | - $sourceSchema = $this->connection->createSchema(); |
|
537 | - $this->ensureOracleIdentifierLengthLimit($sourceSchema, $targetSchema, strlen($this->connection->getPrefix())); |
|
538 | - } |
|
539 | - $this->connection->migrateToSchema($targetSchema); |
|
540 | - $toSchema->performDropTableCalls(); |
|
541 | - } |
|
542 | - |
|
543 | - if (!$schemaOnly) { |
|
544 | - $instance->postSchemaChange($this->output, function () { |
|
545 | - return new SchemaWrapper($this->connection); |
|
546 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
547 | - } |
|
548 | - |
|
549 | - $this->markAsExecuted($version); |
|
550 | - } |
|
551 | - |
|
552 | - public function ensureOracleIdentifierLengthLimit(Schema $sourceSchema, Schema $targetSchema, int $prefixLength) { |
|
553 | - $sequences = $targetSchema->getSequences(); |
|
554 | - |
|
555 | - foreach ($targetSchema->getTables() as $table) { |
|
556 | - try { |
|
557 | - $sourceTable = $sourceSchema->getTable($table->getName()); |
|
558 | - } catch (SchemaException $e) { |
|
559 | - if (\strlen($table->getName()) - $prefixLength > 27) { |
|
560 | - throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
561 | - } |
|
562 | - $sourceTable = null; |
|
563 | - } |
|
564 | - |
|
565 | - foreach ($table->getColumns() as $thing) { |
|
566 | - if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
567 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
568 | - } |
|
569 | - |
|
570 | - if ($thing->getNotnull() && $thing->getDefault() === '' |
|
571 | - && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { |
|
572 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - foreach ($table->getIndexes() as $thing) { |
|
577 | - if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
578 | - throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
579 | - } |
|
580 | - } |
|
581 | - |
|
582 | - foreach ($table->getForeignKeys() as $thing) { |
|
583 | - if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
584 | - throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
585 | - } |
|
586 | - } |
|
587 | - |
|
588 | - $primaryKey = $table->getPrimaryKey(); |
|
589 | - if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) { |
|
590 | - $indexName = strtolower($primaryKey->getName()); |
|
591 | - $isUsingDefaultName = $indexName === 'primary'; |
|
592 | - |
|
593 | - if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) { |
|
594 | - $defaultName = $table->getName() . '_pkey'; |
|
595 | - $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
596 | - |
|
597 | - if ($isUsingDefaultName) { |
|
598 | - $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
599 | - $sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { |
|
600 | - return $sequence->getName() !== $sequenceName; |
|
601 | - }); |
|
602 | - } |
|
603 | - } elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
|
604 | - $defaultName = $table->getName() . '_seq'; |
|
605 | - $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
606 | - } |
|
607 | - |
|
608 | - if (!$isUsingDefaultName && \strlen($indexName) > 30) { |
|
609 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
610 | - } |
|
611 | - if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { |
|
612 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
613 | - } |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - foreach ($sequences as $sequence) { |
|
618 | - if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { |
|
619 | - throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
620 | - } |
|
621 | - } |
|
622 | - } |
|
623 | - |
|
624 | - private function ensureMigrationsAreLoaded() { |
|
625 | - if (empty($this->migrations)) { |
|
626 | - $this->migrations = $this->findMigrations(); |
|
627 | - } |
|
628 | - } |
|
50 | + /** @var boolean */ |
|
51 | + private $migrationTableCreated; |
|
52 | + /** @var array */ |
|
53 | + private $migrations; |
|
54 | + /** @var IOutput */ |
|
55 | + private $output; |
|
56 | + /** @var Connection */ |
|
57 | + private $connection; |
|
58 | + /** @var string */ |
|
59 | + private $appName; |
|
60 | + /** @var bool */ |
|
61 | + private $checkOracle; |
|
62 | + |
|
63 | + /** |
|
64 | + * MigrationService constructor. |
|
65 | + * |
|
66 | + * @param $appName |
|
67 | + * @param Connection $connection |
|
68 | + * @param AppLocator $appLocator |
|
69 | + * @param IOutput|null $output |
|
70 | + * @throws \Exception |
|
71 | + */ |
|
72 | + public function __construct($appName, Connection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
73 | + $this->appName = $appName; |
|
74 | + $this->connection = $connection; |
|
75 | + $this->output = $output; |
|
76 | + if (null === $this->output) { |
|
77 | + $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
78 | + } |
|
79 | + |
|
80 | + if ($appName === 'core') { |
|
81 | + $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
82 | + $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
83 | + $this->checkOracle = true; |
|
84 | + } else { |
|
85 | + if (null === $appLocator) { |
|
86 | + $appLocator = new AppLocator(); |
|
87 | + } |
|
88 | + $appPath = $appLocator->getAppPath($appName); |
|
89 | + $namespace = App::buildAppNamespace($appName); |
|
90 | + $this->migrationsPath = "$appPath/lib/Migration"; |
|
91 | + $this->migrationsNamespace = $namespace . '\\Migration'; |
|
92 | + |
|
93 | + $infoParser = new InfoParser(); |
|
94 | + $info = $infoParser->parse($appPath . '/appinfo/info.xml'); |
|
95 | + if (!isset($info['dependencies']['database'])) { |
|
96 | + $this->checkOracle = true; |
|
97 | + } else { |
|
98 | + $this->checkOracle = false; |
|
99 | + foreach ($info['dependencies']['database'] as $database) { |
|
100 | + if (\is_string($database) && $database === 'oci') { |
|
101 | + $this->checkOracle = true; |
|
102 | + } elseif (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') { |
|
103 | + $this->checkOracle = true; |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns the name of the app for which this migration is executed |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function getApp() { |
|
116 | + return $this->appName; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @return bool |
|
121 | + * @codeCoverageIgnore - this will implicitly tested on installation |
|
122 | + */ |
|
123 | + private function createMigrationTable() { |
|
124 | + if ($this->migrationTableCreated) { |
|
125 | + return false; |
|
126 | + } |
|
127 | + |
|
128 | + if ($this->connection->tableExists('migrations') && \OC::$server->getConfig()->getAppValue('core', 'vendor', '') !== 'owncloud') { |
|
129 | + $this->migrationTableCreated = true; |
|
130 | + return false; |
|
131 | + } |
|
132 | + |
|
133 | + $schema = new SchemaWrapper($this->connection); |
|
134 | + |
|
135 | + /** |
|
136 | + * We drop the table when it has different columns or the definition does not |
|
137 | + * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
138 | + */ |
|
139 | + try { |
|
140 | + $table = $schema->getTable('migrations'); |
|
141 | + $columns = $table->getColumns(); |
|
142 | + |
|
143 | + if (count($columns) === 2) { |
|
144 | + try { |
|
145 | + $column = $table->getColumn('app'); |
|
146 | + $schemaMismatch = $column->getLength() !== 255; |
|
147 | + |
|
148 | + if (!$schemaMismatch) { |
|
149 | + $column = $table->getColumn('version'); |
|
150 | + $schemaMismatch = $column->getLength() !== 255; |
|
151 | + } |
|
152 | + } catch (SchemaException $e) { |
|
153 | + // One of the columns is missing |
|
154 | + $schemaMismatch = true; |
|
155 | + } |
|
156 | + |
|
157 | + if (!$schemaMismatch) { |
|
158 | + // Table exists and schema matches: return back! |
|
159 | + $this->migrationTableCreated = true; |
|
160 | + return false; |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + // Drop the table, when it didn't match our expectations. |
|
165 | + $this->connection->dropTable('migrations'); |
|
166 | + |
|
167 | + // Recreate the schema after the table was dropped. |
|
168 | + $schema = new SchemaWrapper($this->connection); |
|
169 | + } catch (SchemaException $e) { |
|
170 | + // Table not found, no need to panic, we will create it. |
|
171 | + } |
|
172 | + |
|
173 | + $table = $schema->createTable('migrations'); |
|
174 | + $table->addColumn('app', Types::STRING, ['length' => 255]); |
|
175 | + $table->addColumn('version', Types::STRING, ['length' => 255]); |
|
176 | + $table->setPrimaryKey(['app', 'version']); |
|
177 | + |
|
178 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
179 | + |
|
180 | + $this->migrationTableCreated = true; |
|
181 | + |
|
182 | + return true; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Returns all versions which have already been applied |
|
187 | + * |
|
188 | + * @return string[] |
|
189 | + * @codeCoverageIgnore - no need to test this |
|
190 | + */ |
|
191 | + public function getMigratedVersions() { |
|
192 | + $this->createMigrationTable(); |
|
193 | + $qb = $this->connection->getQueryBuilder(); |
|
194 | + |
|
195 | + $qb->select('version') |
|
196 | + ->from('migrations') |
|
197 | + ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
198 | + ->orderBy('version'); |
|
199 | + |
|
200 | + $result = $qb->execute(); |
|
201 | + $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
202 | + $result->closeCursor(); |
|
203 | + |
|
204 | + return $rows; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Returns all versions which are available in the migration folder |
|
209 | + * |
|
210 | + * @return array |
|
211 | + */ |
|
212 | + public function getAvailableVersions() { |
|
213 | + $this->ensureMigrationsAreLoaded(); |
|
214 | + return array_map('strval', array_keys($this->migrations)); |
|
215 | + } |
|
216 | + |
|
217 | + protected function findMigrations() { |
|
218 | + $directory = realpath($this->migrationsPath); |
|
219 | + if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
220 | + return []; |
|
221 | + } |
|
222 | + |
|
223 | + $iterator = new \RegexIterator( |
|
224 | + new \RecursiveIteratorIterator( |
|
225 | + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
226 | + \RecursiveIteratorIterator::LEAVES_ONLY |
|
227 | + ), |
|
228 | + '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
229 | + \RegexIterator::GET_MATCH); |
|
230 | + |
|
231 | + $files = array_keys(iterator_to_array($iterator)); |
|
232 | + uasort($files, function ($a, $b) { |
|
233 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
234 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
235 | + if (!empty($matchA) && !empty($matchB)) { |
|
236 | + if ($matchA[1] !== $matchB[1]) { |
|
237 | + return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
238 | + } |
|
239 | + return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
240 | + } |
|
241 | + return (basename($a) < basename($b)) ? -1 : 1; |
|
242 | + }); |
|
243 | + |
|
244 | + $migrations = []; |
|
245 | + |
|
246 | + foreach ($files as $file) { |
|
247 | + $className = basename($file, '.php'); |
|
248 | + $version = (string) substr($className, 7); |
|
249 | + if ($version === '0') { |
|
250 | + throw new \InvalidArgumentException( |
|
251 | + "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
252 | + ); |
|
253 | + } |
|
254 | + $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
255 | + } |
|
256 | + |
|
257 | + return $migrations; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * @param string $to |
|
262 | + * @return string[] |
|
263 | + */ |
|
264 | + private function getMigrationsToExecute($to) { |
|
265 | + $knownMigrations = $this->getMigratedVersions(); |
|
266 | + $availableMigrations = $this->getAvailableVersions(); |
|
267 | + |
|
268 | + $toBeExecuted = []; |
|
269 | + foreach ($availableMigrations as $v) { |
|
270 | + if ($to !== 'latest' && $v > $to) { |
|
271 | + continue; |
|
272 | + } |
|
273 | + if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
274 | + $toBeExecuted[] = $v; |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + return $toBeExecuted; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param string $m |
|
283 | + * @param string[] $knownMigrations |
|
284 | + * @return bool |
|
285 | + */ |
|
286 | + private function shallBeExecuted($m, $knownMigrations) { |
|
287 | + if (in_array($m, $knownMigrations)) { |
|
288 | + return false; |
|
289 | + } |
|
290 | + |
|
291 | + return true; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * @param string $version |
|
296 | + */ |
|
297 | + private function markAsExecuted($version) { |
|
298 | + $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
299 | + 'app' => $this->appName, |
|
300 | + 'version' => $version |
|
301 | + ]); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Returns the name of the table which holds the already applied versions |
|
306 | + * |
|
307 | + * @return string |
|
308 | + */ |
|
309 | + public function getMigrationsTableName() { |
|
310 | + return $this->connection->getPrefix() . 'migrations'; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Returns the namespace of the version classes |
|
315 | + * |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + public function getMigrationsNamespace() { |
|
319 | + return $this->migrationsNamespace; |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Returns the directory which holds the versions |
|
324 | + * |
|
325 | + * @return string |
|
326 | + */ |
|
327 | + public function getMigrationsDirectory() { |
|
328 | + return $this->migrationsPath; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Return the explicit version for the aliases; current, next, prev, latest |
|
333 | + * |
|
334 | + * @param string $alias |
|
335 | + * @return mixed|null|string |
|
336 | + */ |
|
337 | + public function getMigration($alias) { |
|
338 | + switch ($alias) { |
|
339 | + case 'current': |
|
340 | + return $this->getCurrentVersion(); |
|
341 | + case 'next': |
|
342 | + return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
343 | + case 'prev': |
|
344 | + return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
345 | + case 'latest': |
|
346 | + $this->ensureMigrationsAreLoaded(); |
|
347 | + |
|
348 | + $migrations = $this->getAvailableVersions(); |
|
349 | + return @end($migrations); |
|
350 | + } |
|
351 | + return '0'; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * @param string $version |
|
356 | + * @param int $delta |
|
357 | + * @return null|string |
|
358 | + */ |
|
359 | + private function getRelativeVersion($version, $delta) { |
|
360 | + $this->ensureMigrationsAreLoaded(); |
|
361 | + |
|
362 | + $versions = $this->getAvailableVersions(); |
|
363 | + array_unshift($versions, 0); |
|
364 | + $offset = array_search($version, $versions, true); |
|
365 | + if ($offset === false || !isset($versions[$offset + $delta])) { |
|
366 | + // Unknown version or delta out of bounds. |
|
367 | + return null; |
|
368 | + } |
|
369 | + |
|
370 | + return (string) $versions[$offset + $delta]; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * @return string |
|
375 | + */ |
|
376 | + private function getCurrentVersion() { |
|
377 | + $m = $this->getMigratedVersions(); |
|
378 | + if (count($m) === 0) { |
|
379 | + return '0'; |
|
380 | + } |
|
381 | + $migrations = array_values($m); |
|
382 | + return @end($migrations); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * @param string $version |
|
387 | + * @return string |
|
388 | + * @throws \InvalidArgumentException |
|
389 | + */ |
|
390 | + private function getClass($version) { |
|
391 | + $this->ensureMigrationsAreLoaded(); |
|
392 | + |
|
393 | + if (isset($this->migrations[$version])) { |
|
394 | + return $this->migrations[$version]; |
|
395 | + } |
|
396 | + |
|
397 | + throw new \InvalidArgumentException("Version $version is unknown."); |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Allows to set an IOutput implementation which is used for logging progress and messages |
|
402 | + * |
|
403 | + * @param IOutput $output |
|
404 | + */ |
|
405 | + public function setOutput(IOutput $output) { |
|
406 | + $this->output = $output; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Applies all not yet applied versions up to $to |
|
411 | + * |
|
412 | + * @param string $to |
|
413 | + * @param bool $schemaOnly |
|
414 | + * @throws \InvalidArgumentException |
|
415 | + */ |
|
416 | + public function migrate($to = 'latest', $schemaOnly = false) { |
|
417 | + if ($schemaOnly) { |
|
418 | + $this->migrateSchemaOnly($to); |
|
419 | + return; |
|
420 | + } |
|
421 | + |
|
422 | + // read known migrations |
|
423 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
424 | + foreach ($toBeExecuted as $version) { |
|
425 | + try { |
|
426 | + $this->executeStep($version, $schemaOnly); |
|
427 | + } catch (DriverException $e) { |
|
428 | + // The exception itself does not contain the name of the migration, |
|
429 | + // so we wrap it here, to make debugging easier. |
|
430 | + throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); |
|
431 | + } |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Applies all not yet applied versions up to $to |
|
437 | + * |
|
438 | + * @param string $to |
|
439 | + * @throws \InvalidArgumentException |
|
440 | + */ |
|
441 | + public function migrateSchemaOnly($to = 'latest') { |
|
442 | + // read known migrations |
|
443 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
444 | + |
|
445 | + if (empty($toBeExecuted)) { |
|
446 | + return; |
|
447 | + } |
|
448 | + |
|
449 | + $toSchema = null; |
|
450 | + foreach ($toBeExecuted as $version) { |
|
451 | + $instance = $this->createInstance($version); |
|
452 | + |
|
453 | + $toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { |
|
454 | + return $toSchema ?: new SchemaWrapper($this->connection); |
|
455 | + }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema; |
|
456 | + |
|
457 | + $this->markAsExecuted($version); |
|
458 | + } |
|
459 | + |
|
460 | + if ($toSchema instanceof SchemaWrapper) { |
|
461 | + $targetSchema = $toSchema->getWrappedSchema(); |
|
462 | + if ($this->checkOracle) { |
|
463 | + $beforeSchema = $this->connection->createSchema(); |
|
464 | + $this->ensureOracleIdentifierLengthLimit($beforeSchema, $targetSchema, strlen($this->connection->getPrefix())); |
|
465 | + } |
|
466 | + $this->connection->migrateToSchema($targetSchema); |
|
467 | + $toSchema->performDropTableCalls(); |
|
468 | + } |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Get the human readable descriptions for the migration steps to run |
|
473 | + * |
|
474 | + * @param string $to |
|
475 | + * @return string[] [$name => $description] |
|
476 | + */ |
|
477 | + public function describeMigrationStep($to = 'latest') { |
|
478 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
479 | + $description = []; |
|
480 | + foreach ($toBeExecuted as $version) { |
|
481 | + $migration = $this->createInstance($version); |
|
482 | + if ($migration->name()) { |
|
483 | + $description[$migration->name()] = $migration->description(); |
|
484 | + } |
|
485 | + } |
|
486 | + return $description; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * @param string $version |
|
491 | + * @return IMigrationStep |
|
492 | + * @throws \InvalidArgumentException |
|
493 | + */ |
|
494 | + protected function createInstance($version) { |
|
495 | + $class = $this->getClass($version); |
|
496 | + try { |
|
497 | + $s = \OC::$server->query($class); |
|
498 | + |
|
499 | + if (!$s instanceof IMigrationStep) { |
|
500 | + throw new \InvalidArgumentException('Not a valid migration'); |
|
501 | + } |
|
502 | + } catch (QueryException $e) { |
|
503 | + if (class_exists($class)) { |
|
504 | + $s = new $class(); |
|
505 | + } else { |
|
506 | + throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
507 | + } |
|
508 | + } |
|
509 | + |
|
510 | + return $s; |
|
511 | + } |
|
512 | + |
|
513 | + /** |
|
514 | + * Executes one explicit version |
|
515 | + * |
|
516 | + * @param string $version |
|
517 | + * @param bool $schemaOnly |
|
518 | + * @throws \InvalidArgumentException |
|
519 | + */ |
|
520 | + public function executeStep($version, $schemaOnly = false) { |
|
521 | + $instance = $this->createInstance($version); |
|
522 | + |
|
523 | + if (!$schemaOnly) { |
|
524 | + $instance->preSchemaChange($this->output, function () { |
|
525 | + return new SchemaWrapper($this->connection); |
|
526 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
527 | + } |
|
528 | + |
|
529 | + $toSchema = $instance->changeSchema($this->output, function () { |
|
530 | + return new SchemaWrapper($this->connection); |
|
531 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
532 | + |
|
533 | + if ($toSchema instanceof SchemaWrapper) { |
|
534 | + $targetSchema = $toSchema->getWrappedSchema(); |
|
535 | + if ($this->checkOracle) { |
|
536 | + $sourceSchema = $this->connection->createSchema(); |
|
537 | + $this->ensureOracleIdentifierLengthLimit($sourceSchema, $targetSchema, strlen($this->connection->getPrefix())); |
|
538 | + } |
|
539 | + $this->connection->migrateToSchema($targetSchema); |
|
540 | + $toSchema->performDropTableCalls(); |
|
541 | + } |
|
542 | + |
|
543 | + if (!$schemaOnly) { |
|
544 | + $instance->postSchemaChange($this->output, function () { |
|
545 | + return new SchemaWrapper($this->connection); |
|
546 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
547 | + } |
|
548 | + |
|
549 | + $this->markAsExecuted($version); |
|
550 | + } |
|
551 | + |
|
552 | + public function ensureOracleIdentifierLengthLimit(Schema $sourceSchema, Schema $targetSchema, int $prefixLength) { |
|
553 | + $sequences = $targetSchema->getSequences(); |
|
554 | + |
|
555 | + foreach ($targetSchema->getTables() as $table) { |
|
556 | + try { |
|
557 | + $sourceTable = $sourceSchema->getTable($table->getName()); |
|
558 | + } catch (SchemaException $e) { |
|
559 | + if (\strlen($table->getName()) - $prefixLength > 27) { |
|
560 | + throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
561 | + } |
|
562 | + $sourceTable = null; |
|
563 | + } |
|
564 | + |
|
565 | + foreach ($table->getColumns() as $thing) { |
|
566 | + if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
567 | + throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
568 | + } |
|
569 | + |
|
570 | + if ($thing->getNotnull() && $thing->getDefault() === '' |
|
571 | + && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { |
|
572 | + throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + foreach ($table->getIndexes() as $thing) { |
|
577 | + if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
578 | + throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
579 | + } |
|
580 | + } |
|
581 | + |
|
582 | + foreach ($table->getForeignKeys() as $thing) { |
|
583 | + if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { |
|
584 | + throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
585 | + } |
|
586 | + } |
|
587 | + |
|
588 | + $primaryKey = $table->getPrimaryKey(); |
|
589 | + if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) { |
|
590 | + $indexName = strtolower($primaryKey->getName()); |
|
591 | + $isUsingDefaultName = $indexName === 'primary'; |
|
592 | + |
|
593 | + if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) { |
|
594 | + $defaultName = $table->getName() . '_pkey'; |
|
595 | + $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
596 | + |
|
597 | + if ($isUsingDefaultName) { |
|
598 | + $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
599 | + $sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { |
|
600 | + return $sequence->getName() !== $sequenceName; |
|
601 | + }); |
|
602 | + } |
|
603 | + } elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
|
604 | + $defaultName = $table->getName() . '_seq'; |
|
605 | + $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
606 | + } |
|
607 | + |
|
608 | + if (!$isUsingDefaultName && \strlen($indexName) > 30) { |
|
609 | + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
610 | + } |
|
611 | + if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { |
|
612 | + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
613 | + } |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + foreach ($sequences as $sequence) { |
|
618 | + if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { |
|
619 | + throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
620 | + } |
|
621 | + } |
|
622 | + } |
|
623 | + |
|
624 | + private function ensureMigrationsAreLoaded() { |
|
625 | + if (empty($this->migrations)) { |
|
626 | + $this->migrations = $this->findMigrations(); |
|
627 | + } |
|
628 | + } |
|
629 | 629 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | } |
79 | 79 | |
80 | 80 | if ($appName === 'core') { |
81 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
81 | + $this->migrationsPath = \OC::$SERVERROOT.'/core/Migrations'; |
|
82 | 82 | $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
83 | 83 | $this->checkOracle = true; |
84 | 84 | } else { |
@@ -88,10 +88,10 @@ discard block |
||
88 | 88 | $appPath = $appLocator->getAppPath($appName); |
89 | 89 | $namespace = App::buildAppNamespace($appName); |
90 | 90 | $this->migrationsPath = "$appPath/lib/Migration"; |
91 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
91 | + $this->migrationsNamespace = $namespace.'\\Migration'; |
|
92 | 92 | |
93 | 93 | $infoParser = new InfoParser(); |
94 | - $info = $infoParser->parse($appPath . '/appinfo/info.xml'); |
|
94 | + $info = $infoParser->parse($appPath.'/appinfo/info.xml'); |
|
95 | 95 | if (!isset($info['dependencies']['database'])) { |
96 | 96 | $this->checkOracle = true; |
97 | 97 | } else { |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | \RegexIterator::GET_MATCH); |
230 | 230 | |
231 | 231 | $files = array_keys(iterator_to_array($iterator)); |
232 | - uasort($files, function ($a, $b) { |
|
232 | + uasort($files, function($a, $b) { |
|
233 | 233 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
234 | 234 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
235 | 235 | if (!empty($matchA) && !empty($matchB)) { |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | * @return string |
308 | 308 | */ |
309 | 309 | public function getMigrationsTableName() { |
310 | - return $this->connection->getPrefix() . 'migrations'; |
|
310 | + return $this->connection->getPrefix().'migrations'; |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | /** |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | } catch (DriverException $e) { |
428 | 428 | // The exception itself does not contain the name of the migration, |
429 | 429 | // so we wrap it here, to make debugging easier. |
430 | - throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); |
|
430 | + throw new \Exception('Database error when running migration '.$to.' for app '.$this->getApp(), 0, $e); |
|
431 | 431 | } |
432 | 432 | } |
433 | 433 | } |
@@ -450,7 +450,7 @@ discard block |
||
450 | 450 | foreach ($toBeExecuted as $version) { |
451 | 451 | $instance = $this->createInstance($version); |
452 | 452 | |
453 | - $toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { |
|
453 | + $toSchema = $instance->changeSchema($this->output, function() use ($toSchema) { |
|
454 | 454 | return $toSchema ?: new SchemaWrapper($this->connection); |
455 | 455 | }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema; |
456 | 456 | |
@@ -521,12 +521,12 @@ discard block |
||
521 | 521 | $instance = $this->createInstance($version); |
522 | 522 | |
523 | 523 | if (!$schemaOnly) { |
524 | - $instance->preSchemaChange($this->output, function () { |
|
524 | + $instance->preSchemaChange($this->output, function() { |
|
525 | 525 | return new SchemaWrapper($this->connection); |
526 | 526 | }, ['tablePrefix' => $this->connection->getPrefix()]); |
527 | 527 | } |
528 | 528 | |
529 | - $toSchema = $instance->changeSchema($this->output, function () { |
|
529 | + $toSchema = $instance->changeSchema($this->output, function() { |
|
530 | 530 | return new SchemaWrapper($this->connection); |
531 | 531 | }, ['tablePrefix' => $this->connection->getPrefix()]); |
532 | 532 | |
@@ -541,7 +541,7 @@ discard block |
||
541 | 541 | } |
542 | 542 | |
543 | 543 | if (!$schemaOnly) { |
544 | - $instance->postSchemaChange($this->output, function () { |
|
544 | + $instance->postSchemaChange($this->output, function() { |
|
545 | 545 | return new SchemaWrapper($this->connection); |
546 | 546 | }, ['tablePrefix' => $this->connection->getPrefix()]); |
547 | 547 | } |
@@ -557,31 +557,31 @@ discard block |
||
557 | 557 | $sourceTable = $sourceSchema->getTable($table->getName()); |
558 | 558 | } catch (SchemaException $e) { |
559 | 559 | if (\strlen($table->getName()) - $prefixLength > 27) { |
560 | - throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
560 | + throw new \InvalidArgumentException('Table name "'.$table->getName().'" is too long.'); |
|
561 | 561 | } |
562 | 562 | $sourceTable = null; |
563 | 563 | } |
564 | 564 | |
565 | 565 | foreach ($table->getColumns() as $thing) { |
566 | 566 | if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { |
567 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
567 | + throw new \InvalidArgumentException('Column name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
568 | 568 | } |
569 | 569 | |
570 | 570 | if ($thing->getNotnull() && $thing->getDefault() === '' |
571 | 571 | && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { |
572 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); |
|
572 | + throw new \InvalidArgumentException('Column name "'.$table->getName().'"."'.$thing->getName().'" is NotNull, but has empty string or null as default.'); |
|
573 | 573 | } |
574 | 574 | } |
575 | 575 | |
576 | 576 | foreach ($table->getIndexes() as $thing) { |
577 | 577 | if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { |
578 | - throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
578 | + throw new \InvalidArgumentException('Index name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
579 | 579 | } |
580 | 580 | } |
581 | 581 | |
582 | 582 | foreach ($table->getForeignKeys() as $thing) { |
583 | 583 | if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { |
584 | - throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
584 | + throw new \InvalidArgumentException('Foreign key name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
585 | 585 | } |
586 | 586 | } |
587 | 587 | |
@@ -591,32 +591,32 @@ discard block |
||
591 | 591 | $isUsingDefaultName = $indexName === 'primary'; |
592 | 592 | |
593 | 593 | if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) { |
594 | - $defaultName = $table->getName() . '_pkey'; |
|
594 | + $defaultName = $table->getName().'_pkey'; |
|
595 | 595 | $isUsingDefaultName = strtolower($defaultName) === $indexName; |
596 | 596 | |
597 | 597 | if ($isUsingDefaultName) { |
598 | - $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
599 | - $sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { |
|
598 | + $sequenceName = $table->getName().'_'.implode('_', $primaryKey->getColumns()).'_seq'; |
|
599 | + $sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) { |
|
600 | 600 | return $sequence->getName() !== $sequenceName; |
601 | 601 | }); |
602 | 602 | } |
603 | 603 | } elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
604 | - $defaultName = $table->getName() . '_seq'; |
|
604 | + $defaultName = $table->getName().'_seq'; |
|
605 | 605 | $isUsingDefaultName = strtolower($defaultName) === $indexName; |
606 | 606 | } |
607 | 607 | |
608 | 608 | if (!$isUsingDefaultName && \strlen($indexName) > 30) { |
609 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
609 | + throw new \InvalidArgumentException('Primary index name on "'.$table->getName().'" is too long.'); |
|
610 | 610 | } |
611 | 611 | if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { |
612 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
612 | + throw new \InvalidArgumentException('Primary index name on "'.$table->getName().'" is too long.'); |
|
613 | 613 | } |
614 | 614 | } |
615 | 615 | } |
616 | 616 | |
617 | 617 | foreach ($sequences as $sequence) { |
618 | 618 | if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { |
619 | - throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
619 | + throw new \InvalidArgumentException('Sequence name "'.$sequence->getName().'" is too long.'); |
|
620 | 620 | } |
621 | 621 | } |
622 | 622 | } |
@@ -38,124 +38,124 @@ |
||
38 | 38 | use Doctrine\DBAL\Schema\Schema; |
39 | 39 | |
40 | 40 | class MDB2SchemaManager { |
41 | - /** @var Connection $conn */ |
|
42 | - protected $conn; |
|
41 | + /** @var Connection $conn */ |
|
42 | + protected $conn; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param Connection $conn |
|
46 | - */ |
|
47 | - public function __construct($conn) { |
|
48 | - $this->conn = $conn; |
|
49 | - } |
|
44 | + /** |
|
45 | + * @param Connection $conn |
|
46 | + */ |
|
47 | + public function __construct($conn) { |
|
48 | + $this->conn = $conn; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Creates tables from XML file |
|
53 | - * @param string $file file to read structure from |
|
54 | - * @return bool |
|
55 | - * |
|
56 | - * TODO: write more documentation |
|
57 | - */ |
|
58 | - public function createDbFromStructure($file) { |
|
59 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
60 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
61 | - $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
62 | - return $this->executeSchemaChange($toSchema); |
|
63 | - } |
|
51 | + /** |
|
52 | + * Creates tables from XML file |
|
53 | + * @param string $file file to read structure from |
|
54 | + * @return bool |
|
55 | + * |
|
56 | + * TODO: write more documentation |
|
57 | + */ |
|
58 | + public function createDbFromStructure($file) { |
|
59 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
60 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
61 | + $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
62 | + return $this->executeSchemaChange($toSchema); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return \OC\DB\Migrator |
|
67 | - */ |
|
68 | - public function getMigrator() { |
|
69 | - $random = \OC::$server->getSecureRandom(); |
|
70 | - $platform = $this->conn->getDatabasePlatform(); |
|
71 | - $config = \OC::$server->getConfig(); |
|
72 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
73 | - if ($platform instanceof SqlitePlatform) { |
|
74 | - return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); |
|
75 | - } elseif ($platform instanceof OraclePlatform) { |
|
76 | - return new OracleMigrator($this->conn, $random, $config, $dispatcher); |
|
77 | - } elseif ($platform instanceof MySQLPlatform) { |
|
78 | - return new MySQLMigrator($this->conn, $random, $config, $dispatcher); |
|
79 | - } elseif ($platform instanceof PostgreSQL94Platform) { |
|
80 | - return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); |
|
81 | - } else { |
|
82 | - return new Migrator($this->conn, $random, $config, $dispatcher); |
|
83 | - } |
|
84 | - } |
|
65 | + /** |
|
66 | + * @return \OC\DB\Migrator |
|
67 | + */ |
|
68 | + public function getMigrator() { |
|
69 | + $random = \OC::$server->getSecureRandom(); |
|
70 | + $platform = $this->conn->getDatabasePlatform(); |
|
71 | + $config = \OC::$server->getConfig(); |
|
72 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
73 | + if ($platform instanceof SqlitePlatform) { |
|
74 | + return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); |
|
75 | + } elseif ($platform instanceof OraclePlatform) { |
|
76 | + return new OracleMigrator($this->conn, $random, $config, $dispatcher); |
|
77 | + } elseif ($platform instanceof MySQLPlatform) { |
|
78 | + return new MySQLMigrator($this->conn, $random, $config, $dispatcher); |
|
79 | + } elseif ($platform instanceof PostgreSQL94Platform) { |
|
80 | + return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); |
|
81 | + } else { |
|
82 | + return new Migrator($this->conn, $random, $config, $dispatcher); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Reads database schema from file |
|
88 | - * |
|
89 | - * @param string $file file to read from |
|
90 | - * @return \Doctrine\DBAL\Schema\Schema |
|
91 | - */ |
|
92 | - private function readSchemaFromFile($file) { |
|
93 | - $platform = $this->conn->getDatabasePlatform(); |
|
94 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
95 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
96 | - return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
97 | - } |
|
86 | + /** |
|
87 | + * Reads database schema from file |
|
88 | + * |
|
89 | + * @param string $file file to read from |
|
90 | + * @return \Doctrine\DBAL\Schema\Schema |
|
91 | + */ |
|
92 | + private function readSchemaFromFile($file) { |
|
93 | + $platform = $this->conn->getDatabasePlatform(); |
|
94 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
95 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
96 | + return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * update the database scheme |
|
101 | - * @param string $file file to read structure from |
|
102 | - * @param bool $generateSql only return the sql needed for the upgrade |
|
103 | - * @return string|boolean |
|
104 | - */ |
|
105 | - public function updateDbFromStructure($file, $generateSql = false) { |
|
106 | - $toSchema = $this->readSchemaFromFile($file); |
|
107 | - $migrator = $this->getMigrator(); |
|
99 | + /** |
|
100 | + * update the database scheme |
|
101 | + * @param string $file file to read structure from |
|
102 | + * @param bool $generateSql only return the sql needed for the upgrade |
|
103 | + * @return string|boolean |
|
104 | + */ |
|
105 | + public function updateDbFromStructure($file, $generateSql = false) { |
|
106 | + $toSchema = $this->readSchemaFromFile($file); |
|
107 | + $migrator = $this->getMigrator(); |
|
108 | 108 | |
109 | - if ($generateSql) { |
|
110 | - return $migrator->generateChangeScript($toSchema); |
|
111 | - } else { |
|
112 | - $migrator->migrate($toSchema); |
|
113 | - return true; |
|
114 | - } |
|
115 | - } |
|
109 | + if ($generateSql) { |
|
110 | + return $migrator->generateChangeScript($toSchema); |
|
111 | + } else { |
|
112 | + $migrator->migrate($toSchema); |
|
113 | + return true; |
|
114 | + } |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @param \Doctrine\DBAL\Schema\Schema $schema |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function generateChangeScript($schema) { |
|
122 | - $migrator = $this->getMigrator(); |
|
123 | - return $migrator->generateChangeScript($schema); |
|
124 | - } |
|
117 | + /** |
|
118 | + * @param \Doctrine\DBAL\Schema\Schema $schema |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function generateChangeScript($schema) { |
|
122 | + $migrator = $this->getMigrator(); |
|
123 | + return $migrator->generateChangeScript($schema); |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * remove all tables defined in a database structure xml file |
|
128 | - * |
|
129 | - * @param string $file the xml file describing the tables |
|
130 | - */ |
|
131 | - public function removeDBStructure($file) { |
|
132 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
133 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
134 | - $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
135 | - $toSchema = clone $fromSchema; |
|
136 | - foreach ($toSchema->getTables() as $table) { |
|
137 | - $toSchema->dropTable($table->getName()); |
|
138 | - } |
|
139 | - $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
140 | - $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
141 | - $this->executeSchemaChange($schemaDiff); |
|
142 | - } |
|
126 | + /** |
|
127 | + * remove all tables defined in a database structure xml file |
|
128 | + * |
|
129 | + * @param string $file the xml file describing the tables |
|
130 | + */ |
|
131 | + public function removeDBStructure($file) { |
|
132 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
133 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
134 | + $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
135 | + $toSchema = clone $fromSchema; |
|
136 | + foreach ($toSchema->getTables() as $table) { |
|
137 | + $toSchema->dropTable($table->getName()); |
|
138 | + } |
|
139 | + $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
140 | + $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
141 | + $this->executeSchemaChange($schemaDiff); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - private function executeSchemaChange($schema) { |
|
149 | - $this->conn->beginTransaction(); |
|
150 | - foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
151 | - $this->conn->query($sql); |
|
152 | - } |
|
153 | - $this->conn->commit(); |
|
144 | + /** |
|
145 | + * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + private function executeSchemaChange($schema) { |
|
149 | + $this->conn->beginTransaction(); |
|
150 | + foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
151 | + $this->conn->query($sql); |
|
152 | + } |
|
153 | + $this->conn->commit(); |
|
154 | 154 | |
155 | - if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
156 | - $this->conn->close(); |
|
157 | - $this->conn->connect(); |
|
158 | - } |
|
159 | - return true; |
|
160 | - } |
|
155 | + if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
156 | + $this->conn->close(); |
|
157 | + $this->conn->connect(); |
|
158 | + } |
|
159 | + return true; |
|
160 | + } |
|
161 | 161 | } |
@@ -31,41 +31,41 @@ |
||
31 | 31 | */ |
32 | 32 | class MySqlTools { |
33 | 33 | |
34 | - /** |
|
35 | - * @param IDBConnection $connection |
|
36 | - * @return bool |
|
37 | - */ |
|
38 | - public function supports4ByteCharset(IDBConnection $connection) { |
|
39 | - $variables = ['innodb_file_per_table' => 'ON']; |
|
40 | - if (!$this->isMariaDBWithLargePrefix($connection)) { |
|
41 | - $variables['innodb_file_format'] = 'Barracuda'; |
|
42 | - $variables['innodb_large_prefix'] = 'ON'; |
|
43 | - } |
|
34 | + /** |
|
35 | + * @param IDBConnection $connection |
|
36 | + * @return bool |
|
37 | + */ |
|
38 | + public function supports4ByteCharset(IDBConnection $connection) { |
|
39 | + $variables = ['innodb_file_per_table' => 'ON']; |
|
40 | + if (!$this->isMariaDBWithLargePrefix($connection)) { |
|
41 | + $variables['innodb_file_format'] = 'Barracuda'; |
|
42 | + $variables['innodb_large_prefix'] = 'ON'; |
|
43 | + } |
|
44 | 44 | |
45 | - foreach ($variables as $var => $val) { |
|
46 | - $result = $connection->executeQuery("SHOW VARIABLES LIKE '$var'"); |
|
47 | - $row = $result->fetch(); |
|
48 | - $result->closeCursor(); |
|
49 | - if ($row === false) { |
|
50 | - return false; |
|
51 | - } |
|
52 | - if (strcasecmp($row['Value'], $val) !== 0) { |
|
53 | - return false; |
|
54 | - } |
|
55 | - } |
|
56 | - return true; |
|
57 | - } |
|
45 | + foreach ($variables as $var => $val) { |
|
46 | + $result = $connection->executeQuery("SHOW VARIABLES LIKE '$var'"); |
|
47 | + $row = $result->fetch(); |
|
48 | + $result->closeCursor(); |
|
49 | + if ($row === false) { |
|
50 | + return false; |
|
51 | + } |
|
52 | + if (strcasecmp($row['Value'], $val) !== 0) { |
|
53 | + return false; |
|
54 | + } |
|
55 | + } |
|
56 | + return true; |
|
57 | + } |
|
58 | 58 | |
59 | - protected function isMariaDBWithLargePrefix(IDBConnection $connection) { |
|
60 | - $result = $connection->executeQuery('SELECT VERSION()'); |
|
61 | - $row = strtolower($result->fetchColumn()); |
|
62 | - $result->closeCursor(); |
|
59 | + protected function isMariaDBWithLargePrefix(IDBConnection $connection) { |
|
60 | + $result = $connection->executeQuery('SELECT VERSION()'); |
|
61 | + $row = strtolower($result->fetchColumn()); |
|
62 | + $result->closeCursor(); |
|
63 | 63 | |
64 | - if ($row === false) { |
|
65 | - return false; |
|
66 | - } |
|
64 | + if ($row === false) { |
|
65 | + return false; |
|
66 | + } |
|
67 | 67 | |
68 | - return strpos($row, 'maria') && version_compare($row, '10.3', '>=') || |
|
69 | - strpos($row, 'maria') === false && version_compare($row, '8.0', '>='); |
|
70 | - } |
|
68 | + return strpos($row, 'maria') && version_compare($row, '10.3', '>=') || |
|
69 | + strpos($row, 'maria') === false && version_compare($row, '8.0', '>='); |
|
70 | + } |
|
71 | 71 | } |
@@ -36,51 +36,51 @@ |
||
36 | 36 | */ |
37 | 37 | class PgSqlTools { |
38 | 38 | |
39 | - /** @var \OCP\IConfig */ |
|
40 | - private $config; |
|
39 | + /** @var \OCP\IConfig */ |
|
40 | + private $config; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param \OCP\IConfig $config |
|
44 | - */ |
|
45 | - public function __construct(IConfig $config) { |
|
46 | - $this->config = $config; |
|
47 | - } |
|
42 | + /** |
|
43 | + * @param \OCP\IConfig $config |
|
44 | + */ |
|
45 | + public function __construct(IConfig $config) { |
|
46 | + $this->config = $config; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @brief Resynchronizes all sequences of a database after using INSERTs |
|
51 | - * without leaving out the auto-incremented column. |
|
52 | - * @param \OC\DB\Connection $conn |
|
53 | - * @return null |
|
54 | - */ |
|
55 | - public function resynchronizeDatabaseSequences(Connection $conn) { |
|
56 | - $databaseName = $conn->getDatabase(); |
|
57 | - $conn->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
58 | - /** @var string|AbstractAsset $asset */ |
|
59 | - $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
60 | - if ($asset instanceof AbstractAsset) { |
|
61 | - return preg_match($filterExpression, $asset->getName()) !== false; |
|
62 | - } |
|
63 | - return preg_match($filterExpression, $asset) !== false; |
|
64 | - }); |
|
49 | + /** |
|
50 | + * @brief Resynchronizes all sequences of a database after using INSERTs |
|
51 | + * without leaving out the auto-incremented column. |
|
52 | + * @param \OC\DB\Connection $conn |
|
53 | + * @return null |
|
54 | + */ |
|
55 | + public function resynchronizeDatabaseSequences(Connection $conn) { |
|
56 | + $databaseName = $conn->getDatabase(); |
|
57 | + $conn->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
58 | + /** @var string|AbstractAsset $asset */ |
|
59 | + $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
60 | + if ($asset instanceof AbstractAsset) { |
|
61 | + return preg_match($filterExpression, $asset->getName()) !== false; |
|
62 | + } |
|
63 | + return preg_match($filterExpression, $asset) !== false; |
|
64 | + }); |
|
65 | 65 | |
66 | - foreach ($conn->getSchemaManager()->listSequences() as $sequence) { |
|
67 | - $sequenceName = $sequence->getName(); |
|
68 | - $sqlInfo = 'SELECT table_schema, table_name, column_name |
|
66 | + foreach ($conn->getSchemaManager()->listSequences() as $sequence) { |
|
67 | + $sequenceName = $sequence->getName(); |
|
68 | + $sqlInfo = 'SELECT table_schema, table_name, column_name |
|
69 | 69 | FROM information_schema.columns |
70 | 70 | WHERE column_default = ? AND table_catalog = ?'; |
71 | - $result = $conn->executeQuery($sqlInfo, [ |
|
72 | - "nextval('$sequenceName'::regclass)", |
|
73 | - $databaseName |
|
74 | - ]); |
|
75 | - $sequenceInfo = $result->fetchAssociative(); |
|
76 | - $result->free(); |
|
77 | - /** @var string $tableName */ |
|
78 | - $tableName = $sequenceInfo['table_name']; |
|
79 | - /** @var string $columnName */ |
|
80 | - $columnName = $sequenceInfo['column_name']; |
|
81 | - $sqlMaxId = "SELECT MAX($columnName) FROM $tableName"; |
|
82 | - $sqlSetval = "SELECT setval('$sequenceName', ($sqlMaxId))"; |
|
83 | - $conn->executeQuery($sqlSetval); |
|
84 | - } |
|
85 | - } |
|
71 | + $result = $conn->executeQuery($sqlInfo, [ |
|
72 | + "nextval('$sequenceName'::regclass)", |
|
73 | + $databaseName |
|
74 | + ]); |
|
75 | + $sequenceInfo = $result->fetchAssociative(); |
|
76 | + $result->free(); |
|
77 | + /** @var string $tableName */ |
|
78 | + $tableName = $sequenceInfo['table_name']; |
|
79 | + /** @var string $columnName */ |
|
80 | + $columnName = $sequenceInfo['column_name']; |
|
81 | + $sqlMaxId = "SELECT MAX($columnName) FROM $tableName"; |
|
82 | + $sqlSetval = "SELECT setval('$sequenceName', ($sqlMaxId))"; |
|
83 | + $conn->executeQuery($sqlSetval); |
|
84 | + } |
|
85 | + } |
|
86 | 86 | } |
@@ -54,9 +54,9 @@ |
||
54 | 54 | */ |
55 | 55 | public function resynchronizeDatabaseSequences(Connection $conn) { |
56 | 56 | $databaseName = $conn->getDatabase(); |
57 | - $conn->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
57 | + $conn->getConfiguration()->setSchemaAssetsFilter(function($asset) { |
|
58 | 58 | /** @var string|AbstractAsset $asset */ |
59 | - $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
59 | + $filterExpression = '/^'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
60 | 60 | if ($asset instanceof AbstractAsset) { |
61 | 61 | return preg_match($filterExpression, $asset->getName()) !== false; |
62 | 62 | } |
@@ -28,48 +28,48 @@ |
||
28 | 28 | namespace OC\DB; |
29 | 29 | |
30 | 30 | class AdapterPgSql extends Adapter { |
31 | - protected $compatModePre9_5 = null; |
|
31 | + protected $compatModePre9_5 = null; |
|
32 | 32 | |
33 | - public function lastInsertId($table) { |
|
34 | - $result = $this->conn->executeQuery('SELECT lastval()'); |
|
35 | - $val = $result->fetchOne(); |
|
36 | - $result->free(); |
|
37 | - return (int)$val; |
|
38 | - } |
|
33 | + public function lastInsertId($table) { |
|
34 | + $result = $this->conn->executeQuery('SELECT lastval()'); |
|
35 | + $val = $result->fetchOne(); |
|
36 | + $result->free(); |
|
37 | + return (int)$val; |
|
38 | + } |
|
39 | 39 | |
40 | - public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
|
41 | - public function fixupStatement($statement) { |
|
42 | - $statement = str_replace('`', '"', $statement); |
|
43 | - $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement); |
|
44 | - return $statement; |
|
45 | - } |
|
40 | + public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
|
41 | + public function fixupStatement($statement) { |
|
42 | + $statement = str_replace('`', '"', $statement); |
|
43 | + $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement); |
|
44 | + return $statement; |
|
45 | + } |
|
46 | 46 | |
47 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
48 | - if ($this->isPre9_5CompatMode() === true) { |
|
49 | - return parent::insertIgnoreConflict($table, $values); |
|
50 | - } |
|
47 | + public function insertIgnoreConflict(string $table,array $values) : int { |
|
48 | + if ($this->isPre9_5CompatMode() === true) { |
|
49 | + return parent::insertIgnoreConflict($table, $values); |
|
50 | + } |
|
51 | 51 | |
52 | - // "upsert" is only available since PgSQL 9.5, but the generic way |
|
53 | - // would leave error logs in the DB. |
|
54 | - $builder = $this->conn->getQueryBuilder(); |
|
55 | - $builder->insert($table); |
|
56 | - foreach ($values as $key => $value) { |
|
57 | - $builder->setValue($key, $builder->createNamedParameter($value)); |
|
58 | - } |
|
59 | - $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
60 | - return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
|
61 | - } |
|
52 | + // "upsert" is only available since PgSQL 9.5, but the generic way |
|
53 | + // would leave error logs in the DB. |
|
54 | + $builder = $this->conn->getQueryBuilder(); |
|
55 | + $builder->insert($table); |
|
56 | + foreach ($values as $key => $value) { |
|
57 | + $builder->setValue($key, $builder->createNamedParameter($value)); |
|
58 | + } |
|
59 | + $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
60 | + return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
|
61 | + } |
|
62 | 62 | |
63 | - protected function isPre9_5CompatMode(): bool { |
|
64 | - if ($this->compatModePre9_5 !== null) { |
|
65 | - return $this->compatModePre9_5; |
|
66 | - } |
|
63 | + protected function isPre9_5CompatMode(): bool { |
|
64 | + if ($this->compatModePre9_5 !== null) { |
|
65 | + return $this->compatModePre9_5; |
|
66 | + } |
|
67 | 67 | |
68 | - $result = $this->conn->executeQuery('SHOW SERVER_VERSION'); |
|
69 | - $version = $result->fetchOne(); |
|
70 | - $result->free(); |
|
71 | - $this->compatModePre9_5 = version_compare($version, '9.5', '<'); |
|
68 | + $result = $this->conn->executeQuery('SHOW SERVER_VERSION'); |
|
69 | + $version = $result->fetchOne(); |
|
70 | + $result->free(); |
|
71 | + $this->compatModePre9_5 = version_compare($version, '9.5', '<'); |
|
72 | 72 | |
73 | - return $this->compatModePre9_5; |
|
74 | - } |
|
73 | + return $this->compatModePre9_5; |
|
74 | + } |
|
75 | 75 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | $result = $this->conn->executeQuery('SELECT lastval()'); |
35 | 35 | $val = $result->fetchOne(); |
36 | 36 | $result->free(); |
37 | - return (int)$val; |
|
37 | + return (int) $val; |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | return $statement; |
45 | 45 | } |
46 | 46 | |
47 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
47 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
48 | 48 | if ($this->isPre9_5CompatMode() === true) { |
49 | 49 | return parent::insertIgnoreConflict($table, $values); |
50 | 50 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | foreach ($values as $key => $value) { |
57 | 57 | $builder->setValue($key, $builder->createNamedParameter($value)); |
58 | 58 | } |
59 | - $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
59 | + $queryString = $builder->getSQL().' ON CONFLICT DO NOTHING'; |
|
60 | 60 | return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
61 | 61 | } |
62 | 62 |
@@ -43,59 +43,59 @@ |
||
43 | 43 | */ |
44 | 44 | class PreparedStatement implements IPreparedStatement { |
45 | 45 | |
46 | - /** @var Statement */ |
|
47 | - private $statement; |
|
46 | + /** @var Statement */ |
|
47 | + private $statement; |
|
48 | 48 | |
49 | - /** @var IResult|null */ |
|
50 | - private $result; |
|
49 | + /** @var IResult|null */ |
|
50 | + private $result; |
|
51 | 51 | |
52 | - public function __construct(Statement $statement) { |
|
53 | - $this->statement = $statement; |
|
54 | - } |
|
52 | + public function __construct(Statement $statement) { |
|
53 | + $this->statement = $statement; |
|
54 | + } |
|
55 | 55 | |
56 | - public function closeCursor(): bool { |
|
57 | - $this->getResult()->closeCursor(); |
|
56 | + public function closeCursor(): bool { |
|
57 | + $this->getResult()->closeCursor(); |
|
58 | 58 | |
59 | - return true; |
|
60 | - } |
|
59 | + return true; |
|
60 | + } |
|
61 | 61 | |
62 | - public function fetch(int $fetchMode = PDO::FETCH_ASSOC) { |
|
63 | - return $this->getResult()->fetch($fetchMode); |
|
64 | - } |
|
62 | + public function fetch(int $fetchMode = PDO::FETCH_ASSOC) { |
|
63 | + return $this->getResult()->fetch($fetchMode); |
|
64 | + } |
|
65 | 65 | |
66 | - public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array { |
|
67 | - return $this->getResult()->fetchAll($fetchMode); |
|
68 | - } |
|
66 | + public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array { |
|
67 | + return $this->getResult()->fetchAll($fetchMode); |
|
68 | + } |
|
69 | 69 | |
70 | - public function fetchColumn() { |
|
71 | - return $this->getResult()->fetchOne(); |
|
72 | - } |
|
70 | + public function fetchColumn() { |
|
71 | + return $this->getResult()->fetchOne(); |
|
72 | + } |
|
73 | 73 | |
74 | - public function fetchOne() { |
|
75 | - return $this->getResult()->fetchOne(); |
|
76 | - } |
|
74 | + public function fetchOne() { |
|
75 | + return $this->getResult()->fetchOne(); |
|
76 | + } |
|
77 | 77 | |
78 | - public function bindValue($param, $value, $type = ParameterType::STRING): bool { |
|
79 | - return $this->statement->bindValue($param, $value, $type); |
|
80 | - } |
|
78 | + public function bindValue($param, $value, $type = ParameterType::STRING): bool { |
|
79 | + return $this->statement->bindValue($param, $value, $type); |
|
80 | + } |
|
81 | 81 | |
82 | - public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool { |
|
83 | - return $this->statement->bindParam($param, $variable, $type, $length); |
|
84 | - } |
|
82 | + public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool { |
|
83 | + return $this->statement->bindParam($param, $variable, $type, $length); |
|
84 | + } |
|
85 | 85 | |
86 | - public function execute($params = null): IResult { |
|
87 | - return ($this->result = new ResultAdapter($this->statement->execute($params))); |
|
88 | - } |
|
86 | + public function execute($params = null): IResult { |
|
87 | + return ($this->result = new ResultAdapter($this->statement->execute($params))); |
|
88 | + } |
|
89 | 89 | |
90 | - public function rowCount(): int { |
|
91 | - return $this->getResult()->rowCount(); |
|
92 | - } |
|
90 | + public function rowCount(): int { |
|
91 | + return $this->getResult()->rowCount(); |
|
92 | + } |
|
93 | 93 | |
94 | - private function getResult(): IResult { |
|
95 | - if ($this->result !== null) { |
|
96 | - return $this->result; |
|
97 | - } |
|
94 | + private function getResult(): IResult { |
|
95 | + if ($this->result !== null) { |
|
96 | + return $this->result; |
|
97 | + } |
|
98 | 98 | |
99 | - throw new Exception("You have to execute the prepared statement before accessing the results"); |
|
100 | - } |
|
99 | + throw new Exception("You have to execute the prepared statement before accessing the results"); |
|
100 | + } |
|
101 | 101 | } |
@@ -29,27 +29,27 @@ |
||
29 | 29 | use Doctrine\DBAL\Driver; |
30 | 30 | |
31 | 31 | class ReconnectWrapper extends \Doctrine\DBAL\Connection { |
32 | - public const CHECK_CONNECTION_INTERVAL = 60; |
|
32 | + public const CHECK_CONNECTION_INTERVAL = 60; |
|
33 | 33 | |
34 | - private $lastConnectionCheck = null; |
|
34 | + private $lastConnectionCheck = null; |
|
35 | 35 | |
36 | - public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { |
|
37 | - parent::__construct($params, $driver, $config, $eventManager); |
|
38 | - $this->lastConnectionCheck = time(); |
|
39 | - } |
|
36 | + public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { |
|
37 | + parent::__construct($params, $driver, $config, $eventManager); |
|
38 | + $this->lastConnectionCheck = time(); |
|
39 | + } |
|
40 | 40 | |
41 | - public function connect() { |
|
42 | - $now = time(); |
|
43 | - $checkTime = $now - self::CHECK_CONNECTION_INTERVAL; |
|
41 | + public function connect() { |
|
42 | + $now = time(); |
|
43 | + $checkTime = $now - self::CHECK_CONNECTION_INTERVAL; |
|
44 | 44 | |
45 | - if ($this->lastConnectionCheck > $checkTime || $this->isTransactionActive()) { |
|
46 | - return parent::connect(); |
|
47 | - } |
|
45 | + if ($this->lastConnectionCheck > $checkTime || $this->isTransactionActive()) { |
|
46 | + return parent::connect(); |
|
47 | + } |
|
48 | 48 | |
49 | - $this->lastConnectionCheck = $now; |
|
50 | - if (!$this->isConnected()) { |
|
51 | - $this->close(); |
|
52 | - } |
|
53 | - return parent::connect(); |
|
54 | - } |
|
49 | + $this->lastConnectionCheck = $now; |
|
50 | + if (!$this->isConnected()) { |
|
51 | + $this->close(); |
|
52 | + } |
|
53 | + return parent::connect(); |
|
54 | + } |
|
55 | 55 | } |