MigrationCommand.php 4.52 KB
Newer Older
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
<?php

namespace Biopen\CoreBundle\Command;

use Biopen\SaasBundle\Command\GoGoAbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Biopen\CoreBundle\Document\MigrationState;
use Biopen\CoreBundle\Document\GoGoLogUpdate;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Process\Process;
/**
 * Command to update database when schema need migration
 * Also provide some update message in the admin dashboard
 */
class MigrationCommand extends GoGoAbstractCommand
{
    protected function gogoConfigure()
    {
        $this->setName('db:migrate')
             ->setDescription('Update datatabse each time after code update');
    }

    protected function gogoExecute($em, InputInterface $input, OutputInterface $output)
    {       
        $migrationState = $em->createQueryBuilder('BiopenCoreBundle:MigrationState')->getQuery()->getSingleResult();
        if ($migrationState == null) // Meaning the migration state was not yet in the place in the code
        {
            $migrationState = new MigrationState();
            $em->persist($migrationState);
        }

        // Collecting the Database to be updated
        $dbs = ['gogocarto_default'];
        $dbNames = $em->createQueryBuilder('BiopenSaasBundle:Project')->select('domainName')->hydrate(false)->getQuery()->execute()->toArray();            
        foreach ($dbNames as $object) { $dbs[] = $object['domainName']; }

        if (count($this->migrations) > $migrationState->getMigrationIndex()) {
            $migrationsToRun = array_slice($this->migrations, $migrationState->getMigrationIndex());
            foreach($dbs as $db) {
                foreach($migrationsToRun as $migration) {
                    $this->runCommand($db, $migration);
                }                    
            }
            $this->log(count($migrationsToRun) . " migrations performed");
        } else {
            $this->log("No Migrations to perform");
        }

        $asyncService = $this->getContainer()->get('biopen.async');
Sebastian Castro's avatar
Sebastian Castro committed
51 52 53 54 55 56 57 58 59 60 61 62
        if (count($this->commands) > $migrationState->getCommandsIndex()) {
            $commandsToRun = array_slice($this->commands, $migrationState->getCommandsIndex());
            foreach($dbs as $db) {
                foreach($commandsToRun as $command) {            
                    $asyncService->callCommand($command, [], $db);
                }                    
            }
            $this->log(count($commandsToRun) . " commands to run");
        } else {
            $this->log("No commands to run");
        }

63 64 65 66 67 68 69 70 71 72 73 74 75 76
        if (count($this->messages) > $migrationState->getMessagesIndex()) {
            $messagesToAdd = array_slice($this->messages, $migrationState->getMessagesIndex());
            foreach($dbs as $db) {
                foreach($messagesToAdd as $message) {
                    // create a GoGoLogUpdate                    
                    $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");
        }

        $migrationState->setMigrationIndex(count($this->migrations));
Sebastian Castro's avatar
Sebastian Castro committed
77
        $migrationState->setCommandsIndex(count($this->commands));
78 79 80 81 82 83 84 85 86 87
        $migrationState->setMessagesIndex(count($this->messages));
        $em->flush();        
    }

    private function runCommand($db, $command)
    {
        $process = new Process("mongo {$db} --eval \"{$command}\"");
        return $process->start();
    }
    
Sebastian Castro's avatar
Sebastian Castro committed
88 89 90
    // -----------------------------------------------------------------
    // DO NOT REMOVE A SINGLE ELEMENT OF THOSE ARRAYS, ONLY ADD NEW ONES
    // -----------------------------------------------------------------
91 92 93 94 95 96
    public $migrations = [
      // March 2019
      // "db.Category.renameCollection('CategoryGroup')",
      // "db.Option.renameCollection('Category')"
    ];

Sebastian Castro's avatar
Sebastian Castro committed
97 98 99 100 101
    public $commands = [
      // v2.3.1
      "app:elements:updateJson all"
    ];

102
    public $messages = [
Sebastian Castro's avatar
Sebastian Castro committed
103
        // v2.3.0
Sebastian Castro's avatar
Sebastian Castro committed
104 105 106
        "Un champ <b>Image (url)</b> est maintenant disponible dans la confiugration du formulaire !",
        "Vous pouvez désormais customizer la popup qui s'affiche au survol d'un marqueur. Allez dans Personnalisation -> Marqueur / Popup",
        "Nouvelle option pour le menu (Personnalisation -> La Carte -> onglet Menu) : afficher à côté de chaque catégories le nombre d'élements disponible pour cette catégorie"
107 108
    ];
}