/**
  * Runs command
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln($this->getDescription());
     $this->overrideRemoveNamespacedAssets();
     $this->overrideSchemaDiff();
     /** @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     /** @var ConfigManager $configManager */
     $configManager = $this->getContainer()->get('oro_entity_config.config_manager');
     $metadata = array_filter($em->getMetadataFactory()->getAllMetadata(), function ($doctrineMetadata) use($configManager) {
         /** @var ClassMetadataInfo $doctrineMetadata */
         return $this->isExtendEntity($doctrineMetadata->getReflectionClass()->getName(), $configManager);
     });
     $schemaTool = new SaveSchemaTool($em);
     $sqls = $schemaTool->getUpdateSchemaSql($metadata, true);
     if (0 === count($sqls)) {
         $output->writeln('Nothing to update - a database is already in sync with the current entity metadata.');
     } else {
         if ($input->getOption('dry-run')) {
             $output->writeln(implode(';' . PHP_EOL, $sqls) . ';');
         } else {
             $output->writeln('Updating database schema...');
             $schemaTool->updateSchema($metadata, true);
             $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls)));
         }
     }
     if (!$input->getOption('dry-run')) {
         /** @var EnumSynchronizer $enumSynchronizer */
         $enumSynchronizer = $this->getContainer()->get('oro_entity_extend.enum_synchronizer');
         $enumSynchronizer->sync();
     }
 }
Example #2
0
 public function testGetUpdateSchemaSqlShouldAffectOnlyKnownDBTableParts()
 {
     list($fromSchema, $toSchema) = $this->prepareSchemas();
     $meta = [new \stdClass()];
     $schemaManager = $this->getMockBuilder('Doctrine\\DBAL\\Schema\\AbstractSchemaManager')->setMethods(['createSchema'])->disableOriginalConstructor()->getMockForAbstractClass();
     $schemaManager->expects($this->once())->method('createSchema')->willReturn($fromSchema);
     $this->connection->expects($this->once())->method('getSchemaManager')->willReturn($schemaManager);
     $this->schemaTool->expects($this->once())->method('getSchemaFromMetadata')->with($meta)->willReturn($toSchema);
     $this->assertEquals(['DROP INDEX oro_idx_index_name ON oro_entity_extend_test_relation'], $this->schemaTool->getUpdateSchemaSql($meta, true));
 }
Example #3
0
 /**
  * Runs command
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln($this->getDescription());
     /**
      * Unfortunately due a poor design of the Doctrine\ORM\Tools\SchemaTool::getSchemaFromMetadata
      * we have to use "class_alias" to replace "Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets"
      * with "Oro\Bundle\EntityExtendBundle\Tools\ExtendSchemaUpdateRemoveNamespacedAssets".
      */
     if (!class_exists('Doctrine\\DBAL\\Schema\\Visitor\\RemoveNamespacedAssets', false)) {
         class_alias('Oro\\Bundle\\EntityExtendBundle\\Tools\\ExtendSchemaUpdateRemoveNamespacedAssets', 'Doctrine\\DBAL\\Schema\\Visitor\\RemoveNamespacedAssets');
     }
     /**
      * to disable automatic rename of autogenerated indices
      * we have to use "class_alias" to replace "Doctrine\DBAL\Schema\SchemaDiff"
      * with "Oro\Bundle\MigrationBundle\Migration\Schema\SchemaDiff"
      */
     if (!class_exists('Doctrine\\DBAL\\Schema\\SchemaDiff', false)) {
         class_alias('Oro\\Bundle\\MigrationBundle\\Migration\\Schema\\SchemaDiff', 'Doctrine\\DBAL\\Schema\\SchemaDiff');
     }
     /** @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     /** @var ConfigManager $configManager */
     $configManager = $this->getContainer()->get('oro_entity_config.config_manager');
     $metadata = array_filter($em->getMetadataFactory()->getAllMetadata(), function ($doctrineMetadata) use($configManager) {
         /** @var ClassMetadataInfo $doctrineMetadata */
         return $this->isExtendEntity($doctrineMetadata->getReflectionClass()->getName(), $configManager);
     });
     $schemaTool = new SaveSchemaTool($em);
     $sqls = $schemaTool->getUpdateSchemaSql($metadata, true);
     if (0 === count($sqls)) {
         $output->writeln('Nothing to update - a database is already in sync with the current entity metadata.');
     } else {
         if ($input->getOption('dry-run')) {
             $output->writeln(implode(';' . PHP_EOL, $sqls) . ';');
         } else {
             $output->writeln('Updating database schema...');
             $schemaTool->updateSchema($metadata, true);
             $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls)));
         }
     }
     if (!$input->getOption('dry-run')) {
         /** @var EnumSynchronizer $enumSynchronizer */
         $enumSynchronizer = $this->getContainer()->get('oro_entity_extend.enum_synchronizer');
         $enumSynchronizer->sync();
     }
 }