Skip to content

Commit 6346e2f

Browse files
committed
generating migrations + 2 test db option
1 parent e8c5b0b commit 6346e2f

File tree

7 files changed

+46
-1
lines changed

7 files changed

+46
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ More about generatings docs in [PHPDOC.md](PHPDOC.md)
8787

8888
### Testing
8989

90+
Create test db with name phplist in your mysql DB or uncomment sqlite part in config_test.yml file to use in memory DB or functional tests
9091
To run the server in testing mode (which normally will only be needed for the
9192
automated tests, provide the `--env` option:
9293

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"doctrine/instantiator": "^2.0",
5959
"masterminds/html5": "^2.9",
6060
"ext-dom": "*",
61-
"league/csv": "^9.23.0"
61+
"league/csv": "^9.23.0",
62+
"doctrine/doctrine-migrations-bundle": "^3.4"
6263
},
6364
"require-dev": {
6465
"phpunit/phpunit": "^9.5",
@@ -127,6 +128,7 @@
127128
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle",
128129
"Symfony\\Bundle\\MonologBundle\\MonologBundle",
129130
"Doctrine\\Bundle\\DoctrineBundle\\DoctrineBundle",
131+
"Doctrine\\Bundle\\MigrationsBundle\\DoctrineMigrationsBundle",
130132
"PhpList\\Core\\EmptyStartPageBundle\\EmptyStartPageBundle"
131133
],
132134
"routes": {

config/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
imports:
22
- { resource: services.yml }
33
- { resource: doctrine.yml }
4+
- { resource: doctrine_migrations.yml }
45

56
# Put parameters here that don't need to change on each machine where the app is deployed
67
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration

config/config_test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ framework:
1111

1212
doctrine:
1313
dbal:
14+
# driver: 'pdo_sqlite'
15+
# memory: true
1416
driver: 'pdo_mysql'
1517
host: '%database_host%'
1618
port: '%database_port%'

config/doctrine_migrations.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
doctrine_migrations:
2+
migrations_paths:
3+
'PhpList\Core\Migrations': '%kernel.project_dir%/src/Migrations'
4+
all_or_nothing: true
5+
organize_migrations: false
6+
storage:
7+
table_storage:
8+
table_name: 'doctrine_migration_versions'
9+
version_column_name: 'version'
10+
version_column_length: 191
11+
executed_at_column_name: 'executed_at'

src/Migrations/.gitkeep

Whitespace-only changes.

src/TestingSupport/Traits/DatabaseTestTrait.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpList\Core\TestingSupport\Traits;
66

7+
use Doctrine\DBAL\Platforms\SqlitePlatform;
78
use Doctrine\ORM\EntityManagerInterface;
89
use Doctrine\ORM\Tools\SchemaTool;
910
use 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

Comments
 (0)