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; ...@@ -23,9 +23,10 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager; use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
...@@ -126,7 +127,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -126,7 +127,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @return AbstractPlatform|MariaDb1027Platform|MySQL57Platform|MySqlPlatform * @return AbstractPlatform|MariaDb102Platform|MySQL57Platform|MySqlPlatform
* @throws DBALException * @throws DBALException
*/ */
public function createDatabasePlatformForVersion($version): AbstractPlatform public function createDatabasePlatformForVersion($version): AbstractPlatform
...@@ -134,7 +135,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -134,7 +135,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
if (false !== stripos($version, 'mariadb')) { if (false !== stripos($version, 'mariadb')) {
$versionNumber = $this->getMariaDbMysqlVersionNumber($version); $versionNumber = $this->getMariaDbMysqlVersionNumber($version);
if (version_compare($versionNumber, '10.2.7', '>=')) { if (version_compare($versionNumber, '10.2.7', '>=')) {
return new MariaDb1027Platform(); return new MariaDb102Platform();
} }
} else { } else {
$versionNumber = $this->getOracleMysqlVersionNumber($version); $versionNumber = $this->getOracleMysqlVersionNumber($version);
...@@ -196,7 +197,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -196,7 +197,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(\Doctrine\DBAL\Connection $conn) public function getDatabase(\Doctrine\DBAL\Connection $conn): ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -209,16 +210,18 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -209,16 +210,18 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/** /**
* {@inheritdoc} * {@inheritdoc}
* @return MySqlPlatform
*/ */
public function getDatabasePlatform() public function getDatabasePlatform(): AbstractPlatform
{ {
return new MySqlPlatform(); return new MySqlPlatform();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* @return MySqlSchemaManager
*/ */
public function getSchemaManager(\Doctrine\DBAL\Connection $conn) public function getSchemaManager(\Doctrine\DBAL\Connection $conn): AbstractSchemaManager
{ {
return new MySqlSchemaManager($conn); return new MySqlSchemaManager($conn);
} }
......
...@@ -19,18 +19,17 @@ ...@@ -19,18 +19,17 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
/** /**
* Provides the behavior, features and SQL dialect of the MariaDB 10.2 (10.2.7 GA) database platform. * 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 * @author Vanvelthem Sébastien
* @link www.doctrine-project.org * @link www.doctrine-project.org
*/ */
final class MariaDb1027Platform extends MySqlPlatform final class MariaDb102Platform extends MySqlPlatform
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
...@@ -181,7 +181,7 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -181,7 +181,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
$isNotNull = $tableColumn['null'] !== 'YES'; $isNotNull = $tableColumn['null'] !== 'YES';
if ($this->_platform instanceof MariaDb1027Platform) { if ($this->_platform instanceof MariaDb102Platform) {
$columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default'] ?? null); $columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default'] ?? null);
} else { } else {
$columnDefault = (isset($tableColumn['default'])) ? $tableColumn['default'] : null; $columnDefault = (isset($tableColumn['default'])) ? $tableColumn['default'] : null;
...@@ -217,24 +217,20 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -217,24 +217,20 @@ class MySqlSchemaManager extends AbstractSchemaManager
/** /**
* Return column default value for MariaDB >= 10.2.7 servers that is * Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers.
* compatible with existing doctrine mysql implementation (unquoted literals)
* *
* Since 10.2.7: * - 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).
* 1. Column defaults stored in information_schema are now quoted * - CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE are stored in information_schema
* to distinguish them from expressions (see MDEV-10134 for a what is an expression). * 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://mariadb.com/kb/en/library/information-schema-columns-table/
* @link https://jira.mariadb.org/browse/MDEV-13132 * @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 * @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) { if ($columnDefault === 'NULL' || $columnDefault === null) {
$defaultValue = null; $defaultValue = null;
} elseif (strpos($columnDefault, "'") === 0) { } elseif (strpos($columnDefault, "'") === 0) {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace Doctrine\Tests\DBAL\Driver; namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager; use Doctrine\DBAL\Schema\MySqlSchemaManager;
...@@ -69,9 +69,9 @@ class AbstractMySQLDriverTest extends AbstractDriverTest ...@@ -69,9 +69,9 @@ class AbstractMySQLDriverTest extends AbstractDriverTest
array('10.1.2a-MariaDB-a1~lenny-log', MySqlPlatform::class), 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.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('5.5.5-MariaDB-10.2.8+maria~xenial-log', MariaDb102Platform::class),
array('10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb1027Platform::class), array('10.2.8-MariaDB-10.2.8+maria~xenial-log', MariaDb102Platform::class),
array('10.2.8-MariaDB-1~lenny-log', MariaDb1027Platform::class) array('10.2.8-MariaDB-1~lenny-log', MariaDb102Platform::class)
); );
} }
......
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\Types\MySqlPointType;
class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
...@@ -17,7 +18,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -17,7 +18,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
parent::setUp(); parent::setUp();
if (!Type::hasType('point')) { if (!Type::hasType('point')) {
Type::addType('point', 'Doctrine\Tests\Types\MySqlPointType'); Type::addType('point', MySqlPointType::class);
} }
} }
...@@ -157,15 +158,15 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -157,15 +158,15 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
*/ */
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes() public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
{ {
if ($this->_sm->getDatabasePlatform() instanceof MariaDb1027Platform) { if ($this->_sm->getDatabasePlatform() instanceof MariaDb102Platform) {
$this->markTestSkipped('MariaDb1027Platform supports default values for BLOB and TEXT columns and will propagate values'); $this->markTestSkipped('MariaDb102Platform supports default values for BLOB and TEXT columns and will propagate values');
} }
$table = new Table("text_blob_default_value"); $table = new Table("text_blob_default_value");
$table->addColumn('def_text', 'text', array('default' => 'def')); $table->addColumn('def_text', 'text', ['default' => 'def']);
$table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def')); $table->addColumn('def_text_null', 'text', ['notnull' => false, 'default' => 'def']);
$table->addColumn('def_blob', 'blob', array('default' => 'def')); $table->addColumn('def_blob', 'blob', ['default' => 'def']);
$table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def')); $table->addColumn('def_blob_null', 'blob', ['notnull' => false, 'default' => 'def']);
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
...@@ -192,37 +193,6 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -192,37 +193,6 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull()); self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
} }
/**
* Since MariaDB 10.2.1, Blob and text columns can have a default value
*
* @link https://mariadb.com/kb/en/library/blob-and-text-data-types
*/
public function testDefaultValueSupportForBlobAndText()
{
if (!$this->_sm->getDatabasePlatform() instanceof MariaDb1027Platform) {
$this->markTestSkipped('Only MariaDb1027Platform supports default values for BLOB and TEXT columns');
}
$table = new Table("text_blob_default_value");
$table->addColumn('def_text', 'text', array('default' => 'def'));
$table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
$table->addColumn('def_blob', 'blob', array('default' => 'def'));
$table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
$this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
self::assertSame('def', $onlineTable->getColumn('def_text')->getDefault());
self::assertSame('def', $onlineTable->getColumn('def_text_null')->getDefault());
self::assertSame('def', $onlineTable->getColumn('def_blob')->getDefault());
self::assertSame('def', $onlineTable->getColumn('def_blob_null')->getDefault());
$comparator = new Comparator();
self::assertFalse($comparator->diffTable($table, $onlineTable));
}
public function testColumnCollation() public function testColumnCollation()
{ {
$table = new Table('test_collation'); $table = new Table('test_collation');
...@@ -374,13 +344,12 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -374,13 +344,12 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
*/ */
public function testColumnDefaultValuesDoubleQuoted(): void public function testColumnDefaultValuesDoubleQuoted(): void
{ {
$table = new Table("test_column_default_values_double_quoted"); $table = new Table("test_column_default_values_double_quoted");
$table->addColumn('string_nullable_quoted', 'string', ['notnull' => false, 'default' => 'NULL']); $table->addColumn('string_nullable_quoted', 'string', ['notnull' => false, 'default' => 'NULL']);
$table->addColumn('string_nullable_double_quoted', 'string', ['notnull' => false, 'default' => "'NULL'"]); $table->addColumn('string_nullable_double_quoted', 'string', ['notnull' => false, 'default' => "'NULL'"]);
$table->addColumn('string_notnull_quoted', 'string', ['notnull' => true, 'default' => 'NULL']); $table->addColumn('string_notnull_quoted', 'string', ['notnull' => true, 'default' => 'NULL']);
//$table->addColumn('string_notnull_double_quoted', 'string', ['notnull' => true, 'default' => "\\'NULL\\'"]); $table->addColumn('string_notnull_double_quoted', 'string', ['notnull' => true, 'default' => "\\'NULL\\'"]);
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
...@@ -389,16 +358,14 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -389,16 +358,14 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertSame("'NULL'", $onlineTable->getColumn('string_nullable_double_quoted')->getDefault()); self::assertSame("'NULL'", $onlineTable->getColumn('string_nullable_double_quoted')->getDefault());
self::assertSame("NULL", $onlineTable->getColumn('string_notnull_quoted')->getDefault()); self::assertSame("NULL", $onlineTable->getColumn('string_notnull_quoted')->getDefault());
//self::assertSame("'NULL'", $onlineTable->getColumn('string_notnull_double_quoted')->getDefault()); self::assertSame("\\'NULL\\'", $onlineTable->getColumn('string_notnull_double_quoted')->getDefault());
$comparator = new Comparator(); $comparator = new Comparator();
$diff = $comparator->diffTable($table, $onlineTable); $diff = $comparator->diffTable($table, $onlineTable);
self::assertFalse($diff, "Tables should be identical with double quoted literals."); self::assertFalse($diff, "Tables should be identical with double quoted literals.");
} }
public function testColumnDefaultCurrentTimestamp(): void public function testColumnDefaultCurrentTimestamp(): void
{ {
$platform = $this->_sm->getDatabasePlatform(); $platform = $this->_sm->getDatabasePlatform();
...@@ -423,43 +390,6 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -423,43 +390,6 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
} }
/** /**
* Test default CURRENT_TIME and CURRENT_DATE as default values
*
* Note: MySQL (as of 5.7.19) does not support default value
* for DATE and TIME fields while MariaDB 10.2+ does
*/
public function testColumnDefaultCurrentTimeAndDate()
{
$platform = $this->_sm->getDatabasePlatform();
if (!$platform instanceof MariaDb1027Platform) {
$this->markTestSkipped('Currently only MariaDb1027Platform supports setting CURRENT_TIME and CURRENT_DATE default values.');
}
$table = new Table("test_column_defaults_current_time_and_date");
$currentTimeSql = $platform->getCurrentTimeSQL();
$currentDateSql = $platform->getCurrentDateSQL();
$table->addColumn('col_date', 'date', ['notnull' => true, 'default' => $currentDateSql]);
$table->addColumn('col_time', 'time', ['notnull' => true, 'default' => $currentTimeSql]);
$this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("test_column_defaults_current_time_and_date");
self::assertSame($currentDateSql, $onlineTable->getColumn('col_date')->getDefault());
self::assertSame($currentTimeSql, $onlineTable->getColumn('col_time')->getDefault());
$comparator = new Comparator();
$diff = $comparator->diffTable($table, $onlineTable);
self::assertFalse($diff, "Tables should be identical with column defaults.");
}
/**
*
* @link https://mariadb.com/kb/en/library/string-literals * @link https://mariadb.com/kb/en/library/string-literals
*/ */
public function testColumnDefaultValuesEscaping(): void public function testColumnDefaultValuesEscaping(): void
...@@ -483,9 +413,26 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -483,9 +413,26 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertFalse($diff, "Tables should be identical with values escape sequences."); self::assertFalse($diff, "Tables should be identical with values escape sequences.");
} }
public function testColumnDefaultsUsingDoctrineTable(): void public function testJsonColumnType(): void
{ {
$platform = $this->_sm->getDatabasePlatform();
if (!$platform->hasNativeJsonType()) {
$this->markTestSkipped("Requires native JSON type");
}
$table = new Table('test_mysql_json');
$table->addColumn('col_json', 'json');
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns('test_mysql_json');
self::assertSame(TYPE::JSON, $columns['col_json']->getType()->getName());
}
/**
* @todo split into multiple tests (most of them already made) and remove
*/
public function testColumnDefaultsUsingDoctrineTable(): void
{
$table = new Table("test_column_defaults_with_table"); $table = new Table("test_column_defaults_with_table");
$table->addColumn('col0', 'integer', ['notnull' => false]); $table->addColumn('col0', 'integer', ['notnull' => false]);
$table->addColumn('col1', 'integer', ['notnull' => false, 'default' => null]); $table->addColumn('col1', 'integer', ['notnull' => false, 'default' => null]);
...@@ -534,20 +481,22 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -534,20 +481,22 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
} }
/** /**
* Ensure that an existing table with quoted (literals) default values * Ensure that a table created outside doctrine and containing
* does not trigger a table change. * quoted default values does not trigger a table diff change. I
* Note: MariaDb 10.2 silently change "\'" into "''" when storing in
* information schema, MariaDb102Platform should normalize the table details.
*/ */
public function testColumnDefaultsDoesNotTriggerADiff(): void public function testExistingTableWithQuotedDefaultsDoesNotTriggerChange(): void
{ {
$this->_conn->query('DROP TABLE IF EXISTS test_column_defaults_no_diff'); $this->_conn->query('DROP TABLE IF EXISTS test_column_defaults_with_create');
$sql = " $sql = "
CREATE TABLE test_column_defaults_with_create ( CREATE TABLE test_column_defaults_with_create (
col1 VARCHAR(255) NULL DEFAULT 'O''Connor\'\"', col1 VARCHAR(255) NULL DEFAULT 'O''Connor\'\"',
col2 VARCHAR(255) NULL DEFAULT '''A''' col2 VARCHAR(255) NULL DEFAULT '''A'''
); );
"; ";
$this->_conn->query($sql); $this->_conn->query($sql);
$onlineTable = $this->_sm->listTableDetails("test_column_defaults_with_create"); $onlineTable = $this->_sm->listTableDetails("test_column_defaults_with_create");
self::assertSame("O'Connor'\"", $onlineTable->getColumn('col1')->getDefault()); self::assertSame("O'Connor'\"", $onlineTable->getColumn('col1')->getDefault());
self::assertSame("'A'", $onlineTable->getColumn('col2')->getDefault()); self::assertSame("'A'", $onlineTable->getColumn('col2')->getDefault());
...@@ -561,31 +510,101 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -561,31 +510,101 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertFalse($diff); self::assertFalse($diff);
} }
/**
* Since MariaDB 10.2.1, Blob and text columns can have a default value
*
* @link https://mariadb.com/kb/en/library/blob-and-text-data-types
*/
public function testDoesPropagateDefaultValuesForBlobTextAndJson(): void
{
if (!$this->_sm->getDatabasePlatform() instanceof MariaDb102Platform) {
$this->markTestSkipped('Only relevant for MariaDb102Platform.');
}
$table = new Table("text_blob_default_value");
$json = json_encode(['prop1' => "O'Connor", 'prop2' => 10]);
$table->addColumn('def_text', 'text', ['default' => "O'Connor"]);
$table->addColumn('def_text_null', 'text', ['notnull' => false, 'default' => 'def']);
$table->addColumn('def_blob', 'blob', ['default' => 'def']);
$table->addColumn('def_json', 'json', ['default' => $json]);
$this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
self::assertSame("O'Connor", $onlineTable->getColumn('def_text')->getDefault());
self::assertSame('def', $onlineTable->getColumn('def_text_null')->getDefault());
self::assertSame('def', $onlineTable->getColumn('def_blob')->getDefault());
self::assertSame($json, $onlineTable->getColumn('def_json')->getDefault());
$comparator = new Comparator();
self::assertFalse($comparator->diffTable($table, $onlineTable));
}
/**
* Note: MySQL (as of 5.7.19) does not support default value
* for DATE and TIME fields while MariaDB 10.2+ does
*/
public function testColumnDefaultValuesCurrentTimeAndDate(): void
{
if (!$this->_sm->getDatabasePlatform() instanceof MariaDb102Platform) {
$this->markTestSkipped('Only relevant for MariaDb102Platform.');
}
$platform = $this->_sm->getDatabasePlatform();
$table = new Table("test_column_defaults_current_time_and_date");
$currentTimeSql = $platform->getCurrentTimeSQL();
$currentDateSql = $platform->getCurrentDateSQL();
$table->addColumn('col_date', 'date', ['notnull' => true, 'default' => $currentDateSql]);
$table->addColumn('col_time', 'time', ['notnull' => true, 'default' => $currentTimeSql]);
$this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("test_column_defaults_current_time_and_date");
self::assertSame($currentDateSql, $onlineTable->getColumn('col_date')->getDefault());
self::assertSame($currentTimeSql, $onlineTable->getColumn('col_time')->getDefault());
$comparator = new Comparator();
$diff = $comparator->diffTable($table, $onlineTable);
self::assertFalse($diff, "Tables should be identical with column defaults.");
}
/** /**
* MariaDB supports expressions as default values * MariaDB supports expressions as default values
* *
* @todo remove or implement !!!
*
* @link https://mariadb.com/kb/en/library/information-schema-columns-table/ * @link https://mariadb.com/kb/en/library/information-schema-columns-table/
*/ */
public function testColumnDefaultExpressions(): void public function testColumnDefaultExpressions(): void
{ {
$this->markTestSkipped('Setting an expression as a default value is not yet supported (WIP)'); $this->markTestSkipped('Setting an expression as a default value is not yet supported (WIP)');
if ($this->_sm->getDatabasePlatform() instanceof MariaDb1027Platform) { if (!$this->_sm->getDatabasePlatform() instanceof MariaDb102Platform) {
$this->markTestSkipped('Only relevant for MariaDb102Platform.');
}
$table = new Table("test_column_default_expressions"); $table = new Table("test_column_default_expressions");
$table->addColumn('expression', 'string', ['notnull' => false, 'default' => "concat('A','B')"]); $table->addColumn('expression', 'string', ['notnull' => false, 'default' => "concat('A','B')"]);
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("test_column_default_expressions"); $onlineTable = $this->_sm->listTableDetails("test_column_default_expressions");
self::assertSame("concat('A','B')", $onlineTable->getColumn('expression')->getDefault()); self::assertSame("concat('A','B')", $onlineTable->getColumn('expression')->getDefault());
$comparator = new Comparator(); $comparator = new Comparator();
$diff = $comparator->diffTable($table, $onlineTable); $diff = $comparator->diffTable($table, $onlineTable);
self::assertFalse($diff, "Tables should be identical with expression column defaults."); self::assertFalse($diff, "Tables should be identical with expression column defaults.");
}
} }
} }
...@@ -2,19 +2,19 @@ ...@@ -2,19 +2,19 @@
namespace Doctrine\Tests\DBAL\Platforms; namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb102Platform;
use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
class MariaDb1027PlatformTest extends AbstractMySQLPlatformTestCase class MariaDb102PlatformTest extends AbstractMySQLPlatformTestCase
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createPlatform() public function createPlatform()
{ {
return new MariaDb1027Platform(); return new MariaDb102Platform();
} }
public function testHasNativeJsonType() public function testHasNativeJsonType()
...@@ -34,29 +34,61 @@ class MariaDb1027PlatformTest extends AbstractMySQLPlatformTestCase ...@@ -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() 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 = 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')); $table->addColumn('def_blob', 'blob', array('default' => 'def'));
self::assertSame( 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) $this->_platform->getCreateTableSQL($table)
); );
$diffTable = clone $table; $diffTable = clone $table;
$diffTable->changeColumn('def_text', array('default' => 'def')); $diffTable->changeColumn('def_text', ['default' => "d''ef"]);
$diffTable->changeColumn('def_blob', array('default' => 'def')); $diffTable->changeColumn('def_blob', ['default' => 'def']);
$comparator = new Comparator(); $comparator = new Comparator();
self::assertFalse($comparator->diffTable($table, $diffTable)); 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