Skip to content

Commit

Permalink
[stable10] Repair step to fix orphan reshares
Browse files Browse the repository at this point in the history
Its being reported that shares are orphaned.
And in such state when an upgrade operation is
performed results in failure. This repair
step fixes the orphan shares.

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Apr 4, 2018
1 parent 1eccddb commit 734da98
Show file tree
Hide file tree
Showing 3 changed files with 693 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OC\Repair\RemoveGetETagEntries;
use OC\Repair\RemoveRootShares;
use OC\Repair\RepairMismatchFileCachePath;
use OC\Repair\RepairOrphanedSubshare;
use OC\Repair\RepairSubShares;
use OC\Repair\SharePropagation;
use OC\Repair\SqliteAutoincrement;
Expand Down Expand Up @@ -191,6 +192,7 @@ public static function getBeforeUpgradeRepairSteps() {
new InnoDB(),
new Collation(\OC::$server->getConfig(), $connection),
new SqliteAutoincrement($connection),
new RepairOrphanedSubshare($connection),
new SearchLuceneTables(),
new Apps(\OC::$server->getAppManager(), \OC::$server->getEventDispatcher(), \OC::$server->getConfig(), new \OC_Defaults()),
];
Expand Down Expand Up @@ -249,7 +251,7 @@ public function advance($step = 1, $description = '') {
}

/**
* @param int $max
* emit signal
*/
public function finishProgress() {
// for now just emit as we did in the past
Expand Down
107 changes: 107 additions & 0 deletions lib/private/RepairOrphanedSubshare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* @author Sujith Haridasan <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @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\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class RepairOrphanedSubshare implements IRepairStep {

/** @var IDBConnection */
private $connection;

/** @var IQueryBuilder */
private $missingParents;

/** @var IQueryBuilder */
private $deleteOrphanReshares;

/** @var int */
private $pageLimit;

/**
* RepairOrphanedSubshare constructor.
*
* @param IDBConnection $connection
*/
public function __construct(IDBConnection $connection, $pageLimit = 1000) {
$this->connection = $connection;
$this->pageLimit = $pageLimit;

//This delete query deletes orphan shares whose parents are missing
$this->deleteOrphanReshares = $this->connection->getQueryBuilder();
$this->deleteOrphanReshares
->delete('share')
->where(
$this->deleteOrphanReshares->expr()->eq('parent',
$this->deleteOrphanReshares->createParameter('parentId')));


//Set the query to get the parent id's missing from the table.
$this->missingParents = $this->connection->getQueryBuilder();
$qb = $this->connection->getQueryBuilder();

$qb->select('id')
->from('share');
$this->missingParents->select('parent')
->from('share');
$this->missingParents->where($this->missingParents->expr()->isNotNull('parent'));
$this->missingParents->andWhere($this->missingParents->expr()->notIn('parent', $this->missingParents->createFunction($qb->getSQL())));
$this->missingParents->groupBy('parent');
}

/**
* Returns the step's name
*
* @return string
*/
public function getName() {
return 'Repair orphaned reshare';
}

/**
* Run repair step.
* Must throw exception on error.
*
* @param IOutput $output
* @throws \Exception in case of failure
*/
public function run(IOutput $output) {
//$pageLimit = 1000;

/** A one time call to get missing parents from oc_share table */
do {
$this->missingParents->setMaxResults($this->pageLimit);
$results = $this->missingParents->execute();
$rows = $results->fetchAll();
$results->closeCursor();
if (count($rows) > 0) {
foreach ($rows as $row) {
$this->deleteOrphanReshares->setParameter('parentId', $row['parent'])->execute();
}
}

} while (!empty($rows));
}
}
Loading

0 comments on commit 734da98

Please sign in to comment.