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

Test FK violation during FK create #1035

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions src/Persistence/Sql/Sqlite/SchemaManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Atk4\Data\Persistence\Sql\Sqlite;

use Atk4\Data\Persistence\Sql\Exception;
use Doctrine\DBAL\Exception as DbalException;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
Expand All @@ -25,8 +25,7 @@ public function alterTable(TableDiff $tableDiff): void

$rows = $this->_conn->executeQuery('PRAGMA foreign_key_check')->fetchAllAssociative();
if (count($rows) > 0) {
throw (new Exception('Foreign key constraints are violated'))
->addMoreInfo('data', $rows);
throw new DbalException('Foreign key constraints are violated');
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Schema/TestSqlPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function getConnection(): Persistence\Sql\Connection
null ?? new class() implements SQLLogger {
public function startQuery($sql, array $params = null, array $types = null): void
{
// fix https://github.com/doctrine/dbal/issues/5525
if ($params !== null && $params !== [] && array_is_list($params)) {
$params = array_combine(range(1, count($params)), $params);
}

$test = TestCase::getTestFromBacktrace();
\Closure::bind(fn () => $test->logQuery($sql, $params ?? [], $types ?? []), null, TestCase::class)();
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Schema/MigratorFkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Data\Exception;
use Atk4\Data\Model;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Exception as DbalException;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;

Expand Down Expand Up @@ -76,4 +77,22 @@ public function testForeignKeyViolation(): void
throw $e;
}
}

public function testForeignKeyViolationDuringSetup(): void
{
$country = new Model($this->db, ['table' => 'country']);
$country->addField('name');

$client = new Model($this->db, ['table' => 'client']);
$client->addField('name');
$client->hasOne('country_id', ['model' => $country]);

$this->createMigrator($client)->create();
$this->createMigrator($country)->create();

$client->insert(['name' => 'Leos', 'country_id' => 10]);

$this->expectException(DbalException::class);
$this->createMigrator()->createForeignKey($client->getReference('country_id'));
}
}