/** * Updates DB Schema. Changes from Diamante only will be applied for current schema. Other bundles updating skips * @throws \Exception if there are no changes in entities */ protected function updateDbSchema() { /** * @var $em \Doctrine\ORM\EntityManager */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $event = $em->getEventManager(); $sm = $em->getConnection()->getSchemaManager(); $allMetadata = $em->getMetadataFactory()->getAllMetadata(); $schemaTool = new SchemaTool($em); $entitiesMetadata = array($em->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\MessageReference::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\TicketHistory::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\WatcherList::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\TicketTimeline::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Audit::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\AuditField::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Article::getClassName())); $event->disableListeners(); $currentSchema = $sm->createSchema(); $schemaFromMetadata = $schemaTool->getSchemaFromMetadata($allMetadata); $entitiesSchema = $schemaTool->getSchemaFromMetadata($entitiesMetadata); $entitiesTables = $entitiesSchema->getTables(); $entitiesTableName = array_keys($entitiesTables); $currentDiamanteSchema = $this->getTargetSchema($currentSchema, $entitiesTableName); $diamanteSchemaFromMetadata = $this->getTargetSchema($schemaFromMetadata, $entitiesTableName); $comparator = new Comparator(); $diff = $comparator->compare($currentDiamanteSchema, $diamanteSchemaFromMetadata); $toUpdate = $diff->toSql($em->getConnection()->getDatabasePlatform()); if (empty($toUpdate)) { throw new \Exception('No new updates found. DiamanteDesk is up to date!'); } $conn = $em->getConnection(); foreach ($toUpdate as $sql) { $conn->executeQuery($sql); } }
/** * Get list of Diamante tables to purge data from * * @return array */ protected function getTablesList() { $entitiesMetadata = array($this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName())); $toPurge = array(); /** @var $entity \Doctrine\ORM\Mapping\ClassMetadata */ foreach ($entitiesMetadata as $entity) { $tableName = $entity->getTableName(); $toPurge[] = $tableName; } return $toPurge; }
/** * Get list of Diamante tables to purge data from * * @return array */ protected function getTablesList() { $entitiesMetadata = array($this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\TicketHistory::getClassName()), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tag'), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tagging')); $toPurge = array(); $quoteStrategy = new DefaultQuoteStrategy(); /** @var $entity \Doctrine\ORM\Mapping\ClassMetadata */ foreach ($entitiesMetadata as $entity) { $tableName = $entity->getTableName(); $toPurge[] = $tableName; foreach ($entity->getAssociationMappings() as $assoc) { if (isset($assoc['joinTable'])) { $toPurge[] = $quoteStrategy->getJoinTableName($assoc, $entity, $this->dbPlatform); } } } return $toPurge; }