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.

101 lines
2.9 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Repair;
  25. use Doctrine\DBAL\Platforms\SqlitePlatform;
  26. use Doctrine\DBAL\Schema\ColumnDiff;
  27. use Doctrine\DBAL\Schema\SchemaDiff;
  28. use Doctrine\DBAL\Schema\SchemaException;
  29. use Doctrine\DBAL\Schema\TableDiff;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. /**
  33. * Fixes Sqlite autoincrement by forcing the SQLite table schemas to be
  34. * altered in order to retrigger SQL schema generation through OCSqlitePlatform.
  35. */
  36. class SqliteAutoincrement implements IRepairStep {
  37. /**
  38. * @var \OC\DB\Connection
  39. */
  40. protected $connection;
  41. /**
  42. * @param \OC\DB\Connection $connection
  43. */
  44. public function __construct($connection) {
  45. $this->connection = $connection;
  46. }
  47. public function getName() {
  48. return 'Repair SQLite autoincrement';
  49. }
  50. /**
  51. * Fix mime types
  52. */
  53. public function run(IOutput $out) {
  54. if (!$this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
  55. return;
  56. }
  57. $sourceSchema = $this->connection->getSchemaManager()->createSchema();
  58. $schemaDiff = new SchemaDiff();
  59. foreach ($sourceSchema->getTables() as $tableSchema) {
  60. $primaryKey = $tableSchema->getPrimaryKey();
  61. if (!$primaryKey) {
  62. continue;
  63. }
  64. $columnNames = $primaryKey->getColumns();
  65. // add a column diff for every primary key column,
  66. // but do not actually change anything, this will
  67. // force the generation of SQL statements to alter
  68. // those tables, which will then trigger the
  69. // specific SQL code from OCSqlitePlatform
  70. try {
  71. $tableDiff = new TableDiff($tableSchema->getName());
  72. $tableDiff->fromTable = $tableSchema;
  73. foreach ($columnNames as $columnName) {
  74. $columnSchema = $tableSchema->getColumn($columnName);
  75. $columnDiff = new ColumnDiff($columnSchema->getName(), $columnSchema);
  76. $tableDiff->changedColumns[] = $columnDiff;
  77. $schemaDiff->changedTables[] = $tableDiff;
  78. }
  79. } catch (SchemaException $e) {
  80. // ignore
  81. }
  82. }
  83. $this->connection->beginTransaction();
  84. foreach ($schemaDiff->toSql($this->connection->getDatabasePlatform()) as $sql) {
  85. $this->connection->query($sql);
  86. }
  87. $this->connection->commit();
  88. }
  89. }