Skip to content
Open
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
25 changes: 19 additions & 6 deletions src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\ComparatorConfig;
use Doctrine\DBAL\Schema\Name\Identifier;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
Expand Down Expand Up @@ -52,13 +53,25 @@ public function setup(array $options = []): void
throw new InvalidArgumentException('No supported options.');
}

$schema = $this->dbalConnection->createSchemaManager()->introspectSchema();
$schemaManager = $this->dbalConnection->createSchemaManager();
$schema = $schemaManager->introspectSchema();

if ($schema->hasTable($this->tableName)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check could also be omitted. Then it would be possible to adjust the table.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bu if the table already exist, you're not supposed to add it again / alter it? 🤔

if would require to being able to passe extra fields during setup() calls, for now, it's not supported 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be exactly my use case :-) I just wanted to do it in two steps and fix the bug first.

It would be really nice if you could specify additional fields in the setup... I can gladly update the program so that's possible too...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think the structure isn't quite there yet, since you would also have to adjust the load and save functions.

return;
}

$this->addTableToSchema($schema);
if (class_exists(ComparatorConfig::class)) {
$comparator = $schemaManager->createComparator(new ComparatorConfig(false, false));
} else {
// Backwards compatibility for doctrine/dbal 3.x
$comparator = $schemaManager->createComparator();
}

$migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($schema, $this->addTableToSchema($schema)));

foreach ($migrations as $sql) {
$this->dbalConnection->executeQuery($sql);
}
}

public function drop(): void
Expand Down Expand Up @@ -113,8 +126,10 @@ public function load(): MessageBag
return new MessageBag(...array_merge(...$messages));
}

private function addTableToSchema(Schema $schema): void
private function addTableToSchema(Schema $currentSchema): Schema
{
$schema = clone $currentSchema;

$table = $schema->createTable($this->tableName);
$table->addOption('_symfony_ai_chat_table_name', $this->tableName);
$idColumn = $table->addColumn('id', Types::BIGINT)
Expand All @@ -141,8 +156,6 @@ private function addTableToSchema(Schema $schema): void
}
}

foreach ($schema->toSql($this->dbalConnection->getDatabasePlatform()) as $sql) {
$this->dbalConnection->executeQuery($sql);
}
return $schema;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Table;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Chat\Bridge\Doctrine\DoctrineDbalMessageStore;
Expand Down Expand Up @@ -76,15 +78,23 @@ public function testMessageStoreTableCanBeSetup()
$schema = $this->createMock(Schema::class);
$schema->expects($this->once())->method('hasTable')->willReturn(false);
$schema->expects($this->once())->method('createTable')->with('foo')->willReturn($table);
$schema->expects($this->once())->method('toSql')->with($platform)->willReturn([]);

$sqliteSchemaManager = $this->createMock(AbstractSchemaManager::class);
$sqliteSchemaManager->expects($this->once())->method('introspectSchema')->willReturn($schema);

$comparator = $this->createMock(Comparator::class);
$sqliteSchemaManager->expects($this->once())->method('createComparator')->willReturn($comparator);

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('createSchemaManager')->willReturn($sqliteSchemaManager);
$connection->expects($this->exactly(2))->method('getDatabasePlatform')->willReturn($platform);

$schemaDiff = $this->createMock(SchemaDiff::class);

$comparator->expects($this->once())->method('compareSchemas')->willReturn($schemaDiff);
$platform->expects($this->once())->method('getAlterSchemaSQL')->willReturn(['SQL STATEMENT']);
$connection->expects($this->once())->method('executeQuery')->with('SQL STATEMENT');

$messageStore = new DoctrineDbalMessageStore('foo', $connection);
$messageStore->setup();
}
Expand Down
Loading