SqliteAutoincrement.php 2.9 KB

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