1515use Doctrine \DBAL \Connection as DBALConnection ;
1616use Doctrine \DBAL \Platforms \OraclePlatform ;
1717use Doctrine \DBAL \Result ;
18+ use Doctrine \DBAL \Schema \ComparatorConfig ;
1819use Doctrine \DBAL \Schema \Name \Identifier ;
1920use Doctrine \DBAL \Schema \Name \UnqualifiedName ;
2021use 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