|
| 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