Skip to content

Commit ae52afd

Browse files
Preserve index prefix lengths and order in SHOW CREATE TABLE primary keys
1 parent abf0dac commit ae52afd

2 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7320,21 +7320,24 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
73207320
}
73217321

73227322
// 7. Generate CREATE TABLE statement constraints, collect indexes.
7323+
$format_index_column = function ( $column ) {
7324+
$definition = $this->quote_mysql_identifier( $column['COLUMN_NAME'] );
7325+
if ( null !== $column['SUB_PART'] ) {
7326+
$definition .= sprintf( '(%d)', $column['SUB_PART'] );
7327+
}
7328+
if ( 'D' === $column['COLLATION'] ) {
7329+
$definition .= ' DESC';
7330+
}
7331+
return $definition;
7332+
};
7333+
73237334
foreach ( $grouped_constraints as $constraint ) {
73247335
ksort( $constraint );
73257336
$info = $constraint[1];
73267337

73277338
if ( 'PRIMARY' === $info['INDEX_NAME'] ) {
73287339
$sql = ' PRIMARY KEY (';
7329-
$sql .= implode(
7330-
', ',
7331-
array_map(
7332-
function ( $column ) {
7333-
return $this->quote_mysql_identifier( $column['COLUMN_NAME'] );
7334-
},
7335-
$constraint
7336-
)
7337-
);
7340+
$sql .= implode( ', ', array_map( $format_index_column, $constraint ) );
73387341
$sql .= ')';
73397342
} else {
73407343
$is_unique = '0' === $info['NON_UNIQUE'];
@@ -7347,22 +7350,7 @@ function ( $column ) {
73477350
);
73487351
$sql .= $this->quote_mysql_identifier( $info['INDEX_NAME'] );
73497352
$sql .= ' (';
7350-
$sql .= implode(
7351-
', ',
7352-
array_map(
7353-
function ( $column ) {
7354-
$definition = $this->quote_mysql_identifier( $column['COLUMN_NAME'] );
7355-
if ( null !== $column['SUB_PART'] ) {
7356-
$definition .= sprintf( '(%d)', $column['SUB_PART'] );
7357-
}
7358-
if ( 'D' === $column['COLLATION'] ) {
7359-
$definition .= ' DESC';
7360-
}
7361-
return $definition;
7362-
},
7363-
$constraint
7364-
)
7365-
);
7353+
$sql .= implode( ', ', array_map( $format_index_column, $constraint ) );
73667354
$sql .= ')';
73677355
}
73687356

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,37 @@ public function testShowCreateTableAutoIncrement(): void {
762762
}
763763

764764

765+
public function testShowCreateTablePrimaryKeyWithIndexPrefix(): void {
766+
$this->assertQuery(
767+
'CREATE TABLE t (session_id MEDIUMTEXT NOT NULL, value INT, PRIMARY KEY (session_id(100)))'
768+
);
769+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
770+
$this->assertStringContainsString(
771+
'PRIMARY KEY (`session_id`(100))',
772+
$result[0]->{'Create Table'}
773+
);
774+
775+
// A composite primary key preserves the prefix only where it is defined.
776+
$this->assertQuery(
777+
'CREATE TABLE t2 (a VARCHAR(255) NOT NULL, b MEDIUMTEXT NOT NULL, PRIMARY KEY (a, b(50)))'
778+
);
779+
$result = $this->assertQuery( 'SHOW CREATE TABLE t2' );
780+
$this->assertStringContainsString(
781+
'PRIMARY KEY (`a`, `b`(50))',
782+
$result[0]->{'Create Table'}
783+
);
784+
785+
// Descending key parts are preserved as well, as in MySQL 8.
786+
$this->assertQuery(
787+
'CREATE TABLE t3 (a INT NOT NULL, b VARCHAR(10) NOT NULL, PRIMARY KEY (a DESC, b))'
788+
);
789+
$result = $this->assertQuery( 'SHOW CREATE TABLE t3' );
790+
$this->assertStringContainsString(
791+
'PRIMARY KEY (`a` DESC, `b`)',
792+
$result[0]->{'Create Table'}
793+
);
794+
}
795+
765796
public function testCreateTableSetAutoIncrement(): void {
766797
$this->assertQuery(
767798
'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT) AUTO_INCREMENT=100'

0 commit comments

Comments
 (0)