From 957d603b216a3e947ad275006a018eeb84d34248 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 13 Jul 2021 21:07:05 +0200 Subject: [PATCH] Started implementation of fix Signed-off-by: Christian Wolf --- .../Version000000Date20210701093123.php | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/Migration/Version000000Date20210701093123.php b/lib/Migration/Version000000Date20210701093123.php index c432b91d..b1b07960 100644 --- a/lib/Migration/Version000000Date20210701093123.php +++ b/lib/Migration/Version000000Date20210701093123.php @@ -4,16 +4,57 @@ declare(strict_types=1); namespace OCA\Cookbook\Migration; -use Closure; +use OCP\IDBConnection; use OCP\DB\ISchemaWrapper; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; +use Closure; /** * Auto-generated migration step: Please modify to your needs! */ class Version000000Date20210701093123 extends SimpleMigrationStep { + /** + * @var IDBConnection + */ + private $db; + + public function __construct(IDBConnection $db){ + $this->db = $db; + } + + function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options){ + $this->db->beginTransaction(); + try { + + $qb = $this->db->getQueryBuilder(); + + // Fetch all rows that are non-unique + $qb->selectAlias('n.user_id', 'user') + ->selectAlias($qb->createFunction('COUNT(n.user_id)'), 'cnt') + ->from('cookbook_names', 'n') + ->groupBy('n.user_id', 'n.recipe_id') + ->having('cnt > 1'); + //echo $qb->getSQL() . "\n"; + + $cursor = $qb->execute(); + $result = $cursor->fetchAll(); + + if(sizeof($result) > 0){ + // We have to fix the database + throw new \Exception('This is not yet implemented.'); + } + + // Finish the transaction + $this->db->commit(); + } catch (\Exception $e) { + // Abort the transaction + $this->db->rollBack(); + throw $e; + } + } + /** * @param IOutput $output * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`