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 <sharidasan@owncloud.com>
  • Loading branch information
sharidas committed Apr 6, 2018
1 parent 1eccddb commit 9e2f525
Show file tree
Hide file tree
Showing 3 changed files with 514 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
97 changes: 97 additions & 0 deletions lib/private/Repair/RepairOrphanedSubshare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* @author Sujith Haridasan <sharidasan@owncloud.com>
*
* @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;
}

/**
* 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) {
/**
* DELETE FROM share
* WHERE parent NOT IN (
* SELECT id FROM (
* SELECT id FROM share
* ORDER BY id -- to prevent derived table merge
* ) mysqlerr1093hack
* )
*/

// subselect for all share ids
$allShares = $this->connection->getQueryBuilder()
->select('id')
->from('share')
->orderBy('id'); // to prevent derived table merge, see https://mariadb.com/kb/en/library/derived-table-merge-optimization/#factsheet

// TODO this subquery currently cannot be built with our doctrine wrapper
// TODO make the mysql hack optional? might also be needed for oracle
$subquery = "SELECT `id` FROM ({$allShares->getSQL()}) mysqlerr1093hack";

//This delete query deletes orphan shares whose parents are missing
$deleteOrphanReshares = $this->connection->getQueryBuilder();
$deleteOrphanReshares
->delete('share')
->where($deleteOrphanReshares->expr()->notIn('parent',
$deleteOrphanReshares->createFunction($subquery)));
$deleteOrphanReshares->execute();
}
}
Loading

0 comments on commit 9e2f525

Please sign in to comment.