Skip to content

Commit

Permalink
[SQLite] Replaced SqliteSessionInit with DBAL SQLSessionInit (#4)
Browse files Browse the repository at this point in the history
* Enabled Foreign Keys for SQLite via SqliteSessionInit Event

SqliteSessionInit is executed when building SqliteDbPlatform

* Replaced SqliteSessionInit with DBAL SQLSessionInit
  • Loading branch information
alongosz authored Apr 17, 2019
1 parent 7ee37ec commit 0526533
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/lib/Database/DbPlatform/SqliteDbPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
namespace EzSystems\DoctrineSchema\Database\DbPlatform;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\Listeners\SQLSessionInit;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use EzSystems\DoctrineSchema\Event\Subscriber\SqliteSessionInit;

class SqliteDbPlatform extends SqlitePlatform implements DbPlatform
{
public function __construct(EventManager $eventManager)
{
parent::__construct();

$eventManager->addEventSubscriber(new SqliteSessionInit());
$eventManager->addEventSubscriber(new SQLSessionInit('PRAGMA FOREIGN_KEYS = ON'));
}

/**
Expand Down
37 changes: 0 additions & 37 deletions src/lib/Event/Subscriber/SqliteSessionInit.php

This file was deleted.

7 changes: 5 additions & 2 deletions tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class SqliteTestDatabaseBuilder implements TestDatabaseBuilder
*/
public function buildDatabase(): Connection
{
$eventManager = new EventManager();

return DriverManager::getConnection(
[
'url' => 'sqlite:///:memory:',
'platform' => new SqliteDbPlatform(new EventManager()),
'platform' => new SqliteDbPlatform($eventManager),
],
new Configuration()
new Configuration(),
$eventManager
);
}
}
67 changes: 67 additions & 0 deletions tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\Tests\DoctrineSchema\Database\DbPlatform;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\ParameterType;
use EzSystems\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform;
use EzSystems\Tests\DoctrineSchema\Database\TestDatabaseFactory;
use PHPUnit\Framework\TestCase;

class SqliteDbPlatformTest extends TestCase
{
/**
* @var \EzSystems\Tests\DoctrineSchema\Database\TestDatabaseFactory
*/
private $testDatabaseFactory;

/**
* @var \EzSystems\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform
*/
private $sqliteDbPlatform;

public function setUp(): void
{
$this->sqliteDbPlatform = new SqliteDbPlatform(new EventManager());
$this->testDatabaseFactory = new TestDatabaseFactory();
}

/**
* @throws \Doctrine\DBAL\DBALException
* @throws \EzSystems\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
*/
public function testForeignKeys(): void
{
$connection = $this->testDatabaseFactory->prepareAndConnect($this->sqliteDbPlatform);
$schema = $connection->getSchemaManager()->createSchema();

$primaryTable = $schema->createTable('my_primary_table');
$primaryTable->addColumn('id', 'integer');
$primaryTable->setPrimaryKey(['id']);

$secondaryTable = $schema->createTable('my_secondary_table');
$secondaryTable->addColumn('id', 'integer');
$secondaryTable->setPrimaryKey(['id']);
$secondaryTable->addForeignKeyConstraint($primaryTable, ['id'], ['id']);

// persist table structure
foreach ($schema->toSql($connection->getDatabasePlatform()) as $query) {
$connection->executeUpdate($query);
}

$connection->insert($primaryTable->getName(), ['id' => 1], [ParameterType::INTEGER]);
$connection->insert($secondaryTable->getName(), ['id' => 1], [ParameterType::INTEGER]);

// insert broken record
$this->expectException(DBALException::class);
$this->expectExceptionMessage('FOREIGN KEY constraint failed');
$connection->insert($secondaryTable->getName(), ['id' => 2], [ParameterType::INTEGER]);
}
}

0 comments on commit 0526533

Please sign in to comment.