Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add foreign key constraints for bills and bill_owers tables #181

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions lib/Migration/Version010503Date20221030201200.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace OCA\Cospend\Migration;

use Closure;
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;

class Version010503Date20221030201200 extends SimpleMigrationStep {
/**
* @var IDBConnection
*/
private IDBConnection $connection;

/**
* @param IDBConnection $connection
*/
public function __construct(IDBConnection $connection) {
$this->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'], IQueryBuilder::PARAM_STR);
}

$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'], IQueryBuilder::PARAM_INT);
}

$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) {
}
}