Skip to content

Commit

Permalink
Merge pull request #420 from DHager/default_table_options
Browse files Browse the repository at this point in the history
Allow defaultTableOptions to be configured
  • Loading branch information
guilhermeblanco committed Nov 4, 2015
2 parents ba11cc4 + 0918e9e commit f3782a6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 2 deletions.
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function getDbalConnectionsNode()
->fixXmlConfig('mapping_type')
->fixXmlConfig('slave')
->fixXmlConfig('shard')
->fixXmlConfig('default_table_option')
->children()
->scalarNode('driver')->defaultValue('pdo_mysql')->end()
->scalarNode('platform_service')->end()
Expand All @@ -153,6 +154,11 @@ private function getDbalConnectionsNode()
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('default_table_options')
->info("This option is used by the schema-tool and affects generated SQL. Possible keys include 'charset','collate', and 'engine'.")
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ protected function getConnectionOptions($connection)
'keep_slave' => 'keepSlave',
'shard_choser' => 'shardChoser',
'server_version' => 'serverVersion',
'default_table_options' => 'defaultTableOptions',
) as $old => $new) {
if (isset($options[$old])) {
$options[$new] = $options[$old];
Expand Down
1 change: 1 addition & 0 deletions Resources/config/schema/doctrine-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<xsd:element name="mapping-type" type="named_scalar" />
<xsd:element name="slave" type="slave" />
<xsd:element name="shard" type="shard" />
<xsd:element name="default-table-option" type="named_scalar" />
</xsd:choice>
</xsd:group>

Expand Down
21 changes: 21 additions & 0 deletions Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Configuration Reference
# example
# enum: string
default_table_options:
# Affects schema-tool. If absent, DBAL chooses defaults
# based on the platform. Examples here are for MySQL.
# charset: utf8
# collate: utf8_unicode_ci
# engine: InnoDB
slaves:
# A collection of named slave connections (e.g. slave1, slave2)
slave1:
Expand Down Expand Up @@ -420,6 +427,11 @@ Configuration Reference
<!-- example -->
<doctrine:mapping-type name="enum">string</doctrine:mapping-type>
<!-- example -->
<doctrine:default-table-option name="charset">utf8</doctrine:default-table-option>
<doctrine:default-table-option name="collate">utf8_unicode_ci</doctrine:default-table-option>
<doctrine:default-table-option name="engine">InnoDB</doctrine:default-table-option>
<!-- example -->
<!-- unix-socket: The unix socket to use for MySQL -->
<!-- persistent: True to use as persistent connection for the ibm_db2 driver -->
Expand Down Expand Up @@ -846,6 +858,12 @@ can configure. The following block shows all possible configuration keys:
enum: string
types:
custom: Acme\HelloBundle\MyCustomType
default_table_options:
# Affects schema-tool. If absent, DBAL chooses defaults
# based on the platform.
charset: utf8
collate: utf8_unicode_ci
engine: InnoDB
.. code-block:: xml
Expand Down Expand Up @@ -884,6 +902,9 @@ can configure. The following block shows all possible configuration keys:
>
<doctrine:option key="foo">bar</doctrine:option>
<doctrine:mapping-type name="enum">string</doctrine:mapping-type>
<doctrine:default-table-option name="charset">utf8</doctrine:default-table-option>
<doctrine:default-table-option name="collate">utf8_unicode_ci</doctrine:default-table-option>
<doctrine:default-table-option name="engine">InnoDB</doctrine:default-table-option>
<doctrine:type name="custom">Acme\HelloBundle\MyCustomType</doctrine:type>
</doctrine:dbal>
</doctrine:config>
Expand Down
35 changes: 33 additions & 2 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public function testDbalLoadSingleMasterSlaveConnection()
$this->assertEquals('Doctrine\\DBAL\\Connections\\MasterSlaveConnection', $param['wrapperClass']);
$this->assertTrue($param['keepSlave']);
$this->assertEquals(
array('user' => 'mysql_user', 'password' => 'mysql_s3cr3t', 'port' => null, 'dbname' => 'mysql_db', 'host' => 'localhost', 'unix_socket' => '/path/to/mysqld.sock'),
array('user' => 'mysql_user', 'password' => 'mysql_s3cr3t',
'port' => null, 'dbname' => 'mysql_db', 'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'defaultTableOptions' => array(),
),
$param['master']
);
$this->assertEquals(
Expand All @@ -136,7 +140,11 @@ public function testDbalLoadPoolShardingConnection()
$this->assertEquals('Doctrine\\DBAL\\Sharding\\PoolingShardConnection', $param['wrapperClass']);
$this->assertEquals(new Reference('foo.shard_choser'), $param['shardChoser']);
$this->assertEquals(
array('user' => 'mysql_user', 'password' => 'mysql_s3cr3t', 'port' => null, 'dbname' => 'mysql_db', 'host' => 'localhost', 'unix_socket' => '/path/to/mysqld.sock'),
array('user' => 'mysql_user', 'password' => 'mysql_s3cr3t',
'port' => null, 'dbname' => 'mysql_db', 'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'defaultTableOptions' => array(),
),
$param['global']
);
$this->assertEquals(
Expand Down Expand Up @@ -179,6 +187,7 @@ public function testLoadSimpleSingleConnection()
'password' => null,
'driver' => 'pdo_mysql',
'driverOptions' => array(),
'defaultTableOptions' => array(),
),
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -218,6 +227,7 @@ public function testLoadSimpleSingleConnectionWithoutDbName()
'password' => null,
'driver' => 'pdo_mysql',
'driverOptions' => array(),
'defaultTableOptions' => array(),
),
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -257,6 +267,7 @@ public function testLoadSingleConnection()
'password' => 'sqlite_s3cr3t',
'dbname' => 'sqlite_db',
'memory' => true,
'defaultTableOptions' => array(),
),
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -477,6 +488,26 @@ public function testMultipleEntityManagersMappingBundleDefinitions()
));
}

public function testSingleEntityManagerDefaultTableOptions()
{
$container = $this->loadContainer('orm_single_em_default_table_options', array('YamlBundle', 'AnnotationsBundle', 'XmlBundle'));

$param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);

$this->assertArrayHasKey('defaultTableOptions',$param);

$defaults = $param['defaultTableOptions'];

$this->assertArrayHasKey('charset', $defaults);
$this->assertArrayHasKey('collate', $defaults);
$this->assertArrayHasKey('engine', $defaults);

$this->assertEquals('utf8mb4',$defaults['charset']);
$this->assertEquals('utf8mb4_unicode_ci',$defaults['collate']);
$this->assertEquals('InnoDB',$defaults['engine']);

}

public function testSetTypes()
{
$container = $this->loadContainer('dbal_types');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" ?>

<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">

<config>
<dbal default-connection="default">
<connection name="default" dbname="db">
<default-table-option name="charset">utf8mb4</default-table-option>
<default-table-option name="collate">utf8mb4_unicode_ci</default-table-option>
<default-table-option name="engine">InnoDB</default-table-option>
</connection>
</dbal>

<orm>
<mapping name="AnnotationsBundle" />
<mapping name="YamlBundle" dir="Resources/config/doctrine" alias="yml" />
<mapping name="manual" type="xml" prefix="Fixtures\Bundles\XmlBundle"
dir="%kernel.root_dir%/Tests/DependencyInjection/Fixtures/Bundles/XmlBundle/Resources/config/doctrine"
alias="TestAlias"
/>
</orm>
</config>
</srv:container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: db
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
engine: InnoDB

orm:
mappings:
AnnotationsBundle: ~
YamlBundle:
dir: Resources/config/doctrine
alias: yml
manual:
type: xml
prefix: Fixtures\Bundles\XmlBundle
dir: %kernel.root_dir%/Tests/DependencyInjection/Fixtures/Bundles/XmlBundle/Resources/config/doctrine
alias: TestAlias

0 comments on commit f3782a6

Please sign in to comment.