-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
ORMDatabaseResetter.php
158 lines (125 loc) · 5.61 KB
/
ORMDatabaseResetter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Foundry\Test;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Console\Application;
/**
* @internal
*/
final class ORMDatabaseResetter extends AbstractSchemaResetter
{
public const RESET_MODE_SCHEMA = 'schema';
public const RESET_MODE_MIGRATE = 'migrate';
/** @var list<string> */
private array $connectionsToReset = [];
/** @var list<string> */
private array $objectManagersToReset = [];
/**
* @param list<string> $connectionsToReset
* @param list<string> $objectManagersToReset
*/
public function __construct(private Application $application, private ManagerRegistry $registry, array $connectionsToReset, array $objectManagersToReset, private string $resetMode)
{
self::validateObjectsToReset('connection', \array_keys($registry->getConnectionNames()), $connectionsToReset);
$this->connectionsToReset = $connectionsToReset;
self::validateObjectsToReset('object manager', \array_keys($registry->getManagerNames()), $objectManagersToReset);
$this->objectManagersToReset = $objectManagersToReset;
}
public function resetDatabase(): void
{
$this->dropAndResetDatabase();
$this->createSchema();
}
public function resetSchema(): void
{
if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
// not required as the DAMADoctrineTestBundle wraps each test in a transaction
return;
}
$this->dropSchema();
$this->createSchema();
}
private function createSchema(): void
{
if ($this->isResetUsingMigrations()) {
$this->runCommand($this->application, 'doctrine:migrations:migrate', ['-n' => true]);
return;
}
foreach ($this->objectManagersToReset() as $manager) {
$this->runCommand($this->application, 'doctrine:schema:create', ['--em' => $manager]);
}
}
private function dropSchema(): void
{
if ($this->isResetUsingMigrations()) {
$this->dropAndResetDatabase();
return;
}
foreach ($this->objectManagersToReset() as $manager) {
$this->runCommand($this->application, 'doctrine:schema:drop', ['--em' => $manager, '--force' => true]);
}
}
private function dropAndResetDatabase(): void
{
foreach ($this->connectionsToReset() as $connection) {
$databasePlatform = $this->registry->getConnection($connection)->getDatabasePlatform();
if ($databasePlatform instanceof PostgreSQLPlatform) {
// let's drop all connections to the database to be able to drop it
$this->runCommand(
$this->application,
'dbal:run-sql',
[
'--connection' => $connection,
'sql' => 'SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid()',
],
canFail: true,
);
}
$dropParams = ['--connection' => $connection, '--force' => true];
if (!$databasePlatform instanceof SQLitePlatform) {
// sqlite does not support "--if-exists" (ref: https://github.com/doctrine/dbal/pull/2402)
$dropParams['--if-exists'] = true;
}
$this->runCommand($this->application, 'doctrine:database:drop', $dropParams);
if (!$databasePlatform instanceof SQLitePlatform) {
// d:d:create not supported on SQLite
$this->runCommand($this->application, 'doctrine:database:create', ['--connection' => $connection]);
}
}
}
/** @return list<string> */
private function connectionsToReset(): array
{
if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) {
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of environment variable "FOUNDRY_RESET_CONNECTIONS" is deprecated. Please use bundle configuration: "database_resetter.orm.connections: true".');
return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']);
}
return $this->connectionsToReset ?: [$this->registry->getDefaultConnectionName()];
}
/** @return list<string> */
private function objectManagersToReset(): array
{
if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) {
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of environment variable "FOUNDRY_RESET_OBJECT_MANAGERS" is deprecated. Please use bundle configuration: "database_resetter.orm.object_managers: true".');
return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']);
}
return $this->objectManagersToReset ?: [$this->registry->getDefaultManagerName()];
}
private function isResetUsingMigrations(): bool
{
if (isset($_SERVER['FOUNDRY_RESET_MODE'])) {
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of environment variable "FOUNDRY_RESET_MODE" is deprecated. Please use bundle configuration: "database_resetter.orm.reset_mode: true".');
return self::RESET_MODE_MIGRATE === $_SERVER['FOUNDRY_RESET_MODE'];
}
return self::RESET_MODE_MIGRATE === $this->resetMode;
}
}