From cc01e673d2389c24cc1b07cbac7adb7ede1fc2bb Mon Sep 17 00:00:00 2001 From: gemini-git <15232511+gemini-git@users.noreply.github.com> Date: Sun, 30 Oct 2022 23:04:57 +0100 Subject: [PATCH 1/2] add foreign key constraints for bills and bill_owers tables --- .../Version010503Date20221030201200.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 lib/Migration/Version010503Date20221030201200.php diff --git a/lib/Migration/Version010503Date20221030201200.php b/lib/Migration/Version010503Date20221030201200.php new file mode 100644 index 000000000..a48940196 --- /dev/null +++ b/lib/Migration/Version010503Date20221030201200.php @@ -0,0 +1,114 @@ +connection = $connection; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @throws Exception + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + $queryBuilder = $this->connection->getQueryBuilder(); + + // get existing project ids + $queryBuilder->select('id') + ->from('cospend_projects'); + $result = $queryBuilder->executeQuery(); + + $existingProjectIds = []; + while ($row = $result->fetch()) { + $existingProjectIds[] = $queryBuilder->createNamedParameter($row['id']); + } + + $result->closeCursor(); + $queryBuilder->resetQueryParts(); + + // delete bills without project + $queryBuilder->delete('cospend_bills')->where( + $queryBuilder->expr()->notIn('projectid', $existingProjectIds) + ); + $queryBuilder->executeStatement(); + $queryBuilder->resetQueryParts(); + + // get existing bill ids + $queryBuilder->select('id') + ->from('cospend_bills'); + $result = $queryBuilder->executeQuery(); + + $existingBillIds = []; + while ($row = $result->fetch()) { + $existingBillIds[] = $queryBuilder->createNamedParameter((int)$row['id']); + } + + $result->closeCursor(); + $queryBuilder->resetQueryParts(); + + // delete bill owers without bill + $queryBuilder->delete('cospend_bill_owers')->where( + $queryBuilder->expr()->notIn('billid', $existingBillIds) + ); + $queryBuilder->executeStatement(); + $queryBuilder->resetQueryParts(); + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + * @throws SchemaException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $schema->getTable('cospend_bills') + ->addForeignKeyConstraint( + $schema->getTable('cospend_projects'), + ['projectid'], + ['id'], + ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'] + ); + + $schema->getTable('cospend_bill_owers') + ->addForeignKeyConstraint( + $schema->getTable('cospend_bills'), + ['billid'], + ['id'], + ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'] + ); + + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + } +} From 490dd10d777d530f198cd3e44afa54244a26f6a8 Mon Sep 17 00:00:00 2001 From: gemini-git <15232511+gemini-git@users.noreply.github.com> Date: Sun, 30 Oct 2022 23:08:08 +0100 Subject: [PATCH 2/2] added named parameter type --- lib/Migration/Version010503Date20221030201200.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Migration/Version010503Date20221030201200.php b/lib/Migration/Version010503Date20221030201200.php index a48940196..27848bbd3 100644 --- a/lib/Migration/Version010503Date20221030201200.php +++ b/lib/Migration/Version010503Date20221030201200.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Schema\SchemaException; use OCP\DB\Exception; use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\Migration\SimpleMigrationStep; use OCP\Migration\IOutput; @@ -39,7 +40,7 @@ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $ $existingProjectIds = []; while ($row = $result->fetch()) { - $existingProjectIds[] = $queryBuilder->createNamedParameter($row['id']); + $existingProjectIds[] = $queryBuilder->createNamedParameter($row['id'], IQueryBuilder::PARAM_STR); } $result->closeCursor(); @@ -59,7 +60,7 @@ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $ $existingBillIds = []; while ($row = $result->fetch()) { - $existingBillIds[] = $queryBuilder->createNamedParameter((int)$row['id']); + $existingBillIds[] = $queryBuilder->createNamedParameter((int)$row['id'], IQueryBuilder::PARAM_INT); } $result->closeCursor();