1
0

Version20000Date20201109081918.php 3.2 KB

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