Skip to content

Commit 75ec2de

Browse files
committed
Database: Add support for SPATIAL keys to dbDelta().
`dbDelta()` already supported spatial fields (by virtue of not checking field types), so it's nice to round that out with spatial key support, too. Fixes #36948. git-svn-id: https://develop.svn.wordpress.org/trunk@37574 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 673857f commit 75ec2de

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/wp-admin/includes/upgrade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,7 @@ function dbDelta( $queries = '', $execute = true ) {
22002200
case 'fulltext':
22012201
case 'unique':
22022202
case 'key':
2203+
case 'spatial':
22032204
$validfield = false;
22042205
$indices[] = trim(trim($fld), ", \n");
22052206
break;
@@ -2301,6 +2302,9 @@ function dbDelta( $queries = '', $execute = true ) {
23012302
if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
23022303
$index_string .= 'FULLTEXT ';
23032304
}
2305+
if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) {
2306+
$index_string .= 'SPATIAL ';
2307+
}
23042308
$index_string .= 'KEY ';
23052309
if ($index_name != 'PRIMARY') {
23062310
$index_string .= $index_name;

tests/phpunit/tests/dbdelta.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,52 @@ function test_query_with_backticks_does_not_throw_an_undefined_index_warning() {
488488

489489
$this->assertEmpty( $updates );
490490
}
491+
492+
/**
493+
* @ticket 36948
494+
*/
495+
function test_spatial_indices() {
496+
global $wpdb;
497+
498+
if ( version_compare( $wpdb->db_version(), '5.4', '<' ) ) {
499+
$this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' );
500+
}
501+
502+
$schema =
503+
"
504+
CREATE TABLE {$wpdb->prefix}spatial_index_test (
505+
non_spatial bigint(20) unsigned NOT NULL,
506+
spatial_value geometrycollection NOT NULL,
507+
KEY non_spatial (non_spatial),
508+
SPATIAL KEY spatial_key (spatial_value)
509+
) ENGINE=MyISAM;
510+
";
511+
512+
$wpdb->query( $schema );
513+
514+
$updates = dbDelta( $schema, false );
515+
516+
$this->assertEmpty( $updates );
517+
518+
$schema =
519+
"
520+
CREATE TABLE {$wpdb->prefix}spatial_index_test (
521+
non_spatial bigint(20) unsigned NOT NULL,
522+
spatial_value geometrycollection NOT NULL,
523+
spatial_value2 geometrycollection NOT NULL,
524+
KEY non_spatial (non_spatial),
525+
SPATIAL KEY spatial_key (spatial_value)
526+
SPATIAL KEY spatial_key2 (spatial_value2)
527+
) ENGINE=MyISAM;
528+
";
529+
530+
$updates = dbDelta( $schema, false );
531+
532+
$this->assertSame( array(
533+
"{$wpdb->prefix}spatial_index_test.spatial_value2" => "Added column {$wpdb->prefix}spatial_index_test.spatial_value2",
534+
"Added index {$wpdb->prefix}spatial_index_test SPATIAL KEY spatial_key2 (spatial_value2)"
535+
), $updates );
536+
537+
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spatial_index_test" );
538+
}
491539
}

0 commit comments

Comments
 (0)