MySQLMigrator.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\DB;
  26. use Doctrine\DBAL\Schema\Schema;
  27. use Doctrine\DBAL\Schema\Table;
  28. class MySQLMigrator extends Migrator {
  29. /**
  30. * @param Schema $targetSchema
  31. * @param \Doctrine\DBAL\Connection $connection
  32. * @return \Doctrine\DBAL\Schema\SchemaDiff
  33. */
  34. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
  35. $platform = $connection->getDatabasePlatform();
  36. $platform->registerDoctrineTypeMapping('enum', 'string');
  37. $platform->registerDoctrineTypeMapping('bit', 'string');
  38. $schemaDiff = parent::getDiff($targetSchema, $connection);
  39. // identifiers need to be quoted for mysql
  40. foreach ($schemaDiff->changedTables as $tableDiff) {
  41. $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name);
  42. foreach ($tableDiff->changedColumns as $column) {
  43. $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName);
  44. }
  45. }
  46. return $schemaDiff;
  47. }
  48. /**
  49. * Speed up migration test by disabling autocommit and unique indexes check
  50. *
  51. * @param \Doctrine\DBAL\Schema\Table $table
  52. * @throws \OC\DB\MigrationException
  53. */
  54. protected function checkTableMigrate(Table $table) {
  55. $this->connection->exec('SET autocommit=0');
  56. $this->connection->exec('SET unique_checks=0');
  57. try {
  58. parent::checkTableMigrate($table);
  59. } catch (\Exception $e) {
  60. $this->connection->exec('SET unique_checks=1');
  61. $this->connection->exec('SET autocommit=1');
  62. throw new MigrationException($table->getName(), $e->getMessage());
  63. }
  64. $this->connection->exec('SET unique_checks=1');
  65. $this->connection->exec('SET autocommit=1');
  66. }
  67. }