Unverified Commit ce45348a authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #2415 from bburnichon/feature/mysql-table-metadata

Add some MySQL platform data in Tables
parents ff6cbdbc a7cffda8
......@@ -380,6 +380,20 @@ class MySqlPlatform extends AbstractPlatform
'FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ' . $database . ' AND TABLE_NAME = ' . $table;
}
public function getListTableMetadataSQL(string $table, ?string $database = null) : string
{
return sprintf(
<<<'SQL'
SELECT ENGINE, AUTO_INCREMENT, TABLE_COLLATION, TABLE_COMMENT, CREATE_OPTIONS
FROM information_schema.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = %s AND TABLE_NAME = %s
SQL
,
$database ? $this->quoteStringLiteral($database) : 'DATABASE()',
$this->quoteStringLiteral($table)
);
}
/**
* {@inheritDoc}
*/
......
......@@ -10,6 +10,7 @@ use function array_change_key_case;
use function array_shift;
use function array_values;
use function end;
use function explode;
use function preg_match;
use function preg_replace;
use function str_replace;
......@@ -17,6 +18,7 @@ use function stripslashes;
use function strpos;
use function strtok;
use function strtolower;
use function trim;
/**
* Schema manager for the MySql RDBMS.
......@@ -284,4 +286,44 @@ class MySqlSchemaManager extends AbstractSchemaManager
return $result;
}
public function listTableDetails($tableName)
{
$table = parent::listTableDetails($tableName);
/** @var MySqlPlatform $platform */
$platform = $this->_platform;
$sql = $platform->getListTableMetadataSQL($tableName);
$tableOptions = $this->_conn->fetchAssoc($sql);
$table->addOption('engine', $tableOptions['ENGINE']);
if ($tableOptions['TABLE_COLLATION'] !== null) {
$table->addOption('collation', $tableOptions['TABLE_COLLATION']);
}
if ($tableOptions['AUTO_INCREMENT'] !== null) {
$table->addOption('autoincrement', $tableOptions['AUTO_INCREMENT']);
}
$table->addOption('comment', $tableOptions['TABLE_COMMENT']);
if ($tableOptions['CREATE_OPTIONS'] === null) {
return $table;
}
$createOptionsString = trim($tableOptions['CREATE_OPTIONS']);
$createOptions = [];
if ($createOptionsString !== '') {
foreach (explode(' ', $createOptionsString) as $option) {
[$createOption, $value] = explode('=', $option);
$createOptions[$createOption] = $value;
}
}
$table->addOption('create_options', $createOptions);
return $table;
}
}
......@@ -486,4 +486,43 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_with_create');
self::assertSame($default, $onlineTable->getColumn('col1')->getDefault());
}
public function testEnsureTableOptionsAreReflectedInMetadata() : void
{
$this->connection->query('DROP TABLE IF EXISTS test_table_metadata');
$sql = <<<'SQL'
CREATE TABLE test_table_metadata(
col1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY
)
COLLATE utf8_general_ci
ENGINE InnoDB
ROW_FORMAT COMPRESSED
COMMENT 'This is a test'
AUTO_INCREMENT=42
SQL;
$this->connection->query($sql);
$onlineTable = $this->schemaManager->listTableDetails('test_table_metadata');
self::assertEquals('InnoDB', $onlineTable->getOption('engine'));
self::assertEquals('utf8_general_ci', $onlineTable->getOption('collation'));
self::assertEquals(42, $onlineTable->getOption('autoincrement'));
self::assertEquals('This is a test', $onlineTable->getOption('comment'));
self::assertEquals(['row_format' => 'COMPRESSED'], $onlineTable->getOption('create_options'));
}
public function testEnsureTableWithoutOptionsAreReflectedInMetadata() : void
{
$this->connection->query('DROP TABLE IF EXISTS test_table_empty_metadata');
$this->connection->query('CREATE TABLE test_table_empty_metadata(col1 INT NOT NULL)');
$onlineTable = $this->schemaManager->listTableDetails('test_table_empty_metadata');
self::assertNotEmpty($onlineTable->getOption('engine'));
// collation could be set to default or not set, information_schema indicate a possibly null value
self::assertFalse($onlineTable->hasOption('autoincrement'));
self::assertEquals('', $onlineTable->getOption('comment'));
self::assertEquals([], $onlineTable->getOption('create_options'));
}
}
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