Skip to content

Commit 0183457

Browse files
icewind1991backportbot[bot]
authored andcommitted
feat: multi object store rename command
Signed-off-by: Robin Appelman <[email protected]> [skip ci]
1 parent 423141d commit 0183457

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

apps/files/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<command>OCA\Files\Command\Object\ListObject</command>
5555
<command>OCA\Files\Command\Object\Orphans</command>
5656
<command>OCA\Files\Command\Object\Multi\Users</command>
57+
<command>OCA\Files\Command\Object\Multi\Rename</command>
5758
<command>OCA\Files\Command\WindowsCompatibleFilenames</command>
5859
</commands>
5960

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'OCA\\Files\\Command\\Object\\Get' => $baseDir . '/../lib/Command/Object/Get.php',
3737
'OCA\\Files\\Command\\Object\\Info' => $baseDir . '/../lib/Command/Object/Info.php',
3838
'OCA\\Files\\Command\\Object\\ListObject' => $baseDir . '/../lib/Command/Object/ListObject.php',
39+
'OCA\\Files\\Command\\Object\\Multi\\Rename' => $baseDir . '/../lib/Command/Object/Multi/Rename.php',
3940
'OCA\\Files\\Command\\Object\\Multi\\Users' => $baseDir . '/../lib/Command/Object/Multi/Users.php',
4041
'OCA\\Files\\Command\\Object\\ObjectUtil' => $baseDir . '/../lib/Command/Object/ObjectUtil.php',
4142
'OCA\\Files\\Command\\Object\\Orphans' => $baseDir . '/../lib/Command/Object/Orphans.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ComposerStaticInitFiles
5151
'OCA\\Files\\Command\\Object\\Get' => __DIR__ . '/..' . '/../lib/Command/Object/Get.php',
5252
'OCA\\Files\\Command\\Object\\Info' => __DIR__ . '/..' . '/../lib/Command/Object/Info.php',
5353
'OCA\\Files\\Command\\Object\\ListObject' => __DIR__ . '/..' . '/../lib/Command/Object/ListObject.php',
54+
'OCA\\Files\\Command\\Object\\Multi\\Rename' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/Rename.php',
5455
'OCA\\Files\\Command\\Object\\Multi\\Users' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/Users.php',
5556
'OCA\\Files\\Command\\Object\\ObjectUtil' => __DIR__ . '/..' . '/../lib/Command/Object/ObjectUtil.php',
5657
'OCA\\Files\\Command\\Object\\Orphans' => __DIR__ . '/..' . '/../lib/Command/Object/Orphans.php',
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]>
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Files\Command\Object\Multi;
10+
11+
use OC\Core\Command\Base;
12+
use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
13+
use OCP\IConfig;
14+
use OCP\IDBConnection;
15+
use Symfony\Component\Console\Helper\QuestionHelper;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Question\ConfirmationQuestion;
20+
21+
class Rename extends Base {
22+
public function __construct(
23+
private readonly IDBConnection $connection,
24+
private readonly PrimaryObjectStoreConfig $objectStoreConfig,
25+
private readonly IConfig $config,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
protected function configure(): void {
31+
parent::configure();
32+
$this
33+
->setName('files:object:multi:rename-config')
34+
->setDescription('Rename an object store configuration and move all users over to the new configuration,')
35+
->addArgument('source', InputArgument::REQUIRED, 'Object store configuration to rename')
36+
->addArgument('target', InputArgument::REQUIRED, 'New name for the object store configuration');
37+
}
38+
39+
public function execute(InputInterface $input, OutputInterface $output): int {
40+
$source = $input->getArgument('source');
41+
$target = $input->getArgument('target');
42+
43+
$configs = $this->objectStoreConfig->getObjectStoreConfigs();
44+
if (!isset($configs[$source])) {
45+
$output->writeln('<error>Unknown object store configuration: ' . $source . '</error>');
46+
return 1;
47+
}
48+
49+
if ($source === 'root') {
50+
$output->writeln('<error>Renaming the root configuration is not supported.</error>');
51+
return 1;
52+
}
53+
54+
if ($source === 'default') {
55+
$output->writeln('<error>Renaming the default configuration is not supported.</error>');
56+
return 1;
57+
}
58+
59+
if (!isset($configs[$target])) {
60+
$output->writeln('<comment>Target object store configuration ' . $target . ' doesn\'t exist yet.</comment>');
61+
$output->writeln('The target configuration can be created automatically.');
62+
$output->writeln('However, as this depends on modifying the config.php, this only works as long as the instance runs on a single node or all nodes in a clustered setup have a shared config file (such as from a shared network mount).');
63+
$output->writeln('If the different nodes have a separate copy of the config.php file, the automatic object store configuration creation will lead to the configuration going out of sync.');
64+
$output->writeln('If these requirements are not met, you can manually create the target object store configuration in each node\'s configuration before running the command.');
65+
$output->writeln('');
66+
$output->writeln('<error>Failure to check these requirements will lead to data loss for users.</error>');
67+
68+
/** @var QuestionHelper $helper */
69+
$helper = $this->getHelper('question');
70+
$question = new ConfirmationQuestion('Automatically create target object store configuration? [y/N] ', false);
71+
if ($helper->ask($input, $output, $question)) {
72+
$configs[$target] = $configs[$source];
73+
74+
// update all aliases
75+
foreach ($configs as &$config) {
76+
if ($config === $source) {
77+
$config = $target;
78+
}
79+
}
80+
$this->config->setSystemValue('objectstore', $configs);
81+
} else {
82+
return 0;
83+
}
84+
} elseif (($configs[$source] !== $configs[$target]) || $configs[$source] !== $target) {
85+
$output->writeln('<error>Source and target configuration differ.</error>');
86+
$output->writeln('');
87+
$output->writeln('To ensure proper migration of users, the source and target configuration must be the same to ensure that the objects for the moved users exist on the target configuration.');
88+
$output->writeln('The usual migration process consists of creating a clone of the old configuration, moving the users from the old configuration to the new one, and then adjust the old configuration that is longer used.');
89+
return 1;
90+
}
91+
92+
$query = $this->connection->getQueryBuilder();
93+
$query->update('preferences')
94+
->set('configvalue', $query->createNamedParameter($target))
95+
->where($query->expr()->eq('appid', $query->createNamedParameter('homeobjectstore')))
96+
->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('objectstore')))
97+
->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($source)));
98+
$count = $query->executeStatement();
99+
100+
if ($count > 0) {
101+
$output->writeln('Moved <info>' . $count . '</info> users');
102+
} else {
103+
$output->writeln('No users moved');
104+
}
105+
106+
return 0;
107+
}
108+
}

0 commit comments

Comments
 (0)