Unverified Commit 4fff9761 authored by belgattitude's avatar belgattitude Committed by Luís Cobucci

Test JSON default values and refactoring

parent 1f9deec3
......@@ -23,9 +23,10 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
......@@ -126,7 +127,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/**
* {@inheritdoc}
*
* @return AbstractPlatform|MariaDb1027Platform|MySQL57Platform|MySqlPlatform
* @return AbstractPlatform|MariaDb102Platform|MySQL57Platform|MySqlPlatform
* @throws DBALException
*/
public function createDatabasePlatformForVersion($version): AbstractPlatform
......@@ -134,7 +135,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
if (false !== stripos($version, 'mariadb')) {
$versionNumber = $this->getMariaDbMysqlVersionNumber($version);
if (version_compare($versionNumber, '10.2.7', '>=')) {
return new MariaDb1027Platform();
return new MariaDb102Platform();
}
} else {
$versionNumber = $this->getOracleMysqlVersionNumber($version);
......@@ -196,7 +197,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/**
* {@inheritdoc}
*/
public function getDatabase(\Doctrine\DBAL\Connection $conn)
public function getDatabase(\Doctrine\DBAL\Connection $conn): ?string
{
$params = $conn->getParams();
......@@ -209,16 +210,18 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/**
* {@inheritdoc}
* @return MySqlPlatform
*/
public function getDatabasePlatform()
public function getDatabasePlatform(): AbstractPlatform
{
return new MySqlPlatform();
}
/**
* {@inheritdoc}
* @return MySqlSchemaManager
*/
public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
public function getSchemaManager(\Doctrine\DBAL\Connection $conn): AbstractSchemaManager
{
return new MySqlSchemaManager($conn);
}
......
......@@ -19,18 +19,17 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
/**
* Provides the behavior, features and SQL dialect of the MariaDB 10.2 (10.2.7 GA) database platform.
*
* Note: Should not be used with versions prior ro 10.2.7.
*
* @author Vanvelthem Sébastien
* @link www.doctrine-project.org
*/
final class MariaDb1027Platform extends MySqlPlatform
final class MariaDb102Platform extends MySqlPlatform
{
/**
* {@inheritdoc}
......
......@@ -19,7 +19,7 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;
......@@ -181,7 +181,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
$isNotNull = $tableColumn['null'] !== 'YES';
if ($this->_platform instanceof MariaDb1027Platform) {
if ($this->_platform instanceof MariaDb102Platform) {
$columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default'] ?? null);
} else {
$columnDefault = (isset($tableColumn['default'])) ? $tableColumn['default'] : null;
......@@ -217,24 +217,20 @@ class MySqlSchemaManager extends AbstractSchemaManager
/**
* Return column default value for MariaDB >= 10.2.7 servers that is
* compatible with existing doctrine mysql implementation (unquoted literals)
* Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers.
*
* Since 10.2.7:
*
* 1. Column defaults stored in information_schema are now quoted
* to distinguish them from expressions (see MDEV-10134 for a what is an expression).
* - Since MariaDb 10.2.7 column defaults stored in information_schema are now quoted
* to distinguish them from expressions (see MDEV-10134 for a what is an expression).
* - CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE are stored in information_schema
* as current_timestamp(), currdate(), currtime()
* - Literal escaping is normalized in information schema (store "''" instead of "\'")
*
* @link https://mariadb.com/kb/en/library/information-schema-columns-table/
* @link https://jira.mariadb.org/browse/MDEV-13132
*
* 2. Quoted string defaults use double single quotes as escaping character in information_schema.
*
* @links https://mariadb.com/kb/en/library/string-literals/
*
* @param null|string $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7
*/
private function getMariaDb1027ColumnDefault(MariaDb1027Platform $platform, ?string $columnDefault): ?string {
private function getMariaDb1027ColumnDefault(MariaDb102Platform $platform, ?string $columnDefault): ?string {
if ($columnDefault === 'NULL' || $columnDefault === null) {
$defaultValue = null;
} elseif (strpos($columnDefault, "'") === 0) {
......
......@@ -3,7 +3,7 @@
namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
......@@ -69,9 +69,9 @@ class AbstractMySQLDriverTest extends AbstractDriverTest
array('10.1.2a-MariaDB-a1~lenny-log', MySqlPlatform::class),
array('5.5.40-MariaDB-1~wheezy', MySqlPlatform::class),
array('5.5.40-MariaDB-1~wheezy', MySqlPlatform::class),
array('5.5.5-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class),
array('10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class),
array('10.2.8-MariaDB-1~lenny-log', MariaDb1027Platform::class)
array('5.5.5-MariaDB-10.2.8+maria~xenial-log', MariaDb102Platform::class),
array('10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb102Platform::class),
array('10.2.8-MariaDB-1~lenny-log', MariaDb102Platform::class)
);
}
......
......@@ -2,19 +2,19 @@
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
class MariaDb1027PlatformTest extends AbstractMySQLPlatformTestCase
class MariaDb102PlatformTest extends AbstractMySQLPlatformTestCase
{
/**
* {@inheritdoc}
*/
public function createPlatform()
{
return new MariaDb1027Platform();
return new MariaDb102Platform();
}
public function testHasNativeJsonType()
......@@ -34,29 +34,61 @@ class MariaDb1027PlatformTest extends AbstractMySQLPlatformTestCase
}
/**
* Overrides AbstractMySQLPlatformTestCase::testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
* Overrides and skips AbstractMySQLPlatformTestCase test regarding propagation
* of unsupported default values for Blob and Text columns.
*
* @see AbstractMySQLPlatformTestCase::testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
*/
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
{
$this->markTestSkipped('MariaDB102Platform support propagation of default values for BLOB and TEXT columns');
}
/**
* Since MariaDB 10.2, Text and Blob can have a default value.
*/
public function testPropagateDefaultValuesForTextAndBlobColumnTypes()
{
$table = new Table("text_blob_default_value");
$table->addColumn('def_text', 'text', array('default' => 'def'));
$table->addColumn('def_text', 'text', array('default' => "d''ef"));
$table->addColumn('def_blob', 'blob', array('default' => 'def'));
self::assertSame(
array('CREATE TABLE text_blob_default_value (def_text LONGTEXT DEFAULT \'def\' NOT NULL, def_blob LONGBLOB DEFAULT \'def\' NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'),
["CREATE TABLE text_blob_default_value (def_text LONGTEXT DEFAULT 'd''''ef' NOT NULL, def_blob LONGBLOB DEFAULT 'def' NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"],
$this->_platform->getCreateTableSQL($table)
);
$diffTable = clone $table;
$diffTable->changeColumn('def_text', array('default' => 'def'));
$diffTable->changeColumn('def_blob', array('default' => 'def'));
$diffTable->changeColumn('def_text', ['default' => "d''ef"]);
$diffTable->changeColumn('def_blob', ['default' => 'def']);
$comparator = new Comparator();
self::assertFalse($comparator->diffTable($table, $diffTable));
}
public function testPropagateDefaultValuesForJsonColumnType()
{
$table = new Table("text_json_default_value");
$json = json_encode(['prop1' => "O'Connor", 'prop2' => 10]);
$table->addColumn('def_json', 'text', ['default' => $json]);
self::assertSame(
["CREATE TABLE text_json_default_value (def_json LONGTEXT DEFAULT '{\"prop1\":\"O''Connor\",\"prop2\":10}' NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"],
$this->_platform->getCreateTableSQL($table)
);
$diffTable = clone $table;
$diffTable->changeColumn('def_json', ['default' => $json]);
$comparator = new Comparator();
self::assertFalse($comparator->diffTable($table, $diffTable));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment