From 107a974b3649b04a1a1a40db2536a0502ae682b9 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Fri, 7 Oct 2022 02:20:43 +0000 Subject: [PATCH 1/7] Fix missing background on upgrade Signed-off-by: Christopher Ng --- .../Version25000Date20221007010957.php | 65 +++++++++++++++++++ lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + version.php | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 core/Migrations/Version25000Date20221007010957.php diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php new file mode 100644 index 0000000000000..5c069de35a1a8 --- /dev/null +++ b/core/Migrations/Version25000Date20221007010957.php @@ -0,0 +1,65 @@ + + * + * @author Christopher Ng + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\Core\Migrations; + +use Closure; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * User background settings handling was moved from the + * dashboard app to the theming app so we migrate the + * respective preference values here + * + */ +class Version25000Date20221007010957 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + $qb = $this->connection->getQueryBuilder(); + + $qb->update('preferences') + ->set('appid', 'theming') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('dashboard'))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('background'))) + ->orWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion'))); + + $qb->executeStatement(); + } +} diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9cdd19d72334d..1b721a2cc33e9 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1062,6 +1062,7 @@ 'OC\\Core\\Migrations\\Version24000Date20220425072957' => $baseDir . '/core/Migrations/Version24000Date20220425072957.php', 'OC\\Core\\Migrations\\Version25000Date20220515204012' => $baseDir . '/core/Migrations/Version25000Date20220515204012.php', 'OC\\Core\\Migrations\\Version25000Date20220602190540' => $baseDir . '/core/Migrations/Version25000Date20220602190540.php', + 'OC\\Core\\Migrations\\Version25000Date20221007010957' => $baseDir . '/core/Migrations/Version25000Date20221007010957.php', 'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php', 'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php', 'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index fc636f312b5b1..4539f89f206d7 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1095,6 +1095,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Migrations\\Version24000Date20220425072957' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20220425072957.php', 'OC\\Core\\Migrations\\Version25000Date20220515204012' => __DIR__ . '/../../..' . '/core/Migrations/Version25000Date20220515204012.php', 'OC\\Core\\Migrations\\Version25000Date20220602190540' => __DIR__ . '/../../..' . '/core/Migrations/Version25000Date20220602190540.php', + 'OC\\Core\\Migrations\\Version25000Date20221007010957' => __DIR__ . '/../../..' . '/core/Migrations/Version25000Date20221007010957.php', 'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php', 'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php', 'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php', diff --git a/version.php b/version.php index c61835ff03d97..e747f14b1803e 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ // between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel // when updating major/minor version number. -$OC_Version = [26, 0, 0, 0]; +$OC_Version = [26, 0, 0, 1]; // The human readable string $OC_VersionString = '26.0.0 dev'; From 6e4a90ba36e158ef0aa52f1ee085f43383dd9dc4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 7 Oct 2022 11:17:36 +0200 Subject: [PATCH 2/7] Fix SQL errors Signed-off-by: Joas Schilling --- core/Migrations/Version25000Date20221007010957.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php index 5c069de35a1a8..2c18d180dd702 100644 --- a/core/Migrations/Version25000Date20221007010957.php +++ b/core/Migrations/Version25000Date20221007010957.php @@ -55,10 +55,12 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $qb = $this->connection->getQueryBuilder(); $qb->update('preferences') - ->set('appid', 'theming') + ->set('appid', $qb->createNamedParameter('theming')) ->where($qb->expr()->eq('appid', $qb->createNamedParameter('dashboard'))) - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('background'))) - ->orWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion'))); + ->andWhere($qb->expr()->orX( + $qb->expr()->eq('configkey', $qb->createNamedParameter('background')), + $qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion')) + )); $qb->executeStatement(); } From 7599cbb529b08b952f0968b2d3b6c7e48b1ce7a6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 7 Oct 2022 11:20:48 +0200 Subject: [PATCH 3/7] New code being new Signed-off-by: Joas Schilling --- core/Migrations/Version25000Date20221007010957.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php index 2c18d180dd702..ac33b608ace6d 100644 --- a/core/Migrations/Version25000Date20221007010957.php +++ b/core/Migrations/Version25000Date20221007010957.php @@ -38,9 +38,7 @@ * */ class Version25000Date20221007010957 extends SimpleMigrationStep { - - /** @var IDBConnection */ - protected $connection; + protected IDBConnection $connection; public function __construct(IDBConnection $connection) { $this->connection = $connection; From 7f5ef4dcf751bcd5f17dddf140d349d645b24cba Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Thu, 13 Oct 2022 00:19:00 +0000 Subject: [PATCH 4/7] Delete theming background preferences Signed-off-by: Christopher Ng --- core/Migrations/Version25000Date20221007010957.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php index ac33b608ace6d..5e4a9854382a7 100644 --- a/core/Migrations/Version25000Date20221007010957.php +++ b/core/Migrations/Version25000Date20221007010957.php @@ -52,13 +52,19 @@ public function __construct(IDBConnection $connection) { public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { $qb = $this->connection->getQueryBuilder(); + $orExpr = $qb->expr()->orX( + $qb->expr()->eq('configkey', $qb->createNamedParameter('background')), + $qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion')), + ); + + $qb->delete('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('theming'))) + ->andWhere($orExpr); + $qb->update('preferences') ->set('appid', $qb->createNamedParameter('theming')) ->where($qb->expr()->eq('appid', $qb->createNamedParameter('dashboard'))) - ->andWhere($qb->expr()->orX( - $qb->expr()->eq('configkey', $qb->createNamedParameter('background')), - $qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion')) - )); + ->andWhere($orExpr); $qb->executeStatement(); } From b4f6329a8390650f21119893261b69b965ae795e Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 13 Oct 2022 10:29:37 +0200 Subject: [PATCH 5/7] fix querybuilder instance recuse ...and execute delete query Signed-off-by: Arthur Schiwon --- .../Version25000Date20221007010957.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php index 5e4a9854382a7..18d769ab73584 100644 --- a/core/Migrations/Version25000Date20221007010957.php +++ b/core/Migrations/Version25000Date20221007010957.php @@ -50,22 +50,24 @@ public function __construct(IDBConnection $connection) { * @param array $options */ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { - $qb = $this->connection->getQueryBuilder(); + $cleanUpQuery = $this->connection->getQueryBuilder(); - $orExpr = $qb->expr()->orX( - $qb->expr()->eq('configkey', $qb->createNamedParameter('background')), - $qb->expr()->eq('configkey', $qb->createNamedParameter('backgroundVersion')), + $orExpr = $cleanUpQuery->expr()->orX( + $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('background')), + $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('backgroundVersion')), ); - $qb->delete('preferences') - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('theming'))) + $cleanUpQuery->delete('preferences') + ->where($cleanUpQuery->expr()->eq('appid', $cleanUpQuery->createNamedParameter('theming'))) ->andWhere($orExpr); + $cleanUpQuery->executeStatement(); - $qb->update('preferences') - ->set('appid', $qb->createNamedParameter('theming')) - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('dashboard'))) + $updateQuery = $this->connection->getQueryBuilder(); + $updateQuery->update('preferences') + ->set('appid', $updateQuery->createNamedParameter('theming')) + ->where($updateQuery->expr()->eq('appid', $updateQuery->createNamedParameter('dashboard'))) ->andWhere($orExpr); - $qb->executeStatement(); + $updateQuery->executeStatement(); } } From 4d98128e9a4c170efb7ae1e5e063df14844964a2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 13 Oct 2022 10:36:05 +0200 Subject: [PATCH 6/7] Fix migration parameter handling Signed-off-by: Joas Schilling --- .../Version25000Date20221007010957.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/core/Migrations/Version25000Date20221007010957.php b/core/Migrations/Version25000Date20221007010957.php index 18d769ab73584..21c62acae214e 100644 --- a/core/Migrations/Version25000Date20221007010957.php +++ b/core/Migrations/Version25000Date20221007010957.php @@ -51,23 +51,22 @@ public function __construct(IDBConnection $connection) { */ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { $cleanUpQuery = $this->connection->getQueryBuilder(); - - $orExpr = $cleanUpQuery->expr()->orX( - $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('background')), - $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('backgroundVersion')), - ); - $cleanUpQuery->delete('preferences') ->where($cleanUpQuery->expr()->eq('appid', $cleanUpQuery->createNamedParameter('theming'))) - ->andWhere($orExpr); + ->andWhere($cleanUpQuery->expr()->orX( + $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('background')), + $cleanUpQuery->expr()->eq('configkey', $cleanUpQuery->createNamedParameter('backgroundVersion')), + )); $cleanUpQuery->executeStatement(); $updateQuery = $this->connection->getQueryBuilder(); $updateQuery->update('preferences') ->set('appid', $updateQuery->createNamedParameter('theming')) ->where($updateQuery->expr()->eq('appid', $updateQuery->createNamedParameter('dashboard'))) - ->andWhere($orExpr); - + ->andWhere($updateQuery->expr()->orX( + $updateQuery->expr()->eq('configkey', $updateQuery->createNamedParameter('background')), + $updateQuery->expr()->eq('configkey', $updateQuery->createNamedParameter('backgroundVersion')), + )); $updateQuery->executeStatement(); } } From 59e07102ed2cb54e5122d8c5ca05d3fa11dbc176 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 13 Oct 2022 15:40:47 +0200 Subject: [PATCH 7/7] graceful background image handling - fallback to background image from old location - migrate background images to new location as insensitive job Signed-off-by: Arthur Schiwon --- apps/theming/appinfo/info.xml | 3 + .../lib/Jobs/MigrateBackgroundImages.php | 107 ++++++++++++++++++ .../InitBackgroundImagesMigration.php | 48 ++++++++ .../theming/lib/Service/BackgroundService.php | 18 ++- 4 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 apps/theming/lib/Jobs/MigrateBackgroundImages.php create mode 100644 apps/theming/lib/Migration/InitBackgroundImagesMigration.php diff --git a/apps/theming/appinfo/info.xml b/apps/theming/appinfo/info.xml index fcdaade0fd5f2..402ea9f0ba218 100644 --- a/apps/theming/appinfo/info.xml +++ b/apps/theming/appinfo/info.xml @@ -31,6 +31,9 @@ OCA\Theming\Migration\MigrateUserConfig + + OCA\Theming\Migration\InitBackgroundImagesMigration + diff --git a/apps/theming/lib/Jobs/MigrateBackgroundImages.php b/apps/theming/lib/Jobs/MigrateBackgroundImages.php new file mode 100644 index 0000000000000..97806fa600ac9 --- /dev/null +++ b/apps/theming/lib/Jobs/MigrateBackgroundImages.php @@ -0,0 +1,107 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Theming\Jobs; + +use OCA\Theming\AppInfo\Application; +use OCP\App\IAppManager; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJobList; +use OCP\BackgroundJob\QueuedJob; +use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IConfig; + +class MigrateBackgroundImages extends QueuedJob { + public const TIME_SENSITIVE = 0; + + private IConfig $config; + private IAppManager $appManager; + private IAppDataFactory $appDataFactory; + private IJobList $jobList; + + public function __construct(ITimeFactory $time, IAppDataFactory $appDataFactory, IConfig $config, IAppManager $appManager, IJobList $jobList) { + parent::__construct($time); + $this->config = $config; + $this->appManager = $appManager; + $this->appDataFactory = $appDataFactory; + $this->jobList = $jobList; + } + + protected function run($argument): void { + if (!$this->appManager->isEnabledForUser('dashboard')) { + return; + } + + $themingData = $this->appDataFactory->get(Application::APP_ID); + $dashboardData = $this->appDataFactory->get('dashboard'); + + $userIds = $this->config->getUsersForUserValue('theming', 'background', 'custom'); + + $notSoFastMode = \count($userIds) > 5000; + $reTrigger = false; + $processed = 0; + + foreach ($userIds as $userId) { + try { + // precondition + if ($notSoFastMode) { + if ($this->config->getUserValue($userId, 'theming', 'background-migrated', '0') === '1') { + // already migrated + continue; + } + $reTrigger = true; + } + + // migration + $file = $dashboardData->getFolder($userId)->getFile('background.jpg'); + try { + $targetDir = $themingData->getFolder($userId); + } catch (NotFoundException $e) { + $targetDir = $themingData->newFolder($userId); + } + if (!$targetDir->fileExists('background.jpg')) { + $targetDir->newFile('background.jpg', $file->getContent()); + } + $file->delete(); + } catch (NotFoundException|NotPermittedException $e) { + } + // capture state + if ($notSoFastMode) { + $this->config->setUserValue($userId, 'theming', 'background-migrated', '1'); + $processed++; + } + if ($processed > 4999) { + break; + } + } + + if ($reTrigger) { + $this->jobList->add(self::class); + } + } +} diff --git a/apps/theming/lib/Migration/InitBackgroundImagesMigration.php b/apps/theming/lib/Migration/InitBackgroundImagesMigration.php new file mode 100644 index 0000000000000..c23a9176843b1 --- /dev/null +++ b/apps/theming/lib/Migration/InitBackgroundImagesMigration.php @@ -0,0 +1,48 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Theming\Migration; + +use OCA\Theming\Jobs\MigrateBackgroundImages; +use OCP\BackgroundJob\IJobList; +use OCP\Migration\IOutput; + +class InitBackgroundImagesMigration implements \OCP\Migration\IRepairStep { + + private IJobList $jobList; + + public function __construct(IJobList $jobList) { + $this->jobList = $jobList; + } + + public function getName() { + return 'Initialize migration of background images from dashboard to theming app'; + } + + public function run(IOutput $output) { + $this->jobList->add(MigrateBackgroundImages::class); + } +} diff --git a/apps/theming/lib/Service/BackgroundService.php b/apps/theming/lib/Service/BackgroundService.php index 0614fe0035767..36623735728ba 100644 --- a/apps/theming/lib/Service/BackgroundService.php +++ b/apps/theming/lib/Service/BackgroundService.php @@ -30,6 +30,7 @@ use InvalidArgumentException; use OC\User\NoUserException; use OCA\Theming\AppInfo\Application; +use OCP\Files\AppData\IAppDataFactory; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\IRootFolder; @@ -133,20 +134,22 @@ class BackgroundService { private IAppData $appData; private IConfig $config; private string $userId; + private IAppDataFactory $appDataFactory; public function __construct( - IRootFolder $rootFolder, - IAppData $appData, - IConfig $config, - ?string $userId + IRootFolder $rootFolder, + IAppDataFactory $appDataFactory, + IConfig $config, + ?string $userId ) { if ($userId === null) { return; } $this->rootFolder = $rootFolder; - $this->appData = $appData; + $this->appData = $appDataFactory->get(Application::APP_ID); $this->config = $config; $this->userId = $userId; + $this->appDataFactory = $appDataFactory; } public function setDefaultBackground(): void { @@ -193,6 +196,11 @@ public function getBackground(): ?ISimpleFile { try { return $this->getAppDataFolder()->getFile('background.jpg'); } catch (NotFoundException | NotPermittedException $e) { + try { + // Fallback can be removed in 26 + $dashboardFolder = $this->appDataFactory->get('dashboard'); + return $dashboardFolder->getFolder($this->userId)->getFile('background.jpg'); + } catch (\Throwable $t) {} } } return null;