From a51a68fb75f732b622e6b03190a77e290019e5bc Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Thu, 26 May 2016 04:58:13 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/upgrade.php | 4 +++ tests/phpunit/tests/dbdelta.php | 48 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 589d294b02..c025d81b76 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -2200,6 +2200,7 @@ function dbDelta( $queries = '', $execute = true ) { case 'fulltext': case 'unique': case 'key': + case 'spatial': $validfield = false; $indices[] = trim(trim($fld), ", \n"); break; @@ -2301,6 +2302,9 @@ function dbDelta( $queries = '', $execute = true ) { if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) { $index_string .= 'FULLTEXT '; } + if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) { + $index_string .= 'SPATIAL '; + } $index_string .= 'KEY '; if ($index_name != 'PRIMARY') { $index_string .= $index_name; diff --git a/tests/phpunit/tests/dbdelta.php b/tests/phpunit/tests/dbdelta.php index 426a702b6f..9f293417db 100644 --- a/tests/phpunit/tests/dbdelta.php +++ b/tests/phpunit/tests/dbdelta.php @@ -488,4 +488,52 @@ class Tests_dbDelta extends WP_UnitTestCase { $this->assertEmpty( $updates ); } + + /** + * @ticket 36948 + */ + function test_spatial_indices() { + global $wpdb; + + if ( version_compare( $wpdb->db_version(), '5.4', '<' ) ) { + $this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' ); + } + + $schema = + " + CREATE TABLE {$wpdb->prefix}spatial_index_test ( + non_spatial bigint(20) unsigned NOT NULL, + spatial_value geometrycollection NOT NULL, + KEY non_spatial (non_spatial), + SPATIAL KEY spatial_key (spatial_value) + ) ENGINE=MyISAM; + "; + + $wpdb->query( $schema ); + + $updates = dbDelta( $schema, false ); + + $this->assertEmpty( $updates ); + + $schema = + " + CREATE TABLE {$wpdb->prefix}spatial_index_test ( + non_spatial bigint(20) unsigned NOT NULL, + spatial_value geometrycollection NOT NULL, + spatial_value2 geometrycollection NOT NULL, + KEY non_spatial (non_spatial), + SPATIAL KEY spatial_key (spatial_value) + SPATIAL KEY spatial_key2 (spatial_value2) + ) ENGINE=MyISAM; + "; + + $updates = dbDelta( $schema, false ); + + $this->assertSame( array( + "{$wpdb->prefix}spatial_index_test.spatial_value2" => "Added column {$wpdb->prefix}spatial_index_test.spatial_value2", + "Added index {$wpdb->prefix}spatial_index_test SPATIAL KEY spatial_key2 (spatial_value2)" + ), $updates ); + + $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spatial_index_test" ); + } }