1
0

Version1015Date20211104103506.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  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 OCA\Files_External\Migration;
  25. use Closure;
  26. use OC\Files\Cache\Storage;
  27. use OCP\DB\Exception;
  28. use OCP\DB\IResult;
  29. use OCP\DB\ISchemaWrapper;
  30. use OCP\IDBConnection;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\SimpleMigrationStep;
  33. use Psr\Log\LoggerInterface;
  34. class Version1015Date20211104103506 extends SimpleMigrationStep {
  35. /** @var IDBConnection */
  36. private $connection;
  37. /** @var LoggerInterface */
  38. private $logger;
  39. public function __construct(IDBConnection $connection, LoggerInterface $logger) {
  40. $this->connection = $connection;
  41. $this->logger = $logger;
  42. }
  43. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  44. $qb = $this->connection->getQueryBuilder();
  45. $qb->update('storages')
  46. ->set('id', $qb->createParameter('newId'))
  47. ->where($qb->expr()->eq('id', $qb->createParameter('oldId')));
  48. $mounts = $this->getS3Mounts();
  49. if (!$mounts instanceof IResult) {
  50. throw new \Exception('Could not fetch existing mounts for migration');
  51. }
  52. while ($mount = $mounts->fetch()) {
  53. $config = $this->getStorageConfig((int)$mount['mount_id']);
  54. $hostname = $config['hostname'];
  55. $bucket = $config['bucket'];
  56. $key = $config['key'];
  57. $oldId = Storage::adjustStorageId('amazon::' . $bucket);
  58. $newId = Storage::adjustStorageId('amazon::external::' . md5($hostname . ':' . $bucket . ':' . $key));
  59. try {
  60. $qb->setParameter('oldId', $oldId);
  61. $qb->setParameter('newId', $newId);
  62. $qb->execute();
  63. $this->logger->info('Migrated s3 storage id for mount with id ' . $mount['mount_id'] . ' to ' . $newId);
  64. } catch (Exception $e) {
  65. $this->logger->error('Failed to migrate external s3 storage id for mount with id ' . $mount['mount_id'], [
  66. 'exception' => $e
  67. ]);
  68. }
  69. }
  70. return null;
  71. }
  72. /**
  73. * @throws Exception
  74. * @return \OCP\DB\IResult|int
  75. */
  76. private function getS3Mounts() {
  77. $qb = $this->connection->getQueryBuilder();
  78. $qb->select('m.mount_id')
  79. ->selectAlias('c.value', 'bucket')
  80. ->from('external_mounts', 'm')
  81. ->innerJoin('m', 'external_config', 'c', 'c.mount_id = m.mount_id')
  82. ->where($qb->expr()->eq('m.storage_backend', $qb->createPositionalParameter('amazons3')))
  83. ->andWhere($qb->expr()->eq('c.key', $qb->createPositionalParameter('bucket')));
  84. return $qb->execute();
  85. }
  86. /**
  87. * @throws Exception
  88. */
  89. private function getStorageConfig(int $mountId): array {
  90. $qb = $this->connection->getQueryBuilder();
  91. $qb->select('key', 'value')
  92. ->from('external_config')
  93. ->where($qb->expr()->eq('mount_id', $qb->createPositionalParameter($mountId)));
  94. $config = [];
  95. foreach ($qb->execute()->fetchAll() as $row) {
  96. $config[$row['key']] = $row['value'];
  97. }
  98. return $config;
  99. }
  100. }