Skip to content

Commit

Permalink
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 Mar 1, 2018
1 parent 881f595 commit 055e710
Show file tree
Hide file tree
Showing 3 changed files with 434 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 @@ -37,6 +37,7 @@
use OC\Repair\OldGroupMembershipShares;
use OC\Repair\RemoveGetETagEntries;
use OC\Repair\RemoveRootShares;
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
160 changes: 160 additions & 0 deletions lib/private/Repair/RepairOrphanedSubshare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?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 $availableParents;

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

/** @var IQueryBuilder */
private $deleteOrphanReshares;
/**
* RepairOrphanedSubshare constructor.
*
* @param IDBConnection $connection
*/
public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}

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

/**
* This delete query deletes orphan shares whose parents are missing
* @param $parentId
*/
private function setDeleteOrphanSharesQuery($parentId) {
$builder = $this->connection->getQueryBuilder();
$builder
->delete('share')
->where($builder->expr()->eq('parent', $builder->createNamedParameter($parentId)));
$this->deleteOrphanReshares = $builder;
}

/**
* This select query is to get the result of parents === id
* @param $parentId
*/
private function setSelectShareId($parentId) {
$builder = $this->connection->getQueryBuilder();
$builder
->select('id')
->from('share')
->where($builder->expr()->eq('id', $builder->createNamedParameter($parentId)))->setMaxResults(2);//Where id = parent from above query or available parents
$this->getSharesWithParentsAvailable = $builder;
}

/**
* This select query gives us unique parents from the table
* @param $pageLimit
* @param $paginationOffset
*/
private function setSelectGetAllParents($pageLimit, $paginationOffset) {
$builder = $this->connection->getQueryBuilder();
$builder
->select('parent')
->from('share')
->groupBy('parent')->orderBy('parent')->setMaxResults($pageLimit)->setFirstResult($paginationOffset);

$this->availableParents = $builder;
}
/**
* Run repair step.
* Must throw exception on error.
*
* @param IOutput $output
* @throws \Exception in case of failure
* @since 9.1.0
*/
public function run(IOutput $output) {
$missingParents = [];
$paginationOffset = 1;
$pageLimit = 1000;
$deleteRows = [];

do {
$this->setSelectGetAllParents($pageLimit, $paginationOffset);
$results = $this->availableParents->execute();
$rows = $results->fetchAll();
$results->closeCursor();
$paginationOffset += $pageLimit;
$lastResultCount = 0;

foreach ($rows as $row) {
if ($row['parent'] === null) {
$lastResultCount++;
continue;
}
$this->setSelectShareId($row['parent']);
$getIdQuery = $this->getSharesWithParentsAvailable->execute();
$getIdRows = $getIdQuery->fetchAll();
$getIdQuery->closeCursor();
$lastResultCount++;
/**
* Check if the query result is empty.
* And if parent is not added to missingParents array, then
* add it so that shares of this parent who became orphans can
* be deleted later.
*/
if ((count($getIdRows) === 0) &&
(in_array($row['parent'], $missingParents, true) === false)) {
$missingParents[] = $row['parent'];
}
}

if (count($missingParents) > 0) {
foreach ($missingParents as $missingParent) {
$this->setDeleteOrphanSharesQuery($missingParent);
$deleteRows[] = $this->deleteOrphanReshares;
}
$missingParents = []; //Reset the array
}
} while($lastResultCount > 0);

if (count($deleteRows) > 0) {
foreach ($deleteRows as $deleteRow) {
$deleteRow->execute();
}
}
}
}
Loading

0 comments on commit 055e710

Please sign in to comment.