From b0e10c6f398d05fda95afd74da16058c0e9b68df Mon Sep 17 00:00:00 2001
From: Sebastian Castro <sebastian.castro@protonmail.com>
Date: Thu, 21 Jan 2021 10:56:08 +0100
Subject: [PATCH] Fix migration command

---
 src/Command/MigrationCommand.php | 41 ++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/Command/MigrationCommand.php b/src/Command/MigrationCommand.php
index 329d30330..25c311b18 100644
--- a/src/Command/MigrationCommand.php
+++ b/src/Command/MigrationCommand.php
@@ -6,6 +6,7 @@ use App\Document\MigrationState;
 use App\Services\AsyncService;
 use Doctrine\ODM\MongoDB\DocumentManager;
 use App\Services\DocumentManagerFactory;
+use Symfony\Component\Console\Command\Command;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -15,7 +16,7 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt
  * Command to update database when schema need migration
  * Also provide some update message in the admin dashboard.
  */
-class MigrationCommand extends GoGoAbstractCommand
+class MigrationCommand extends Command
 {
     // -----------------------------------------------------------------
     // DO NOT REMOVE A SINGLE ELEMENT OF THOSE ARRAYS, ONLY ADD NEW ONES
@@ -76,22 +77,28 @@ class MigrationCommand extends GoGoAbstractCommand
         "La connexion via un compte tiers (Google, Facebook, LesCommuns.org) est maintenant possible ! Changez la configuration dans Utilisateurs -> Configuration"
     ];
 
-    public function __construct(DocumentManagerFactory $dm, LoggerInterface $commandsLogger,
+    public function __construct(DocumentManagerFactory $dmFactory, LoggerInterface $commandsLogger,
                                TokenStorageInterface $security,
                                AsyncService $asyncService)
     {
         $this->asyncService = $asyncService;
-        parent::__construct($dm, $commandsLogger, $security);
+        $this->dm = $dmFactory->getRootManager();
+        $this->dmFactory = $dmFactory;
+        $this->logger = $commandsLogger;
+        $this->security = $security;
+        parent::__construct();
     }
 
-    protected function gogoConfigure(): void
+    protected function configure(): void
     {
         $this->setName('db:migrate')
              ->setDescription('Update datatabse each time after code update');
     }
 
-    protected function gogoExecute(DocumentManager $dm, InputInterface $input, OutputInterface $output): void
+    protected function execute(InputInterface $input, OutputInterface $output): void
     {
+        $dm = $this->dm;
+        $this->output = $output;
         $migrationState = $dm->query('MigrationState')->getQuery()->getSingleResult();
         if (null == $migrationState) { // Meaning the migration state was not yet in the place in the code
             $migrationState = new MigrationState();
@@ -109,11 +116,10 @@ class MigrationCommand extends GoGoAbstractCommand
                 $migrationsToRun = array_unique($migrationsToRun);
                 foreach ($dbs as $db) {
                     foreach ($migrationsToRun as $migration) {
-                        $this->log('run migration '.$migration.' on project '.$db);
+                        $this->log('run migration '.$migration, $db);
                         $this->runMongoCommand($dm, $db, $migration);
                     }
                 }
-                $this->log(count($migrationsToRun).' migrations performed');
             } else {
                 $this->log('No Migrations to perform');
             }
@@ -123,10 +129,9 @@ class MigrationCommand extends GoGoAbstractCommand
             if (count(self::$commands) > $migrationState->getCommandsIndex()) {
                 $commandsToRun = array_slice(self::$commands, $migrationState->getCommandsIndex());
                 $commandsToRun = array_unique($commandsToRun);
-                $this->log(count($commandsToRun).' commands to run');
                 foreach ($dbs as $db) {
                     foreach ($commandsToRun as $command) {
-                        $this->log('call command '.$command.' on project '.$db);
+                        $this->log('call command '.$command, $db);
                         $this->asyncService->callCommand($command, [], $db);
                     }
                 }
@@ -136,15 +141,13 @@ class MigrationCommand extends GoGoAbstractCommand
 
             if (count(self::$messages) > $migrationState->getMessagesIndex()) {
                 $messagesToAdd = array_slice(self::$messages, $migrationState->getMessagesIndex());
-                $this->log(count($messagesToAdd).' messages to add');
                 foreach ($dbs as $db) {
-                    $this->log('add message on project '.$db);
+                    $this->log(count($messagesToAdd).' messages to add', $db);
                     foreach ($messagesToAdd as $message) {
                         // create a GoGoLogUpdate
                         $this->asyncService->callCommand('gogolog:add:message', ['"'.$message.'"'], $db);
                     }
                 }
-                $this->log(count($messagesToAdd).' messages added to admin dashboard');
             } else {
                 $this->log('No Messages to add to dashboard');
             }
@@ -165,4 +168,18 @@ class MigrationCommand extends GoGoAbstractCommand
         $db = $mongo->selectDB($dbName);
         return $db->execute($command);
     }
+
+    protected function log($message, $db = null)
+    {
+        if ($db) $message = "DB {$db} : $message";
+        $this->logger->info($message);
+        $this->output->writeln($message);
+    }
+
+    protected function error($message, $db = null)
+    {
+        if ($db) $message = "DB {$this->db} : $message";
+        $this->logger->error($message);
+        $this->output->writeln('ERROR '.$message);        
+    }
 }
-- 
GitLab