From 9540c596826b90bac9f92b4bd29ddd9f42520ec9 Mon Sep 17 00:00:00 2001 From: Benjamin Hummel Date: Mon, 24 Nov 2025 11:33:35 +0100 Subject: [PATCH 1/5] adding support for databases with other existing tables --- .../Doctrine/DoctrineDbalMessageStore.php | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php index 6a2ef80fb..62d7a1a38 100644 --- a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php +++ b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php @@ -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; @@ -52,13 +53,23 @@ public function setup(array $options = []): void throw new InvalidArgumentException('No supported options.'); } - $schema = $this->dbalConnection->createSchemaManager()->introspectSchema(); + $platform = $this->dbalConnection->getDatabasePlatform(); + $schemaManager = $this->dbalConnection->createSchemaManager(); - if ($schema->hasTable($this->tableName)) { - return; + $currentSchema = $schemaManager->introspectSchema(); + + if (class_exists(ComparatorConfig::class)) { + $comparator = $schemaManager->createComparator(new ComparatorConfig(false, false)); + } else { + // Backwards compatibility for doctrine/dbal 3.x + $comparator = $schemaManager->createComparator(); } - $this->addTableToSchema($schema); + $migrations = $platform->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema))); + + foreach ($migrations as $sql) { + $this->dbalConnection->executeQuery($sql); + } } public function drop(): void @@ -113,8 +124,14 @@ public function load(): MessageBag return new MessageBag(...array_merge(...$messages)); } - private function addTableToSchema(Schema $schema): void + private function defineTableSchema(Schema $currentSchema): Schema { + $schema = clone $currentSchema; + + if ($schema->hasTable($this->tableName)) { + $schema->dropTable($this->tableName); + } + $table = $schema->createTable($this->tableName); $table->addOption('_symfony_ai_chat_table_name', $this->tableName); $idColumn = $table->addColumn('id', Types::BIGINT) @@ -141,8 +158,6 @@ private function addTableToSchema(Schema $schema): void } } - foreach ($schema->toSql($this->dbalConnection->getDatabasePlatform()) as $sql) { - $this->dbalConnection->executeQuery($sql); - } + return $schema; } } From c5376361d7b7c9addfe4129b17a8642ffa78eb26 Mon Sep 17 00:00:00 2001 From: Benjamin Hummel Date: Mon, 24 Nov 2025 11:47:44 +0100 Subject: [PATCH 2/5] adding support for databases with other existing tables --- src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php index 62d7a1a38..a33fbc849 100644 --- a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php +++ b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php @@ -53,11 +53,8 @@ public function setup(array $options = []): void throw new InvalidArgumentException('No supported options.'); } - $platform = $this->dbalConnection->getDatabasePlatform(); $schemaManager = $this->dbalConnection->createSchemaManager(); - $currentSchema = $schemaManager->introspectSchema(); - if (class_exists(ComparatorConfig::class)) { $comparator = $schemaManager->createComparator(new ComparatorConfig(false, false)); } else { @@ -65,7 +62,8 @@ public function setup(array $options = []): void $comparator = $schemaManager->createComparator(); } - $migrations = $platform->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema))); + $currentSchema = $schemaManager->introspectSchema(); + $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema))); foreach ($migrations as $sql) { $this->dbalConnection->executeQuery($sql); From a01d22e8b87cd7212cbcfb71a51c345813bed540 Mon Sep 17 00:00:00 2001 From: Benjamin Hummel Date: Mon, 24 Nov 2025 12:09:24 +0100 Subject: [PATCH 3/5] fix tests --- .../src/Bridge/Doctrine/DoctrineDbalMessageStore.php | 10 +++++----- .../Bridge/Doctrine/DoctrineDbalMessageStoreTest.php | 12 +++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php index a33fbc849..82cdc87b9 100644 --- a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php +++ b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php @@ -54,6 +54,11 @@ public function setup(array $options = []): void } $schemaManager = $this->dbalConnection->createSchemaManager(); + $currentSchema = $schemaManager->introspectSchema(); + + if ($currentSchema->hasTable($this->tableName)) { + return; + } if (class_exists(ComparatorConfig::class)) { $comparator = $schemaManager->createComparator(new ComparatorConfig(false, false)); @@ -62,7 +67,6 @@ public function setup(array $options = []): void $comparator = $schemaManager->createComparator(); } - $currentSchema = $schemaManager->introspectSchema(); $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema))); foreach ($migrations as $sql) { @@ -126,10 +130,6 @@ private function defineTableSchema(Schema $currentSchema): Schema { $schema = clone $currentSchema; - if ($schema->hasTable($this->tableName)) { - $schema->dropTable($this->tableName); - } - $table = $schema->createTable($this->tableName); $table->addOption('_symfony_ai_chat_table_name', $this->tableName); $idColumn = $table->addColumn('id', Types::BIGINT) diff --git a/src/chat/tests/Bridge/Doctrine/DoctrineDbalMessageStoreTest.php b/src/chat/tests/Bridge/Doctrine/DoctrineDbalMessageStoreTest.php index b6b9c01cf..020fb41b7 100644 --- a/src/chat/tests/Bridge/Doctrine/DoctrineDbalMessageStoreTest.php +++ b/src/chat/tests/Bridge/Doctrine/DoctrineDbalMessageStoreTest.php @@ -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; @@ -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(); } From 922aa1a4816444e4bfebd9e75b4d5b67f476ce99 Mon Sep 17 00:00:00 2001 From: Benjamin Hummel Date: Mon, 24 Nov 2025 12:14:08 +0100 Subject: [PATCH 4/5] fix namings --- src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php index 82cdc87b9..891e0aab7 100644 --- a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php +++ b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php @@ -54,9 +54,9 @@ public function setup(array $options = []): void } $schemaManager = $this->dbalConnection->createSchemaManager(); - $currentSchema = $schemaManager->introspectSchema(); + $schema = $schemaManager->introspectSchema(); - if ($currentSchema->hasTable($this->tableName)) { + if ($schema->hasTable($this->tableName)) { return; } @@ -67,7 +67,7 @@ public function setup(array $options = []): void $comparator = $schemaManager->createComparator(); } - $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema))); + $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($schema, $this->defineTableSchema($schema))); foreach ($migrations as $sql) { $this->dbalConnection->executeQuery($sql); From be6ecf9b0a745b2729e5ffcfbea8d35a21546df0 Mon Sep 17 00:00:00 2001 From: Benjamin Hummel Date: Mon, 24 Nov 2025 12:16:15 +0100 Subject: [PATCH 5/5] fix namings --- src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php index 891e0aab7..340b3b2ec 100644 --- a/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php +++ b/src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php @@ -67,7 +67,7 @@ public function setup(array $options = []): void $comparator = $schemaManager->createComparator(); } - $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($schema, $this->defineTableSchema($schema))); + $migrations = $this->dbalConnection->getDatabasePlatform()->getAlterSchemaSQL($comparator->compareSchemas($schema, $this->addTableToSchema($schema))); foreach ($migrations as $sql) { $this->dbalConnection->executeQuery($sql); @@ -126,7 +126,7 @@ public function load(): MessageBag return new MessageBag(...array_merge(...$messages)); } - private function defineTableSchema(Schema $currentSchema): Schema + private function addTableToSchema(Schema $currentSchema): Schema { $schema = clone $currentSchema;