Skip to content

Commit 7bb4903

Browse files
juliusknorrbackportbot[bot]
authored andcommitted
Add migration to move over existing storage ids
Signed-off-by: Julius Härtl <[email protected]>
1 parent 2e804fb commit 7bb4903

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)