-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stable10] Repair step to fix orphan reshares
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
Showing
3 changed files
with
693 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.