44
55namespace PhpList \Core \TestingSupport \Traits ;
66
7+ use Doctrine \DBAL \Platforms \SqlitePlatform ;
78use Doctrine \ORM \EntityManagerInterface ;
89use Doctrine \ORM \Tools \SchemaTool ;
910use Doctrine \ORM \Tools \ToolsException ;
@@ -83,6 +84,15 @@ protected function loadSchema(): void
8384 $ schemaTool = new SchemaTool ($ this ->entityManager );
8485 $ metadata = $ this ->entityManager ->getMetadataFactory ()->getAllMetadata ();
8586
87+ if ($ this ->entityManager ->getConnection ()->getDatabasePlatform () instanceof SqlitePlatform) {
88+ $ this ->runForSqlite ($ metadata , $ schemaTool );
89+ } else {
90+ $ this ->runForMySql ($ metadata , $ schemaTool );
91+ }
92+ }
93+
94+ private function runForMySql ($ metadata , $ schemaTool ): void
95+ {
8696 try {
8797 $ schemaTool ->createSchema ($ metadata );
8898 } catch (ToolsException $ e ) {
@@ -98,4 +108,22 @@ protected function loadSchema(): void
98108 }
99109 }
100110 }
111+
112+ private function runForSqlite ($ metadata , $ schemaTool ): void
113+ {
114+ $ connection = $ this ->entityManager ->getConnection ();
115+ $ schemaManager = $ connection ->createSchemaManager ();
116+
117+ foreach ($ metadata as $ classMetadata ) {
118+ $ tableName = $ classMetadata ->getTableName ();
119+
120+ if (!$ schemaManager ->tablesExist ([$ tableName ])) {
121+ try {
122+ $ schemaTool ->createSchema ([$ classMetadata ]);
123+ } catch (ToolsException $ e ) {
124+ echo $ e ->getMessage ();
125+ }
126+ }
127+ }
128+ }
101129}
0 commit comments