Skip to content

Commit c47654a

Browse files
committed
Adding a listener to support the notion of "reset" of the database.
1 parent 9f34d2c commit c47654a

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"install": [
3131
{
3232
"type": "class",
33-
"class": "Mouf\\Database\\Patcher\\DatabasePatchInstaller2",
33+
"class": "Mouf\\Database\\Patcher\\DatabasePatchInstaller3",
3434
"description": "Create the 'patches' table in your database to track patches applied."
3535
}
3636
],

src/Mouf/Database/Patcher/DatabasePatchInstaller2.php renamed to src/Mouf/Database/Patcher/DatabasePatchInstaller3.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* @author David Negrier <[email protected]>
3737
*/
38-
class DatabasePatchInstaller2 implements PackageInstallerInterface
38+
class DatabasePatchInstaller3 implements PackageInstallerInterface
3939
{
4040
/**
4141
* (non-PHPdoc)
@@ -48,7 +48,7 @@ public static function install(MoufManager $moufManager) {
4848
$dbConnectionDescriptor = $moufManager->getInstanceDescriptor('dbalConnection');
4949

5050
//Let's get the instance of PatchConnection or create it if not exist
51-
if ($moufManager->has('patchConnection')) {
51+
if ($moufManager->has('patchConnection')) {
5252
$patchConnection = $moufManager->get('patchConnection');
5353
$patchConnectionDescriptor = $moufManager->getInstanceDescriptor('patchConnection');
5454
} else {
@@ -60,11 +60,15 @@ public static function install(MoufManager $moufManager) {
6060
$patchConnection = $moufManager->get('patchConnection');
6161
}
6262

63-
// Let's create the table.
6463
$existingPatches = $moufManager->findInstances("Mouf\\Database\\Patcher\\DatabasePatch");
65-
foreach($existingPatches as $existingPatche){
66-
$patchIntance = $moufManager->getInstanceDescriptor($existingPatche);
67-
$patchIntance->getProperty('patchConnection')->setValue($patchConnectionDescriptor);
64+
foreach($existingPatches as $existingPatch) {
65+
$patchInstance = $moufManager->getInstanceDescriptor($existingPatch);
66+
$patchInstance->getProperty('patchConnection')->setValue($patchConnectionDescriptor);
67+
}
68+
69+
$patchServiceDescriptor = $moufManager->getInstanceDescriptor('patchService');
70+
if ($patchServiceDescriptor->getProperty('listeners')->getValue() === null) {
71+
$patchServiceDescriptor->getProperty('listeners')->setValue([ $patchConnectionDescriptor ]);
6872
}
6973

7074
// Finally, let's change the dbalConnection configuration to add an ignore rule on the "patches" table.

src/Mouf/Database/Patcher/PatchConnection.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
namespace Mouf\Database\Patcher;
2222

2323
use Doctrine\DBAL\Connection;
24+
use Mouf\Utils\Patcher\PatchListenerInterface;
2425

2526

2627
/**
2728
* Class representing the patches table in database
2829
*
2930
* @author Pierre Vaidie
3031
*/
31-
class PatchConnection
32+
class PatchConnection implements PatchListenerInterface
3233
{
3334
/**
3435
* @var string
@@ -39,16 +40,22 @@ class PatchConnection
3940
* @var Connection
4041
*/
4142
private $dbalConnection;
43+
/**
44+
* @var Connection
45+
*/
46+
private $dbalRootConnection;
4247

4348
/**
4449
* DatabasePatchTable constructor.
45-
* @param string $tableName
46-
* @param Connection $dbalConnection
50+
* @param string $tableName
51+
* @param Connection $dbalConnection
52+
* @param Connection|null $dbalRootConnection The DBAL connection with "root" credentials to drop and create the database again.
4753
*/
48-
public function __construct($tableName, Connection $dbalConnection)
54+
public function __construct($tableName, Connection $dbalConnection, Connection $dbalRootConnection = null)
4955
{
5056
$this->tableName = $tableName;
5157
$this->dbalConnection = $dbalConnection;
58+
$this->dbalRootConnection = $dbalRootConnection ?: $dbalConnection;
5259
}
5360

5461
/**
@@ -86,4 +93,14 @@ public function getConnection() {
8693
public function setConnnection(Connection $dbalConnection) {
8794
$this->dbalConnection = $dbalConnection;
8895
}
96+
97+
/**
98+
* Triggered when the 'reset()' method is called on the PatchService
99+
*/
100+
public function onReset(): void
101+
{
102+
// Let's drop and recreate the database from 0!
103+
$dbName = $this->dbalConnection->getDatabase();
104+
$this->dbalRootConnection->getSchemaManager()->dropAndCreateDatabase($dbName);
105+
}
89106
}

0 commit comments

Comments
 (0)