Skip to content

Commit 68a4285

Browse files
committed
Add migration to move over existing storage ids
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 71d59bc commit 68a4285

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

apps/files_external/appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This application enables administrators to configure connections to external sto
99

1010
External storage can be configured using the GUI or at the command line. This second option provides the advanced user with more flexibility for configuring bulk external storage mounts and setting mount priorities. More information is available in the external storage GUI documentation and the external storage Configuration File documentation.
1111
</description>
12-
<version>1.11.1</version>
12+
<version>1.11.2</version>
1313
<licence>agpl</licence>
1414
<author>Robin Appelman</author>
1515
<author>Michael Gapczynski</author>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
7+
*
8+
* @author Julius Härtl <jus@bitgrid.net>
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 Doctrine\DBAL\DBALException;
30+
use Doctrine\DBAL\Driver\ResultStatement;
31+
use OC\Files\Cache\Storage;
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 ResultStatement) {
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 (DBALException $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+
* @return ResultStatement
84+
*/
85+
private function getS3Mounts() {
86+
$qb = $this->connection->getQueryBuilder();
87+
$qb->select('m.mount_id')
88+
->selectAlias('c.value', 'bucket')
89+
->from('external_mounts', 'm')
90+
->innerJoin('m', 'external_config', 'c', 'c.mount_id = m.mount_id')
91+
->where($qb->expr()->eq('m.storage_backend', $qb->createPositionalParameter('amazons3')))
92+
->andWhere($qb->expr()->eq('c.key', $qb->createPositionalParameter('bucket')));
93+
return $qb->execute();
94+
}
95+
96+
/**
97+
* @throws DBALException
98+
*/
99+
private function getStorageConfig(int $mountId): array {
100+
$qb = $this->connection->getQueryBuilder();
101+
$qb->select('key', 'value')
102+
->from('external_config')
103+
->where($qb->expr()->eq('mount_id', $qb->createPositionalParameter($mountId)));
104+
$config = [];
105+
foreach ($qb->execute()->fetchAll() as $row) {
106+
$config[$row['key']] = $row['value'];
107+
}
108+
return $config;
109+
}
110+
}

0 commit comments

Comments
 (0)