Version20000Date20201109081918.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Migrations;
  25. use Closure;
  26. use Doctrine\DBAL\Types\Type;
  27. use OCP\DB\ISchemaWrapper;
  28. use OCP\IDBConnection;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\SimpleMigrationStep;
  31. class Version20000Date20201109081918 extends SimpleMigrationStep {
  32. /** @var IDBConnection */
  33. protected $connection;
  34. public function __construct(IDBConnection $connection) {
  35. $this->connection = $connection;
  36. }
  37. /**
  38. * @param IOutput $output
  39. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  40. * @param array $options
  41. * @return null|ISchemaWrapper
  42. */
  43. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  44. /** @var ISchemaWrapper $schema */
  45. $schema = $schemaClosure();
  46. if (!$schema->hasTable('storages_credentials')) {
  47. $table = $schema->createTable('storages_credentials');
  48. $table->addColumn('id', Type::BIGINT, [
  49. 'autoincrement' => true,
  50. 'notnull' => true,
  51. 'length' => 64,
  52. ]);
  53. $table->addColumn('user', Type::STRING, [
  54. 'notnull' => false,
  55. 'length' => 64,
  56. ]);
  57. $table->addColumn('identifier', Type::STRING, [
  58. 'notnull' => true,
  59. 'length' => 64,
  60. ]);
  61. $table->addColumn('credentials', Type::TEXT, [
  62. 'notnull' => false,
  63. ]);
  64. $table->setPrimaryKey(['id']);
  65. $table->addUniqueIndex(['user', 'identifier'], 'stocred_ui');
  66. $table->addIndex(['user'], 'stocred_user');
  67. }
  68. return $schema;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. *
  73. * @since 13.0.0
  74. */
  75. public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
  76. if (!$this->connection->tableExists('credentials')) {
  77. return;
  78. }
  79. $query = $this->connection->getQueryBuilder();
  80. $query->select('*')
  81. ->from('credentials');
  82. $insert = $this->connection->getQueryBuilder();
  83. $insert->insert('storages_credentials')
  84. ->setValue('user', $insert->createParameter('user'))
  85. ->setValue('identifier', $insert->createParameter('identifier'))
  86. ->setValue('credentials', $insert->createParameter('credentials'));
  87. $result = $query->execute();
  88. while ($row = $result->fetch()) {
  89. $insert->setParameter('user', (string) $row['user'])
  90. ->setParameter('identifier', (string) $row['identifier'])
  91. ->setParameter('credentials', (string) $row['credentials']);
  92. $insert->execute();
  93. }
  94. $result->closeCursor();
  95. }
  96. }