Skip to content

Commit 53d8e10

Browse files
committed
adding support for databases with other existing tables
1 parent 6ed950f commit 53d8e10

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/chat/src/Bridge/Doctrine/DoctrineDbalMessageStore.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Connection as DBALConnection;
1616
use Doctrine\DBAL\Platforms\OraclePlatform;
1717
use Doctrine\DBAL\Result;
18+
use Doctrine\DBAL\Schema\ComparatorConfig;
1819
use Doctrine\DBAL\Schema\Name\Identifier;
1920
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
2021
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
@@ -52,13 +53,23 @@ public function setup(array $options = []): void
5253
throw new InvalidArgumentException('No supported options.');
5354
}
5455

55-
$schema = $this->dbalConnection->createSchemaManager()->introspectSchema();
56+
$platform = $this->dbalConnection->getDatabasePlatform();
57+
$schemaManager = $this->dbalConnection->createSchemaManager();
5658

57-
if ($schema->hasTable($this->tableName)) {
58-
return;
59+
$currentSchema = $schemaManager->introspectSchema();
60+
61+
if (class_exists(ComparatorConfig::class)) {
62+
$comparator = $schemaManager->createComparator(new ComparatorConfig(false, false));
63+
} else {
64+
// Backwards compatibility for doctrine/dbal 3.x
65+
$comparator = $schemaManager->createComparator();
5966
}
6067

61-
$this->addTableToSchema($schema);
68+
$migrations = $platform->getAlterSchemaSQL($comparator->compareSchemas($currentSchema, $this->defineTableSchema($currentSchema)));
69+
70+
foreach ($migrations as $sql) {
71+
$this->dbalConnection->executeQuery($sql);
72+
}
6273
}
6374

6475
public function drop(): void
@@ -113,8 +124,14 @@ public function load(): MessageBag
113124
return new MessageBag(...array_merge(...$messages));
114125
}
115126

116-
private function addTableToSchema(Schema $schema): void
127+
private function defineTableSchema(Schema $currentSchema): Schema
117128
{
129+
$schema = clone $currentSchema;
130+
131+
if ($schema->hasTable($this->tableName)) {
132+
$schema->dropTable($this->tableName);
133+
}
134+
118135
$table = $schema->createTable($this->tableName);
119136
$table->addOption('_symfony_ai_chat_table_name', $this->tableName);
120137
$idColumn = $table->addColumn('id', Types::BIGINT)
@@ -141,8 +158,6 @@ private function addTableToSchema(Schema $schema): void
141158
}
142159
}
143160

144-
foreach ($schema->toSql($this->dbalConnection->getDatabasePlatform()) as $sql) {
145-
$this->dbalConnection->executeQuery($sql);
146-
}
161+
return $schema;
147162
}
148163
}

0 commit comments

Comments
 (0)