Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dbal related code from DoctrineBundle #1

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
99e4572
add dbal config & extension
dmaicher Nov 23, 2019
3a589e9
cleanup config: remove orm stuff
dmaicher Nov 23, 2019
dfca364
require doctrine/dbal
dmaicher Dec 10, 2019
193d4ec
move more dbal related things
dmaicher Dec 10, 2019
afcbb2c
add data collector + some fixes
dmaicher Dec 10, 2019
300d236
fix data collector
dmaicher Dec 10, 2019
5f7fec1
add ProfilerController
dmaicher Dec 10, 2019
6d75488
add ConnectionRegistry
dmaicher Dec 10, 2019
d7d6ce7
use registry
dmaicher Feb 29, 2020
20b3cb2
start adding tests
dmaicher Feb 29, 2020
ebe8d49
add some more tests
dmaicher Feb 29, 2020
7356d86
add some more tests
dmaicher Feb 29, 2020
c944ac6
adjust composer requirements
dmaicher Mar 7, 2020
af74eec
adjust composer requirements
dmaicher Mar 7, 2020
7630931
CS fixes
dmaicher Mar 7, 2020
4f5ec77
add more tests
dmaicher Mar 7, 2020
2c6f5bd
add schema + extension tests
dmaicher Mar 8, 2020
98fb67e
cleanup
dmaicher Mar 8, 2020
4f78716
more cleanup
dmaicher Mar 8, 2020
25dc5ad
address first review comments
dmaicher Mar 8, 2020
ded7eb8
add RunSqlDoctrineCommand proxy
dmaicher Mar 8, 2020
bd278bf
cs fix
dmaicher Mar 8, 2020
cdc3fd4
add 2 more commands + tests
dmaicher May 11, 2020
ea8b8be
define command services + cleanup
dmaicher May 11, 2020
ed66ca2
cleanup
dmaicher May 11, 2020
1a3d3e6
use correct version of factory from 2.0.x
dmaicher May 11, 2020
d05284d
fix CS
dmaicher May 11, 2020
479e43f
remove unused class
dmaicher May 11, 2020
fe82cd8
fix registry + add test
dmaicher May 11, 2020
4ccb226
drop support for phpunit 7
dmaicher May 11, 2020
1c46aa5
fix phpunit 7 compat
dmaicher May 11, 2020
4d595a3
fix phpunit warnings
dmaicher May 11, 2020
5436c29
fix CS
dmaicher May 11, 2020
4df8eea
fix deprecations for doctrine/dbal >= 2.11
dmaicher May 13, 2020
8d51194
fix CS
dmaicher May 13, 2020
a7361d8
fix comments
dmaicher May 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- 7.4

env:
global:
Expand All @@ -21,7 +21,7 @@ before_install:
- composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-master

install:
- travis_retry composer update -n --prefer-dist --prefer-stable
- travis_retry composer update -n --prefer-stable

script:
- ./vendor/bin/phpunit -v
Expand All @@ -32,20 +32,19 @@ jobs:
- php: 7.1
env: LOWEST SYMFONY_DEPRECATIONS_HELPER=weak
install:
- travis_retry composer update -n --prefer-lowest --prefer-stable --prefer-dist
- travis_retry composer update -n --prefer-lowest --prefer-stable

# Test against latest Symfony 4.4 dev
# Symfony 4.4
- php: 7.3
env: SYMFONY_REQUIRE="4.4.*"
install:
- travis_retry composer update -n --prefer-dist
- travis_retry composer update -n

# Test dev versions
- php: 7.3
if: type = cron
env: DEV
# Symfony 5.0
- php: 7.4
env: SYMFONY_REQUIRE="5.0.*"
install:
- travis_retry composer update -n --prefer-dist
- travis_retry composer update -n

- stage: Code Quality
env: CODING_STANDARDS
Expand All @@ -56,7 +55,7 @@ jobs:
- stage: Coverage
php: 7.3
install:
- travis_retry composer update -n --prefer-dist
- travis_retry composer update -n
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
Expand Down
25 changes: 20 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["DBAL", "Database"],
"homepage": "http://www.doctrine-project.org",
"license": "MIT",
"minimum-stability": "dev",
"minimum-stability": "stable",
"authors": [
{
"name": "Doctrine Project",
Expand All @@ -14,23 +14,38 @@
],
"require": {
"php": "^7.1",
"doctrine/dbal": "^2.9",
"jdorn/sql-formatter": "^1.2.16",
"psr/container": "^1.0",
"symfony/config": "^4.3.3|^5.0",
"symfony/console": "^3.4.30|^4.3.3|^5.0",
"symfony/dependency-injection": "^4.3.3|^5.0",
"symfony/doctrine-bridge": "^4.3.7|^5.0",
"symfony/framework-bundle": "^3.4.30|^4.3.3|^5.0",
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
"symfony/http-kernel": "^3.4.30|^4.3.3|^5.0"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"phpunit/phpunit": "^7.5 || ^8.4",
"symfony/phpunit-bridge": "^5.0"
"phpunit/phpunit": "^7.5 || ^8.5",
"symfony/phpunit-bridge": "^5.0",
"symfony/twig-bridge": "^3.4.30|^4.3.3|^5.0",
"symfony/web-profiler-bundle": "^3.4.30|^4.3.3|^5.0",
"symfony/yaml": "^3.4.30|^4.3.3|^5.0",
"twig/twig": "^1.34|^2.12"
},
"config": {
"sort-packages": true
"sort-packages": true,
"preferred-install": "dist"
},
"conflict": {
"doctrine/orm": "<2.6",
"twig/twig": "<1.34|>=2.0,<2.4"
},
"autoload": {
"psr-4": { "Doctrine\\Bundle\\DBALBundle\\": "src" }
},
"autoload-dev": {
"psr-4": { "Doctrine\\Bundle\\DBALBundle\\Tests\\": "tests" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
Expand Down
53 changes: 53 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value=""/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<file>.</file>
<exclude-pattern>vendor/*</exclude-pattern>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName"/>
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes"/>
<exclude name="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty">
<exclude-pattern>tests/Fixtures/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
>
<testsuites>
<testsuite name="Doctrine DBAL Bundle Test Suite">
<directory>tests/</directory>
<exclude>tests/Fixtures</exclude>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
120 changes: 120 additions & 0 deletions src/Command/CreateDatabaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Doctrine\Bundle\DBALBundle\Command;

use Doctrine\Bundle\DBALBundle\ConnectionRegistry;
use Doctrine\DBAL\DriverManager;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class CreateDatabaseCommand extends Command
{
/** @var ConnectionRegistry */
private $registry;

public function __construct(ConnectionRegistry $registry)
{
parent::__construct();
$this->registry = $registry;
}

/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:database:create')
->setDescription('Creates the configured database')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->addOption('if-not-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database already exists')
->setHelp(<<<EOT
The <info>%command.name%</info> command creates the default connections database:

<info>php %command.full_name%</info>

You can also optionally specify the name of a connection to create the database for:

<info>php %command.full_name% --connection=default</info>
EOT
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$connectionName = $input->getOption('connection');
if (empty($connectionName)) {
$connectionName = $this->registry->getDefaultConnectionName();
}
$connection = $this->registry->getConnection($connectionName);

$ifNotExists = $input->getOption('if-not-exists');

$params = $connection->getParams();
if (isset($params['master'])) {
$params = $params['master'];
}

// Cannot inject `shard` option in parent::getDoctrineConnection
// cause it will try to connect to a non-existing database
if (isset($params['shards'])) {
$shards = $params['shards'];
// Default select global
$params = array_merge($params, $params['global']);
unset($params['global']['dbname'], $params['global']['path'], $params['global']['url']);
if ($input->getOption('shard')) {
foreach ($shards as $i => $shard) {
if ($shard['id'] === (int) $input->getOption('shard')) {
// Select sharded database
$params = array_merge($params, $shard);
unset($params['shards'][$i]['dbname'], $params['shards'][$i]['path'], $params['shards'][$i]['url'], $params['id']);
break;
}
}
}
}

$hasPath = isset($params['path']);
$name = $hasPath ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
if (! $name) {
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be created.");
}
// Need to get rid of _every_ occurrence of dbname from connection configuration and we have already extracted all relevant info from url
unset($params['dbname'], $params['path'], $params['url']);

$tmpConnection = DriverManager::getConnection($params);
$tmpConnection->connect($input->getOption('shard'));
$shouldNotCreateDatabase = $ifNotExists && in_array($name, $tmpConnection->getSchemaManager()->listDatabases());

// Only quote if we don't have a path
if (! $hasPath) {
$name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
}

$error = false;
try {
if ($shouldNotCreateDatabase) {
$output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> already exists. Skipped.</info>', $name, $connectionName));
} else {
$tmpConnection->getSchemaManager()->createDatabase($name);
$output->writeln(sprintf('<info>Created database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
}
} catch (Exception $e) {
$output->writeln(sprintf('<error>Could not create database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$error = true;
}

$tmpConnection->close();

return $error ? 1 : 0;
}
}
Loading