Version20000Date20201109081918.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Migrations;
  8. use Closure;
  9. use OCP\DB\ISchemaWrapper;
  10. use OCP\DB\Types;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\SimpleMigrationStep;
  14. class Version20000Date20201109081918 extends SimpleMigrationStep {
  15. public function __construct(
  16. protected IDBConnection $connection,
  17. ) {
  18. }
  19. /**
  20. * @param IOutput $output
  21. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  22. * @param array $options
  23. * @return null|ISchemaWrapper
  24. */
  25. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  26. /** @var ISchemaWrapper $schema */
  27. $schema = $schemaClosure();
  28. if (!$schema->hasTable('storages_credentials')) {
  29. $table = $schema->createTable('storages_credentials');
  30. $table->addColumn('id', Types::BIGINT, [
  31. 'autoincrement' => true,
  32. 'notnull' => true,
  33. 'length' => 64,
  34. ]);
  35. $table->addColumn('user', Types::STRING, [
  36. 'notnull' => false,
  37. 'length' => 64,
  38. ]);
  39. $table->addColumn('identifier', Types::STRING, [
  40. 'notnull' => true,
  41. 'length' => 64,
  42. ]);
  43. $table->addColumn('credentials', Types::TEXT, [
  44. 'notnull' => false,
  45. ]);
  46. $table->setPrimaryKey(['id']);
  47. $table->addUniqueIndex(['user', 'identifier'], 'stocred_ui');
  48. $table->addIndex(['user'], 'stocred_user');
  49. }
  50. return $schema;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. *
  55. * @since 13.0.0
  56. */
  57. public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
  58. if (!$this->connection->tableExists('credentials')) {
  59. return;
  60. }
  61. $query = $this->connection->getQueryBuilder();
  62. $query->select('*')
  63. ->from('credentials');
  64. $insert = $this->connection->getQueryBuilder();
  65. $insert->insert('storages_credentials')
  66. ->setValue('user', $insert->createParameter('user'))
  67. ->setValue('identifier', $insert->createParameter('identifier'))
  68. ->setValue('credentials', $insert->createParameter('credentials'));
  69. $result = $query->execute();
  70. while ($row = $result->fetch()) {
  71. $insert->setParameter('user', (string)$row['user'])
  72. ->setParameter('identifier', (string)$row['identifier'])
  73. ->setParameter('credentials', (string)$row['credentials']);
  74. $insert->execute();
  75. }
  76. $result->closeCursor();
  77. }
  78. }