Skip to content
Snippets Groups Projects
Select Git revision
  • 5c4d4b5b015bf18e5285121226ad3a1fe68f7ba3
  • master default protected
  • release_4.0.0
  • laminas_migration
  • 4.0.0
  • 3.0.3
  • 3.0.2
  • 3.0.1
  • 3.0.0
  • 1.2.15
  • 1.2.14
  • 1.2.13
  • 1.2.12
  • 1.2.11
  • 1.2.10
  • 1.2.9
  • 1.2.8
  • 1.2.7
  • 1.2.6
  • 1.2.5
  • 1.2.4
  • 1.2.3
  • 1.2.2
  • 1.2.1
24 results

IndexController.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    IndexController.php 6.07 KiB
    <?php
    
    namespace UnicaenOracle\Controller;
    
    use Doctrine\DBAL\Connection;
    use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
    use UnicaenOracle\Service\Traits\DataServiceAwareTrait;
    use UnicaenOracle\Service\Traits\SchemaServiceAwareTrait;
    use Zend\Log\LoggerAwareTrait;
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\ServiceManager\ServiceLocatorAwareTrait;
    
    class IndexController extends AbstractActionController implements ServiceLocatorAwareInterface
    {
        use ServiceLocatorAwareTrait;
        use SchemaServiceAwareTrait;
        use DataServiceAwareTrait;
        use LoggerAwareTrait;
    
        /**
         * Action en mode CLI.
         */
        public function generateScriptForSchemaClearingConsoleAction()
        {
            $destDir = $this->params('output-dir', '/tmp'); // ex: '/tmp'
            $connName = $this->params('connection', 'doctrine.connection.orm_default');
    
            /** @var Connection $connection */
            $connection = $this->getServiceLocator()->get($connName);
            $schemaName = $this->schemaService->extractSchemaNameFromConnection($connection);
    
            $this->log("=====================================");
            $this->log(" Db schema clearing script generator");
            $this->log("=====================================");
    
            $this->log("# Generating database clearing scripts...");
            $outputFilePath = $destDir . "/oracle-clear-schema-$schemaName.sql";
            $this->schemaService->createSchemaClearingScriptFile($connection, $outputFilePath);
            $this->log($outputFilePath);
    
            $this->log("Done.");
    
            exit(0);
        }
    
        /**
         * Action en mode CLI.
         */
        public function generateScriptForSchemaCreationConsoleAction()
        {
            $destDir = $this->params('output-dir', '/tmp'); // ex: '/tmp'
            $srcConnName = $this->params('src-connection', 'doctrine.connection.orm_default');
            $dstConnName = $this->params('dst-connection', 'doctrine.connection.orm_default');
            $refConstraintsIncluded = (bool) $this->params('ref-constraints-included', 1);
    
            /** @var Connection $srcConn */
            /** @var Connection $dstConn */
            $srcConn = $this->getServiceLocator()->get($srcConnName);
            $dstConn = $this->getServiceLocator()->get($dstConnName);
    
            $srcSchemaName = $this->schemaService->extractSchemaNameFromConnection($srcConn);
            $dstSchemaName = $this->schemaService->extractSchemaNameFromConnection($dstConn);
    
            $this->log("=====================================");
            $this->log(" Db schema creation script generator");
            $this->log("=====================================");
    
            $this->log("# Generating schema creation script " . ($refConstraintsIncluded ? 'with' : 'without') . ' ref constraints...');
            $outputFilePath = $destDir . "/oracle-generate-schema-$dstSchemaName-from-$srcSchemaName.sql";
            $this->schemaService->createSchemaCreationScriptFile($srcConn, $dstConn, $refConstraintsIncluded, $outputFilePath);
            $this->log($outputFilePath);
    
            $this->log("Done.");
    
            exit(0);
        }
    
        /**
         * Action en mode CLI.
         */
        public function generateScriptsForDataInsertsConsoleAction()
        {
            $destDir = $this->params('output-dir', '/tmp'); // ex: '/tmp'
            $tableNames = $this->params('tables');
            $srcConnName = $this->params('src-connection', 'doctrine.connection.orm_default');
            $dstConnName = $this->params('dst-connection', 'doctrine.connection.orm_default');
    
            /** @var Connection $srcConn */
            /** @var Connection $dstConn */
            $srcConn = $this->getServiceLocator()->get($srcConnName);
            $dstConn = $this->getServiceLocator()->get($dstConnName);
    
            $srcSchemaName = $this->schemaService->extractSchemaNameFromConnection($srcConn);
            $dstSchemaName = $this->schemaService->extractSchemaNameFromConnection($dstConn);
    
            $this->log("===================================");
            $this->log(" Db data inserts scripts generator");
            $this->log("===================================");
    
            $this->log("# Generating data inserts scripts...");
            $tableNames = array_filter(array_map('trim', explode(',', $tableNames)));
            $outputFilePathTemplate = $destDir . "/oracle-data-insert-from-$srcSchemaName.%s-into-$dstSchemaName.sql";
            $outputFilePaths = $this->dataService->createDataInsertsScriptFile($srcConn, $dstSchemaName, $tableNames, $outputFilePathTemplate);
            foreach ($outputFilePaths as $outputFilePath) {
                $this->log($outputFilePath);
            }
    
            $this->log("Done.");
    
            exit(0);
        }
    
        /**
         * Action en mode CLI.
         */
        public function generateScriptForRefConstraintsCreationConsoleAction()
        {
            $destDir = $this->params('output-dir', '/tmp'); // ex: '/tmp'
            $srcConnName = $this->params('src-connection', 'doctrine.connection.orm_default');
            $dstConnName = $this->params('dst-connection', 'doctrine.connection.orm_default');
    
            /** @var Connection $srcConn */
            /** @var Connection $dstConn */
            $srcConn = $this->getServiceLocator()->get($srcConnName);
            $dstConn = $this->getServiceLocator()->get($dstConnName);
    
            $srcSchemaName = $this->schemaService->extractSchemaNameFromConnection($srcConn);
            $dstSchemaName = $this->schemaService->extractSchemaNameFromConnection($dstConn);
    
            $this->log("==============================================");
            $this->log(" Db ref constraints creation script generator");
            $this->log("==============================================");
    
            $this->log("# Generating ref constraints creation script...");
            $outputFilePath = $destDir . "/oracle-generate-ref-constraints-$dstSchemaName-from-$srcSchemaName.sql";
            $this->schemaService->createRefConstraintsCreationScriptFile($srcConn, $dstConn, $outputFilePath);
            $this->log($outputFilePath);
    
            $this->log("Done.");
    
            exit(0);
        }
    
        /**
         * @param string $message
         */
        private function log($message)
        {
            if ($this->logger !== null) {
                $this->logger->info($message);
            } else {
                echo $message . PHP_EOL;
            }
        }
    }