Skip to content

Commit fc30176

Browse files
committed
feat: add pre-migrate command
Signed-off-by: Kent Delante <[email protected]>
1 parent 32cb3bb commit fc30176

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

apps/files/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<command>OCA\Files\Command\Object\Put</command>
4747
<command>OCA\Files\Command\Object\Multi\Users</command>
4848
<command>OCA\Files\Command\Object\Multi\Move</command>
49+
<command>OCA\Files\Command\Object\Multi\PreMigrate</command>
4950
</commands>
5051

5152
<activity>

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'OCA\\Files\\Command\\Object\\Delete' => $baseDir . '/../lib/Command/Object/Delete.php',
3636
'OCA\\Files\\Command\\Object\\Get' => $baseDir . '/../lib/Command/Object/Get.php',
3737
'OCA\\Files\\Command\\Object\\Multi\\Move' => $baseDir . '/../lib/Command/Object/Multi/Move.php',
38+
'OCA\\Files\\Command\\Object\\Multi\\PreMigrate' => $baseDir . '/../lib/Command/Object/Multi/PreMigrate.php',
3839
'OCA\\Files\\Command\\Object\\Multi\\Users' => $baseDir . '/../lib/Command/Object/Multi/Users.php',
3940
'OCA\\Files\\Command\\Object\\ObjectUtil' => $baseDir . '/../lib/Command/Object/ObjectUtil.php',
4041
'OCA\\Files\\Command\\Object\\Put' => $baseDir . '/../lib/Command/Object/Put.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ComposerStaticInitFiles
5050
'OCA\\Files\\Command\\Object\\Delete' => __DIR__ . '/..' . '/../lib/Command/Object/Delete.php',
5151
'OCA\\Files\\Command\\Object\\Get' => __DIR__ . '/..' . '/../lib/Command/Object/Get.php',
5252
'OCA\\Files\\Command\\Object\\Multi\\Move' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/Move.php',
53+
'OCA\\Files\\Command\\Object\\Multi\\PreMigrate' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/PreMigrate.php',
5354
'OCA\\Files\\Command\\Object\\Multi\\Users' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/Users.php',
5455
'OCA\\Files\\Command\\Object\\ObjectUtil' => __DIR__ . '/..' . '/../lib/Command/Object/ObjectUtil.php',
5556
'OCA\\Files\\Command\\Object\\Put' => __DIR__ . '/..' . '/../lib/Command/Object/Put.php',
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
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\IUserManager;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class PreMigrate extends Base {
20+
public function __construct(
21+
private PrimaryObjectStoreConfig $objectStoreConfig,
22+
private IUserManager $userManager,
23+
private IConfig $config,
24+
) {
25+
parent::__construct();
26+
}
27+
28+
protected function configure(): void {
29+
parent::configure();
30+
$this
31+
->setName('files:object:multi:pre-migrate')
32+
->setDescription('Assign a configured object store to users who don\'t have one assigned yet.')
33+
->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'The name of the configured object store')
34+
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The userId of the user to assign the object store')
35+
->addOption('all', 'a', InputOption::VALUE_NONE, 'Assign the object store to all users');
36+
}
37+
38+
public function execute(InputInterface $input, OutputInterface $output): int {
39+
$objectStore = $input->getOption('object-store');
40+
if (!$objectStore) {
41+
$output->writeln('Please specify the object store');
42+
return 1;
43+
}
44+
45+
$configs = $this->objectStoreConfig->getObjectStoreConfigs();
46+
if (!isset($configs[$objectStore])) {
47+
$output->writeln('<error>Unknown object store configuration: ' . $objectStore . '</error>');
48+
return 1;
49+
}
50+
51+
if ($input->getOption('all')) {
52+
$users = $this->userManager->getSeenUsers();
53+
} elseif ($userId = $input->getOption('user')) {
54+
$user = $this->userManager->get($userId);
55+
if (!$user) {
56+
$output->writeln('<error>User ' . $userId . ' not found</error>');
57+
return 1;
58+
}
59+
$users = new \ArrayIterator([$user]);
60+
} else {
61+
$output->writeln('<comment>Please specify a user id with --user or --all for all users</comment>');
62+
return 1;
63+
}
64+
65+
$count = 0;
66+
foreach ($users as $user) {
67+
if (!$this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', null)) {
68+
$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'objectstore', $objectStore);
69+
$count++;
70+
}
71+
}
72+
$output->writeln('Assigned object store <info>' . $objectStore . '</info> to <info>' . $count . '</info> users');
73+
74+
return 0;
75+
}
76+
}

0 commit comments

Comments
 (0)