Skip to content

Commit a573af0

Browse files
authored
Merge pull request #31 from moufmouf/feature/reset
Support for the "reset" feature.
2 parents 9f34d2c + 1e637b1 commit a573af0

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": ">=7.1",
1717
"mouf/database.doctrine-dbal-wrapper": "^1.0",
18-
"mouf/utils.patcher": "^2.0",
18+
"mouf/utils.patcher": "^2.1",
1919
"mouf/classname-mapper": "^1.0",
2020
"symfony/filesystem": "^2.0 || ^3.0",
2121
"thecodingmachine/dbal-fluid-schema-builder": "^1.0"
@@ -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/AbstractDatabasePatch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected function saveDbSchema()
154154
{
155155
$schema = $this->patchConnection->getConnection()->getSchemaManager()->createSchema();
156156
file_put_contents(__DIR__.'/../../../../generated/schema', serialize($schema));
157-
chmod(__DIR__.'/../../../../generated/schema', 0664);
157+
@chmod(__DIR__.'/../../../../generated/schema', 0664);
158158
}
159159

160160
/**

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

+10-6
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

+26-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
namespace Mouf\Database\Patcher;
2222

2323
use Doctrine\DBAL\Connection;
24+
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
25+
use Mouf\Utils\Patcher\PatchListenerInterface;
2426

2527

2628
/**
2729
* Class representing the patches table in database
2830
*
2931
* @author Pierre Vaidie
3032
*/
31-
class PatchConnection
33+
class PatchConnection implements PatchListenerInterface
3234
{
3335
/**
3436
* @var string
@@ -39,16 +41,22 @@ class PatchConnection
3941
* @var Connection
4042
*/
4143
private $dbalConnection;
44+
/**
45+
* @var Connection
46+
*/
47+
private $dbalRootConnection;
4248

4349
/**
4450
* DatabasePatchTable constructor.
45-
* @param string $tableName
46-
* @param Connection $dbalConnection
51+
* @param string $tableName
52+
* @param Connection $dbalConnection
53+
* @param Connection|null $dbalRootConnection The DBAL connection with "root" credentials to drop and create the database again.
4754
*/
48-
public function __construct($tableName, Connection $dbalConnection)
55+
public function __construct($tableName, Connection $dbalConnection, Connection $dbalRootConnection = null)
4956
{
5057
$this->tableName = $tableName;
5158
$this->dbalConnection = $dbalConnection;
59+
$this->dbalRootConnection = $dbalRootConnection ?: $dbalConnection;
5260
}
5361

5462
/**
@@ -86,4 +94,18 @@ public function getConnection() {
8694
public function setConnnection(Connection $dbalConnection) {
8795
$this->dbalConnection = $dbalConnection;
8896
}
97+
98+
/**
99+
* Triggered when the 'reset()' method is called on the PatchService
100+
*/
101+
public function onReset(): void
102+
{
103+
// Let's drop and recreate the database from 0!
104+
$dbName = $this->dbalConnection->getDatabase();
105+
$this->dbalRootConnection->getSchemaManager()->dropAndCreateDatabase($dbName);
106+
107+
if ($this->dbalRootConnection->getDriver() instanceof AbstractMySQLDriver) {
108+
$this->dbalRootConnection->exec('USE '.$dbName);
109+
}
110+
}
89111
}

0 commit comments

Comments
 (0)