From 82e724589b8da9b00ce5fe9eb9244beaa01827e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 21 Jul 2017 16:28:12 +0200 Subject: [PATCH] Adding utility method to create Migration classes instances in installers --- .../Patcher/DatabasePatchInstaller.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Mouf/Database/Patcher/DatabasePatchInstaller.php b/src/Mouf/Database/Patcher/DatabasePatchInstaller.php index 1831177..830cdb4 100644 --- a/src/Mouf/Database/Patcher/DatabasePatchInstaller.php +++ b/src/Mouf/Database/Patcher/DatabasePatchInstaller.php @@ -21,6 +21,7 @@ namespace Mouf\Database\Patcher; use Doctrine\DBAL\Connection; +use Mouf\Actions\InstallUtils; use Mouf\ClassProxy; use Mouf\Installer\PackageInstallerInterface; use Mouf\InstanceProxy; @@ -50,7 +51,7 @@ class DatabasePatchInstaller * @param string $upSqlFileName The SQL file containing the patch, relative to ROOT_PATH. Should not start with /. * @param string $downSqlFileName (optional) The SQL file containing the revert patch, relative to ROOT_PATH. Should not start with /. */ - public static function registerPatch(MoufManager $moufManager, $uniqueName, $description, $upSqlFileName, $downSqlFileName = null) + public static function registerPatch(MoufManager $moufManager, string $uniqueName, string $description, string $upSqlFileName, string $downSqlFileName = null): void { // First, let's find if this patch already exists... We assume that $uniqueName = "dbpatch.$instanceName". @@ -83,6 +84,35 @@ public static function registerPatch(MoufManager $moufManager, $uniqueName, $des } } + /** + * Registers a database migration patch in the patch system. + * Note: the patch will not be executed, only registered in "Awaiting" state. + * The user will have to manually execute the patch. + * + * Note: if the patch already exists, we will update this instance. + * + */ + public static function registerMigrationPatch(MoufManager $moufManager, string $className): void + { + // If the patch already exists, we go in edit mode. + $exists = $moufManager->has($className); + + $patchDescriptor = InstallUtils::getOrCreateInstance($className, $className, $moufManager); + + $patchDescriptor->getProperty('patchConnection')->setValue($moufManager->getInstanceDescriptor('patchConnection')); + + // Register the patch in the patchService. + if (!$exists) { + $patchManager = $moufManager->getInstanceDescriptor('patchService'); + $patchs = $patchManager->getProperty('patchs')->getValue(); + if ($patchs === null) { + $patchs = array(); + } + $patchs[] = $patchDescriptor; + $patchManager->getProperty('patchs')->setValue($patchs); + } + } + public static function generatePatch(MoufManager $moufManager, $description, $instanceName, $selfedit = 'false') {