aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Repair
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2019-07-08 14:47:26 +0200
committerMorris Jobke <hey@morrisjobke.de>2019-07-08 14:47:26 +0200
commit53d2d95478a2de64778739c7f823f6e01e399d5f (patch)
tree4484c2fc677c018fda1e19be142832ed83f38e2b /lib/private/Repair
parenteb092bbdc74fd10253e7a75850d5725df27daa25 (diff)
downloadnextcloud-server-53d2d95478a2de64778739c7f823f6e01e399d5f.tar.gz
nextcloud-server-53d2d95478a2de64778739c7f823f6e01e399d5f.zip
Remove one time repair steps that have already run when updating to 17
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'lib/private/Repair')
-rw-r--r--lib/private/Repair/NC13/RepairInvalidPaths.php197
-rw-r--r--lib/private/Repair/NC14/RepairPendingCronJobs.php77
-rw-r--r--lib/private/Repair/NC15/SetVcardDatabaseUID.php154
-rw-r--r--lib/private/Repair/RemoveRootShares.php142
4 files changed, 0 insertions, 570 deletions
diff --git a/lib/private/Repair/NC13/RepairInvalidPaths.php b/lib/private/Repair/NC13/RepairInvalidPaths.php
deleted file mode 100644
index 941224012a1..00000000000
--- a/lib/private/Repair/NC13/RepairInvalidPaths.php
+++ /dev/null
@@ -1,197 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
- *
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- *
- * @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 <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Repair\NC13;
-
-
-use OCP\DB\QueryBuilder\IQueryBuilder;
-use OCP\IConfig;
-use OCP\IDBConnection;
-use OCP\Migration\IOutput;
-use OCP\Migration\IRepairStep;
-
-class RepairInvalidPaths implements IRepairStep {
- const MAX_ROWS = 1000;
-
- /** @var IDBConnection */
- private $connection;
- /** @var IConfig */
- private $config;
-
- private $getIdQuery;
- private $updateQuery;
- private $reparentQuery;
- private $deleteQuery;
-
- public function __construct(IDBConnection $connection, IConfig $config) {
- $this->connection = $connection;
- $this->config = $config;
- }
-
-
- public function getName() {
- return 'Repair invalid paths in file cache';
- }
-
- /**
- * @return \Generator
- * @suppress SqlInjectionChecker
- */
- private function getInvalidEntries() {
- $builder = $this->connection->getQueryBuilder();
-
- $computedPath = $builder->func()->concat(
- 'p.path',
- $builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
- );
-
- //select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name;
- $builder->select('f.fileid', 'f.path', 'f.name', 'f.parent', 'f.storage')
- ->selectAlias('p.path', 'parent_path')
- ->selectAlias('p.storage', 'parent_storage')
- ->from('filecache', 'f')
- ->innerJoin('f', 'filecache', 'p', $builder->expr()->andX(
- $builder->expr()->eq('f.parent', 'p.fileid'),
- $builder->expr()->nonEmptyString('p.name')
- ))
- ->where($builder->expr()->neq('f.path', $computedPath))
- ->setMaxResults(self::MAX_ROWS);
-
- do {
- $result = $builder->execute();
- $rows = $result->fetchAll();
- foreach ($rows as $row) {
- yield $row;
- }
- $result->closeCursor();
- } while (count($rows) > 0);
- }
-
- private function getId($storage, $path) {
- if (!$this->getIdQuery) {
- $builder = $this->connection->getQueryBuilder();
-
- $this->getIdQuery = $builder->select('fileid')
- ->from('filecache')
- ->where($builder->expr()->eq('storage', $builder->createParameter('storage')))
- ->andWhere($builder->expr()->eq('path_hash', $builder->createParameter('path_hash')));
- }
-
- $this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT);
- $this->getIdQuery->setParameter('path_hash', md5($path));
-
- return $this->getIdQuery->execute()->fetchColumn();
- }
-
- /**
- * @param string $fileid
- * @param string $newPath
- * @param string $newStorage
- * @suppress SqlInjectionChecker
- */
- private function update($fileid, $newPath, $newStorage) {
- if (!$this->updateQuery) {
- $builder = $this->connection->getQueryBuilder();
-
- $this->updateQuery = $builder->update('filecache')
- ->set('path', $builder->createParameter('newpath'))
- ->set('path_hash', $builder->func()->md5($builder->createParameter('newpath')))
- ->set('storage', $builder->createParameter('newstorage'))
- ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
- }
-
- $this->updateQuery->setParameter('newpath', $newPath);
- $this->updateQuery->setParameter('newstorage', $newStorage);
- $this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
-
- $this->updateQuery->execute();
- }
-
- private function reparent($from, $to) {
- if (!$this->reparentQuery) {
- $builder = $this->connection->getQueryBuilder();
-
- $this->reparentQuery = $builder->update('filecache')
- ->set('parent', $builder->createParameter('to'))
- ->where($builder->expr()->eq('fileid', $builder->createParameter('from')));
- }
-
- $this->reparentQuery->setParameter('from', $from);
- $this->reparentQuery->setParameter('to', $to);
-
- $this->reparentQuery->execute();
- }
-
- private function delete($fileid) {
- if (!$this->deleteQuery) {
- $builder = $this->connection->getQueryBuilder();
-
- $this->deleteQuery = $builder->delete('filecache')
- ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
- }
-
- $this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
-
- $this->deleteQuery->execute();
- }
-
- private function repair() {
- $this->connection->beginTransaction();
- $entries = $this->getInvalidEntries();
- $count = 0;
- foreach ($entries as $entry) {
- $count++;
- $calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
- if ($newId = $this->getId($entry['parent_storage'], $calculatedPath)) {
- // a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
- $this->reparent($entry['fileid'], $newId);
- $this->delete($entry['fileid']);
- } else {
- $this->update($entry['fileid'], $calculatedPath, $entry['parent_storage']);
- }
- }
- $this->connection->commit();
- return $count;
- }
-
- private function shouldRun() {
- $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
-
- // was added to 11.0.5.2, 12.0.0.30 and 13.0.0.1
- $shouldRun = version_compare($versionFromBeforeUpdate, '11.0.5.2', '<');
- $shouldRun |= version_compare($versionFromBeforeUpdate, '12.0.0.0', '>=') && version_compare($versionFromBeforeUpdate, '12.0.0.30', '<');
- $shouldRun |= version_compare($versionFromBeforeUpdate, '13.0.0.0', '==');
- return $shouldRun;
- }
-
- public function run(IOutput $output) {
- if ($this->shouldRun()) {
- $count = $this->repair();
-
- $output->info('Repaired ' . $count . ' paths');
- }
- }
-}
diff --git a/lib/private/Repair/NC14/RepairPendingCronJobs.php b/lib/private/Repair/NC14/RepairPendingCronJobs.php
deleted file mode 100644
index a8ca2c75e89..00000000000
--- a/lib/private/Repair/NC14/RepairPendingCronJobs.php
+++ /dev/null
@@ -1,77 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2018 Morris Jobke <hey@morrisjobke.de>
- *
- * @author Morris Jobke <hey@morrisjobke.de>
- *
- * @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 <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Repair\NC14;
-
-use OCP\DB\QueryBuilder\IQueryBuilder;
-use OCP\IConfig;
-use OCP\IDBConnection;
-use OCP\Migration\IOutput;
-use OCP\Migration\IRepairStep;
-
-class RepairPendingCronJobs implements IRepairStep {
- const MAX_ROWS = 1000;
-
- /** @var IDBConnection */
- private $connection;
- /** @var IConfig */
- private $config;
-
- public function __construct(IDBConnection $connection, IConfig $config) {
- $this->connection = $connection;
- $this->config = $config;
- }
-
-
- public function getName() {
- return 'Repair pending cron jobs';
- }
-
- private function shouldRun() {
- $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
-
- return version_compare($versionFromBeforeUpdate, '14.0.0.9', '<');
- }
-
- /**
- * @suppress SqlInjectionChecker
- */
- private function repair() {
- $reset = $this->connection->getQueryBuilder();
- $reset->update('jobs')
- ->set('reserved_at', $reset->expr()->literal(0, IQueryBuilder::PARAM_INT))
- ->where($reset->expr()->neq('reserved_at', $reset->expr()->literal(0, IQueryBuilder::PARAM_INT)));
-
- return $reset->execute();
- }
-
- public function run(IOutput $output) {
- if ($this->shouldRun()) {
- $count = $this->repair();
-
- $output->info('Repaired ' . $count . ' pending cron job(s).');
- } else {
- $output->info('No need to repair pending cron jobs.');
- }
- }
-}
diff --git a/lib/private/Repair/NC15/SetVcardDatabaseUID.php b/lib/private/Repair/NC15/SetVcardDatabaseUID.php
deleted file mode 100644
index cefb1c18111..00000000000
--- a/lib/private/Repair/NC15/SetVcardDatabaseUID.php
+++ /dev/null
@@ -1,154 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
- *
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- *
- * @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 <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Repair\NC15;
-
-use OCP\IConfig;
-use OCP\IDBConnection;
-use OCP\ILogger;
-use OCP\Migration\IOutput;
-use OCP\Migration\IRepairStep;
-use Sabre\VObject\Reader;
-use Sabre\VObject\ParseException;
-
-class SetVcardDatabaseUID implements IRepairStep {
- const MAX_ROWS = 1000;
-
- /** @var IDBConnection */
- private $connection;
-
- /** @var IConfig */
- private $config;
-
- /** @var ILogger */
- private $logger;
-
- private $updateQuery;
-
- public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
- $this->connection = $connection;
- $this->config = $config;
- $this->logger = $logger;
- }
-
- public function getName() {
- return 'Extract the vcard uid and store it in the db';
- }
-
- /**
- * @return \Generator
- * @suppress SqlInjectionChecker
- */
- private function getInvalidEntries() {
- $builder = $this->connection->getQueryBuilder();
-
- $builder->select('id', 'carddata')
- ->from('cards')
- ->where($builder->expr()->isNull('uid'))
- ->setMaxResults(self::MAX_ROWS);
-
- do {
- $result = $builder->execute();
- $rows = $result->fetchAll();
- foreach ($rows as $row) {
- yield $row;
- }
- $result->closeCursor();
- } while (count($rows) > 0);
- }
-
- /**
- * Extract UID from vcard
- *
- * @param string $cardData the vcard raw data
- * @param IOutput $output the output logger
- * @return string the uid or empty if none
- */
- private function getUID(string $cardData, IOutput $output): string {
- try {
- $vCard = Reader::read($cardData);
- if ($vCard->UID) {
- $uid = $vCard->UID->getValue();
-
- return $uid;
- }
- } catch (ParseException $e) {
- $output->warning('One vCard is broken. We logged the exception and will continue the repair.');
- $this->logger->logException($e);
- }
-
- return '';
- }
-
- /**
- * @param int $id
- * @param string $uid
- */
- private function update(int $id, string $uid) {
- if (!$this->updateQuery) {
- $builder = $this->connection->getQueryBuilder();
-
- $this->updateQuery = $builder->update('cards')
- ->set('uid', $builder->createParameter('uid'))
- ->where($builder->expr()->eq('id', $builder->createParameter('id')));
- }
-
- $this->updateQuery->setParameter('id', $id);
- $this->updateQuery->setParameter('uid', $uid);
-
- $this->updateQuery->execute();
- }
-
- private function repair(IOutput $output): int {
- $this->connection->beginTransaction();
- $entries = $this->getInvalidEntries();
- $count = 0;
- foreach ($entries as $entry) {
- $count++;
- $cardData = $entry['carddata'];
- if (is_resource($cardData)) {
- $cardData = stream_get_contents($cardData);
- }
- $uid = $this->getUID($cardData, $output);
- $this->update($entry['id'], $uid);
- }
- $this->connection->commit();
-
- return $count;
- }
-
- private function shouldRun() {
- $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
-
- // was added to 15.0.0.2
- return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<=');
- }
-
- public function run(IOutput $output) {
- if ($this->shouldRun()) {
- $count = $this->repair($output);
-
- $output->info('Fixed ' . $count . ' vcards');
- }
- }
-}
diff --git a/lib/private/Repair/RemoveRootShares.php b/lib/private/Repair/RemoveRootShares.php
deleted file mode 100644
index a06105384fb..00000000000
--- a/lib/private/Repair/RemoveRootShares.php
+++ /dev/null
@@ -1,142 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-namespace OC\Repair;
-
-use OCP\Files\IRootFolder;
-use OCP\IDBConnection;
-use OCP\IUser;
-use OCP\IUserManager;
-use OCP\Migration\IOutput;
-use OCP\Migration\IRepairStep;
-
-/**
- * Class RemoveRootShares
- *
- * @package OC\Repair
- */
-class RemoveRootShares implements IRepairStep {
-
- /** @var IDBConnection */
- protected $connection;
-
- /** @var IUserManager */
- protected $userManager;
-
- /** @var IRootFolder */
- protected $rootFolder;
-
- /**
- * RemoveRootShares constructor.
- *
- * @param IDBConnection $connection
- * @param IUserManager $userManager
- * @param IRootFolder $rootFolder
- */
- public function __construct(IDBConnection $connection,
- IUserManager $userManager,
- IRootFolder $rootFolder) {
- $this->connection = $connection;
- $this->userManager = $userManager;
- $this->rootFolder = $rootFolder;
- }
-
- /**
- * @return string
- */
- public function getName() {
- return 'Remove shares of a users root folder';
- }
-
- /**
- * @param IOutput $output
- */
- public function run(IOutput $output) {
- if ($this->rootSharesExist()) {
- $this->removeRootShares($output);
- }
- }
-
- /**
- * @param IOutput $output
- */
- private function removeRootShares(IOutput $output) {
- $function = function(IUser $user) use ($output) {
- $userFolder = $this->rootFolder->getUserFolder($user->getUID());
- $fileId = $userFolder->getId();
-
- $qb = $this->connection->getQueryBuilder();
- $qb->delete('share')
- ->where($qb->expr()->eq('file_source', $qb->createNamedParameter($fileId)))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
- $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
- ));
-
- $qb->execute();
-
- $output->advance();
- };
-
- $output->startProgress($this->userManager->countSeenUsers());
-
- $this->userManager->callForSeenUsers($function);
-
- $output->finishProgress();
- }
-
- /**
- * Verify if this repair steps is required
- * It *should* not be necessary in most cases and it can be very
- * costly.
- *
- * @return bool
- */
- private function rootSharesExist() {
- $qb = $this->connection->getQueryBuilder();
- $qb2 = $this->connection->getQueryBuilder();
-
- $qb->select('fileid')
- ->from('filecache')
- ->where($qb->expr()->eq('path', $qb->expr()->literal('files')));
-
- $qb2->select('id')
- ->from('share')
- ->where($qb2->expr()->in('file_source', $qb2->createFunction($qb->getSQL())))
- ->andWhere($qb2->expr()->orX(
- $qb2->expr()->eq('item_type', $qb->expr()->literal('file')),
- $qb2->expr()->eq('item_type', $qb->expr()->literal('folder'))
- ))
- ->setMaxResults(1);
-
- $cursor = $qb2->execute();
- $data = $cursor->fetch();
- $cursor->closeCursor();
-
- if ($data === false) {
- return false;
- }
-
- return true;
- }
-}
-