You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MySQLMigrator.php 2.5KB

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