Issues (71)

app/bin/db-create.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
require __DIR__.'./../vendor/autoload.php';
6
7
use App\Kernel;
8
use Doctrine\DBAL\DriverManager;
0 ignored issues
show
The type Doctrine\DBAL\DriverManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
$kernel = new Kernel('test', true);
11
$kernel->boot();
12
$container = $kernel->getContainer();
13
$connectionName = $container->get('doctrine')->getDefaultConnectionName();
14
/** @var \Doctrine\DBAL\Connection $connection */
15
$connection = $container->get('doctrine')->getConnection($connectionName);
16
$params = $connection->getParams();
17
// Cannot inject `shard` option in parent::getDoctrineConnection
18
// cause it will try to connect to a non-existing database
19
if (isset($params['shards'])) {
20
    $shards = $params['shards'];
21
    // Default select global
22
    $params = array_merge($params, $params['global']);
23
    unset($params['global']['dbname']);
24
    if ($input->getOption('shard')) {
25
        foreach ($shards as $i => $shard) {
26
            if ($shard['id'] === (int) $input->getOption('shard')) {
27
                // Select sharded database
28
                $params = array_merge($params, $shard);
29
                unset($params['shards'][$i]['dbname'], $params['id']);
30
                break;
31
            }
32
        }
33
    }
34
}
35
36
$hasPath = isset($params['path']);
37
$name = $hasPath ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
38
if (!$name) {
39
    throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
40
}
41
// Need to get rid of _every_ occurrence of dbname from connection configuration and we have already extracted all relevant info from url
42
unset($params['dbname'], $params['path'], $params['url']);
43
44
$tmpConnection = DriverManager::getConnection($params);
45
$tmpConnection->connect($params['shards']);
46
$shouldNotCreateDatabase = $ifNotExists && in_array($name, $tmpConnection->getSchemaManager()->listDatabases(), true);
47
48
// Only quote if we don't have a path
49
if (!$hasPath) {
50
    $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
51
}
52
53
$error = false;
54
try {
55
    if ($shouldNotCreateDatabase) {
56
        echo 'error';
57
//        $output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> already exists. Skipped.</info>', $name, $connectionName));
58
    } else {
59
        $tmpConnection->getSchemaManager()->createDatabase($name);
60
        echo "Database $name created \n";
61
//        $output->writeln(sprintf('<info>Created database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
62
    }
63
} catch (\Exception $e) {
64
//    $output->writeln(sprintf('<error>Could not create database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
65
//    $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
66
    echo $e->getMessage();
67
    $error = true;
68
}
69
70
$tmpConnection->close();
71