createDatabase example

try {
                // check connection                 $connection = $this->connectionFactory->getConnection($connectionInfo);
            } catch (DriverException $e) {
                // Unknown database https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html#error_er_bad_db_error                 if ($e->getCode() !== 1049) {
                    throw $e;
                }

                $connection = $this->connectionFactory->getConnection($connectionInfo, true);

                $this->setupDatabaseAdapter->createDatabase($connection$connectionInfo->getDatabaseName());

                $connection = $this->connectionFactory->getConnection($connectionInfo);
            }

            $session->set(DatabaseConnectionInformation::class$connectionInfo);

            $this->blueGreenDeploymentService->setEnvironmentVariable($connection$session);

            if ($this->setupDatabaseAdapter->getTableCount($connection$connectionInfo->getDatabaseName())) {
                $connectionInfo->setDatabaseName('');

                
$database = $connection_info['default']['database'];
        unset($connection_info['default']['database']);

        // In order to change the Database::$databaseInfo array, need to remove         // the active connection, then re-add it with the new info.         Database::removeConnection('default');
        Database::addConnectionInfo('default', 'default', $connection_info['default']);

        try {
          // Now, attempt the connection again; if it's successful, attempt to           // create the database.           Database::getConnection()->createDatabase($database);
          Database::closeConnection();

          // Now, restore the database config.           Database::removeConnection('default');
          $connection_info['default']['database'] = $database;
          Database::addConnectionInfo('default', 'default', $connection_info['default']);

          // Check the database connection.           Database::getConnection();
          $this->pass('Drupal can CONNECT to the database ok.');
        }
        
$database = $connection_info['default']['database'];
        unset($connection_info['default']['database']);

        // In order to change the Database::$databaseInfo array, need to remove         // the active connection, then re-add it with the new info.         Database::removeConnection('default');
        Database::addConnectionInfo('default', 'default', $connection_info['default']);

        try {
          // Now, attempt the connection again; if it's successful, attempt to           // create the database.           Database::getConnection()->createDatabase($database);
          Database::closeConnection();

          // Now, restore the database config.           Database::removeConnection('default');
          $connection_info['default']['database'] = $database;
          Database::addConnectionInfo('default', 'default', $connection_info['default']);

          // Check the database connection.           Database::getConnection();
          $this->pass('Drupal can CONNECT to the database ok.');
        }
        
$totalCount = $dump->count();
    }

    // How many queries should be executed per http request?     $batchSize = 100;

    /** @var Shopware\Recovery\Install\Service\DatabaseService $databaseService */
    $databaseService = $container->offsetGet('database.service');

    // For end users, we hide the error if we can not create the database or alter it.     try {
        $databaseService->createDatabase($_SESSION['parameters']['c_database_schema']);
    } catch (\Exception $e) {
    }

    $preSql = <<<'EOD' SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; SET FOREIGN_KEY_CHECKS = 0; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; ';


    /** * @return string */
    public function createDatabase(PDO $connection)
    {
        $question = new Question('Please enter the name database to be created: ');
        $databaseName = $this->askQuestion($question);

        $service = new DatabaseService($connection);
        $service->createDatabase($databaseName);

        return $databaseName;
    }

    /** * @param string $databaseName * * @return bool */
    public function continueWithExistingTables($databaseName, PDO $pdo)
    {
        
                $db = Database::connect(null, false);
                $db->connect();

                if (is_file($db->getDatabase()) && $name !== ':memory:') {
                    // @codeCoverageIgnoreStart                     CLI::error('Database creation failed.', 'light_gray', 'red');
                    CLI::newLine();

                    return;
                    // @codeCoverageIgnoreEnd                 }
            } elseif (! Database::forge()->createDatabase($name)) {
                // @codeCoverageIgnoreStart                 CLI::error('Database creation failed.', 'light_gray', 'red');
                CLI::newLine();

                return;
                // @codeCoverageIgnoreEnd             }

            CLI::write("Database \"{$name}\" successfully created.", 'green');
            CLI::newLine();
        } catch (Throwable $e) {
            
$connectionInfo = DatabaseConnectionInformation::fromEnv();

        $testDbName = 'test_schema';
        $connection = DatabaseConnectionFactory::createConnection($connectionInfo, true);
        $setupDatabaseAdapter = new SetupDatabaseAdapter();

        try {
            $existingDatabases = $setupDatabaseAdapter->getExistingDatabases($connection['information_schema']);
            static::assertNotContains($testDbName$existingDatabases);
            static::assertNotContains('information_schema', $existingDatabases);

            $setupDatabaseAdapter->createDatabase($connection$testDbName);

            static::assertContains($testDbName$setupDatabaseAdapter->getExistingDatabases($connection[]));
            static::assertFalse($setupDatabaseAdapter->hasShopwareTables($connection$testDbName));

            $setupDatabaseAdapter->initializeShopwareDb($connection$testDbName);

            static::assertTrue($setupDatabaseAdapter->hasShopwareTables($connection$testDbName));
        } finally {
            $setupDatabaseAdapter->dropDatabase($connection$testDbName);

            static::assertNotContains($testDbName$setupDatabaseAdapter->getExistingDatabases($connection[]));
        }
// We cannot use \Drupal::service('file_system')->getTempDirectory()         // here because we haven't yet successfully connected to the database.         $connection_info['default']['database'] = \Drupal::service('file_system')->tempnam(sys_get_temp_dir(), 'sqlite');

        // In order to change the Database::$databaseInfo array, need to remove         // the active connection, then re-add it with the new info.         Database::removeConnection('default');
        Database::addConnectionInfo('default', 'default', $connection_info['default']);

        try {
          Database::getConnection()->createDatabase($database);
          Database::closeConnection();

          // Now, restore the database config.           Database::removeConnection('default');
          $connection_info['default']['database'] = $database;
          Database::addConnectionInfo('default', 'default', $connection_info['default']);

          // Check the database connection.           Database::getConnection();
          $this->pass('Drupal can CONNECT to the database ok.');
        }
        


        while ($step = array_shift($steps)) {
            switch ($step) {
                case 'drop':
                    $io->comment('Drop database');
                    $database->dropDatabase($dbConfig['dbname']);
                    break;

                case 'create':
                    $io->comment('Create database');
                    $database->createDatabase($dbConfig['dbname']);
                    break;

                case 'clear':
                    $io->comment('Clear database');
                    $database->emptyDatabase($dbConfig['dbname']);
                    break;

                case 'import':
                    $io->comment('Import database');
                    $database->importFile($dbConfig['dbname']$rootDir . '/_sql/install/latest.sql');

                    
$this->IOHelper,
            $connectionInfo
        );

        /** @var ConfigWriter $configWriter */
        $configWriter = $this->container->offsetGet('config.writer');
        $configWriter->writeConfig($connectionInfo);

        $conn = $this->initDatabaseConnection($connectionInfo$container);
        $databaseService = new DatabaseService($conn);

        $databaseService->createDatabase($connectionInfo->databaseName);
        $databaseService->selectDatabase($connectionInfo->databaseName);

        $skipImport = $databaseService->containsShopwareSchema()
            && $input->getOption('no-skip-import')
            && $this->shouldSkipImport();

        if (!$skipImport) {
            $this->importDatabase();
            $this->importSnippets();
        }

        
$output->writeln('Prepare installation');
        $output->writeln('');

        $dropDatabase = $input->getOption('drop-database');
        if ($dropDatabase) {
            $this->setupDatabaseAdapter->dropDatabase($connection$databaseConnectionInformation->getDatabaseName());
            $output->writeln('Drop database `' . $databaseConnectionInformation->getDatabaseName() . '`');
        }

        $createDatabase = $input->getOption('create-database') || $dropDatabase;
        if ($createDatabase) {
            $this->setupDatabaseAdapter->createDatabase($connection$databaseConnectionInformation->getDatabaseName());
            $output->writeln('Created database `' . $databaseConnectionInformation->getDatabaseName() . '`');
        }

        $importedBaseSchema = $this->setupDatabaseAdapter->initializeShopwareDb($connection$databaseConnectionInformation->getDatabaseName());

        if ($importedBaseSchema) {
            $output->writeln('Imported base schema.sql');
        }

        $output->writeln('');
    }
}
Home | Imprint | This part of the site doesn't use cookies.