<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54711] trunk/tests/phpunit/tests: Tests: Move `dbDelta()` tests to the `db` directory.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54711">54711</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54711","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-10-28 14:47:13 +0000 (Fri, 28 Oct 2022)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Tests: Move `dbDelta()` tests to the `db` directory.
This aims to bring some consistency with other database-related tests.
Includes adding a missing `@covers` tag to the class DocBlock.
Follow-up to <a href="https://core.trac.wordpress.org/changeset/30345">[30345]</a>, <a href="https://core.trac.wordpress.org/changeset/32108">[32108]</a>, <a href="https://core.trac.wordpress.org/changeset/32770">[32770]</a>, <a href="https://core.trac.wordpress.org/changeset/51798">[51798]</a>.
Props pbearne, SergeyBiryukov.
See <a href="https://core.trac.wordpress.org/ticket/56793">#56793</a>, <a href="https://core.trac.wordpress.org/ticket/56782">#56782</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsdbrealEscapephp">trunk/tests/phpunit/tests/db/realEscape.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsdbdbDeltaphp">trunk/tests/phpunit/tests/db/dbDelta.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsdbdeltaphp">trunk/tests/phpunit/tests/dbdelta.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsdbdbDeltaphpfromrev54710trunktestsphpunittestsdbdeltaphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/tests/db/dbDelta.php (from rev 54710, trunk/tests/phpunit/tests/dbdelta.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/db/dbDelta.php (rev 0)
+++ trunk/tests/phpunit/tests/db/dbDelta.php 2022-10-28 14:47:13 UTC (rev 54711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,1098 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test dbDelta()
+ *
+ * @group upgrade
+ * @group dbdelta
+ *
+ * @covers ::dbDelta
+ */
+class Tests_DB_dbDelta extends WP_UnitTestCase {
+
+ /**
+ * The maximum size of an index with utf8mb4 collation and charset with a standard
+ * byte limit of 767. floor(767/4) = 191 characters.
+ */
+ protected $max_index_length = 191;
+
+ /**
+ * Database engine used for creating tables.
+ *
+ * Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead.
+ */
+ protected $db_engine = '';
+
+ /**
+ * The database server version.
+ *
+ * @var string
+ */
+ private static $db_version;
+
+ /**
+ * Full database server information.
+ *
+ * @var string
+ */
+ private static $db_server_info;
+
+ /**
+ * Make sure the upgrade code is loaded before the tests are run.
+ */
+ public static function set_up_before_class() {
+
+ global $wpdb;
+
+ parent::set_up_before_class();
+
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
+
+ self::$db_version = $wpdb->db_version();
+ self::$db_server_info = $wpdb->db_server_info();
+ }
+
+ /**
+ * Create a custom table to be used in each test.
+ */
+ public function set_up() {
+
+ global $wpdb;
+
+ if ( version_compare( self::$db_version, '5.7', '<' ) ) {
+ // Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead.
+ $this->db_engine = 'ENGINE=MyISAM';
+ }
+
+ $wpdb->query(
+ $wpdb->prepare(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (" .
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ 'id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1(%d)),
+ KEY compound_key (id,column_1(%d)),
+ FULLTEXT KEY fulltext_key (column_1)' .
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ ") {$this->db_engine}
+ ",
+ $this->max_index_length,
+ $this->max_index_length
+ )
+ );
+
+ // This has to be called after the `CREATE TABLE` above as the `_create_temporary_tables` filter
+ // causes it to create a temporary table, and a temporary table cannot use a FULLTEXT index.
+ parent::set_up();
+ }
+
+ /**
+ * Delete the custom table on teardown.
+ */
+ public function tear_down() {
+
+ global $wpdb;
+
+ parent::tear_down();
+
+ // This has to be called after the parent `tearDown()` method.
+ $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test" );
+ }
+
+ /**
+ * Test table creation.
+ */
+ public function test_creating_a_table() {
+
+ remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
+ remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
+
+ global $wpdb;
+
+ $updates = dbDelta(
+ "CREATE TABLE {$wpdb->prefix}dbdelta_create_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ PRIMARY KEY (id)
+ );"
+ );
+
+ $expected = array(
+ "{$wpdb->prefix}dbdelta_create_test" => "Created table {$wpdb->prefix}dbdelta_create_test",
+ );
+
+ $this->assertSame( $expected, $updates );
+
+ $this->assertSame(
+ "{$wpdb->prefix}dbdelta_create_test",
+ $wpdb->get_var(
+ $wpdb->prepare(
+ 'SHOW TABLES LIKE %s',
+ $wpdb->esc_like( "{$wpdb->prefix}dbdelta_create_test" )
+ )
+ )
+ );
+
+ $wpdb->query( "DROP TABLE {$wpdb->prefix}dbdelta_create_test" );
+ }
+
+ /**
+ * Test that it does nothing for an existing table.
+ */
+ public function test_existing_table() {
+
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ "
+ );
+
+ $this->assertSame( array(), $updates );
+ }
+
+ /**
+ * Test the column type is updated.
+ */
+ public function test_column_type_change() {
+
+ global $wpdb;
+
+ // id: bigint(20) => int(11)
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id int(11) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ "
+ );
+
+ $bigint_display_width = '(20)';
+
+ /*
+ * MySQL 8.0.17 or later does not support display width for integer data types,
+ * so if display width is the only difference, it can be safely ignored.
+ * Note: This is specific to MySQL and does not affect MariaDB.
+ */
+ if ( version_compare( self::$db_version, '8.0.17', '>=' )
+ && ! str_contains( self::$db_server_info, 'MariaDB' )
+ ) {
+ $bigint_display_width = '';
+ }
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.id"
+ => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$bigint_display_width} to int(11)",
+ ),
+ $updates
+ );
+ }
+
+ /**
+ * Test new column added.
+ */
+ public function test_column_added() {
+
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ extra_col longtext,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ "
+ );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.extra_col"
+ => "Added column {$wpdb->prefix}dbdelta_test.extra_col",
+ ),
+ $updates
+ );
+
+ $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
+ $this->assertTableHasPrimaryKey( 'id', $wpdb->prefix . 'dbdelta_test' );
+ }
+
+ /**
+ * Test that it does nothing when a column is removed.
+ *
+ * @ticket 26801
+ */
+ public function test_columns_arent_removed() {
+
+ global $wpdb;
+
+ // No column column_1.
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ "
+ );
+
+ $this->assertSame( array(), $updates );
+
+ $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
+ }
+
+ /**
+ * Test that nothing happens with $execute is false.
+ */
+ public function test_no_execution() {
+
+ global $wpdb;
+
+ // Added column extra_col.
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ extra_col longtext,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ ",
+ false // Don't execute.
+ );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.extra_col"
+ => "Added column {$wpdb->prefix}dbdelta_test.extra_col",
+ ),
+ $updates
+ );
+
+ $this->assertTableHasNotColumn( 'extra_col', $wpdb->prefix . 'dbdelta_test' );
+ }
+
+ /**
+ * Test inserting into the database
+ */
+ public function test_insert_into_table() {
+ global $wpdb;
+
+ $insert = dbDelta(
+ "INSERT INTO {$wpdb->prefix}dbdelta_test (column_1) VALUES ('wcphilly2015')"
+ );
+
+ $this->assertSame(
+ array(),
+ $insert
+ );
+
+ $this->assertTableRowHasValue( 'column_1', 'wcphilly2015', $wpdb->prefix . 'dbdelta_test' );
+
+ }
+
+ /**
+ * Test that FULLTEXT indexes are detected.
+ *
+ * @ticket 14445
+ */
+ public function test_fulltext_index() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ )
+ ",
+ false
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ //
+ // Assertions.
+ //
+
+ /**
+ * Assert that a table has a row with a value in a field.
+ *
+ * @param string $column The field name.
+ * @param string $value The field value.
+ * @param string $table The database table name.
+ */
+ protected function assertTableRowHasValue( $column, $value, $table ) {
+ global $wpdb;
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $table_row = $wpdb->get_row( "select $column from {$table} where $column = '$value'" );
+
+ $expected = (object) array(
+ $column => $value,
+ );
+
+ $this->assertEquals( $expected, $table_row );
+ }
+
+ /**
+ * Assert that a table has a column.
+ *
+ * @param string $column The field name.
+ * @param string $table The database table name.
+ */
+ protected function assertTableHasColumn( $column, $table ) {
+ global $wpdb;
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $table_fields = $wpdb->get_results( "DESCRIBE $table" );
+
+ $this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
+ }
+
+ /**
+ * Assert that a table has a primary key.
+ *
+ * Checks for single-column primary keys. May not work for multi-column primary keys.
+ *
+ * @param string $column The column for the primary key.
+ * @param string $table The database table name.
+ */
+ protected function assertTableHasPrimaryKey( $column, $table ) {
+ global $wpdb;
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $table_indices = $wpdb->get_results( "SHOW INDEX FROM $table" );
+
+ $this->assertCount(
+ 1,
+ wp_list_filter(
+ $table_indices,
+ array(
+ 'Key_name' => 'PRIMARY',
+ 'Column_name' => $column,
+ ),
+ 'AND'
+ )
+ );
+ }
+
+ /**
+ * Assert that a table doesn't have a column.
+ *
+ * @param string $column The field name.
+ * @param string $table The database table name.
+ */
+ protected function assertTableHasNotColumn( $column, $table ) {
+
+ global $wpdb;
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $table_fields = $wpdb->get_results( "DESCRIBE $table" );
+
+ $this->assertCount( 0, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
+ }
+
+ /**
+ * @ticket 31869
+ */
+ public function test_truncated_index() {
+ global $wpdb;
+
+ if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
+ $this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
+ }
+
+ // This table needs to be actually created.
+ remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
+ remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
+
+ $table_name = "{$wpdb->prefix}test_truncated_index";
+
+ $create = "
+ CREATE TABLE $table_name (
+ a varchar(255) COLLATE utf8mb4_unicode_ci,
+ KEY a_key (a)
+ ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $wpdb->query( $create );
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $index = $wpdb->get_row( "SHOW INDEXES FROM $table_name WHERE Key_name='a_key';" );
+
+ $actual = dbDelta( $create, false );
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $wpdb->query( "DROP TABLE IF EXISTS $table_name;" );
+
+ if ( 191 !== $index->Sub_part ) {
+ $this->markTestSkipped( 'This test requires the index to be truncated.' );
+ }
+
+ $this->assertSame( array(), $actual );
+ }
+
+ /**
+ * @ticket 36748
+ */
+ public function test_dont_downsize_text_fields() {
+ global $wpdb;
+
+ $result = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 tinytext,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertSame( array(), $result );
+ }
+
+ /**
+ * @ticket 36748
+ */
+ public function test_dont_downsize_blob_fields() {
+ global $wpdb;
+
+ $result = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 tinyblob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertSame( array(), $result );
+ }
+
+ /**
+ * @ticket 36748
+ */
+ public function test_upsize_text_fields() {
+ global $wpdb;
+
+ $result = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 bigtext,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.column_2"
+ => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext",
+ ),
+ $result
+ );
+ }
+
+ /**
+ * @ticket 36748
+ */
+ public function test_upsize_blob_fields() {
+ global $wpdb;
+
+ $result = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 mediumblob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.column_3"
+ => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob",
+ ),
+ $result
+ );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_query_with_backticks_does_not_throw_an_undefined_index_warning() {
+ global $wpdb;
+
+ $schema = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT,
+ `column_1` varchar(255) NOT NULL,
+ PRIMARY KEY (id),
+ KEY compound_key (id,column_1($this->max_index_length))
+ )
+ ";
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $wpdb->query( $schema );
+
+ $updates = dbDelta( $schema, false );
+
+ $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 36948
+ */
+ public function test_spatial_indices() {
+ global $wpdb;
+
+ if ( version_compare( self::$db_version, '5.4', '<' ) ) {
+ $this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' );
+ }
+
+ $geometrycollection_name = 'geometrycollection';
+
+ if ( version_compare( self::$db_version, '8.0.11', '>=' )
+ && ! str_contains( self::$db_server_info, 'MariaDB' )
+ ) {
+ /*
+ * MySQL 8.0.11 or later uses GeomCollection data type name
+ * as the preferred synonym for GeometryCollection.
+ * Note: This is specific to MySQL and does not affect MariaDB.
+ */
+ $geometrycollection_name = 'geomcollection';
+ }
+
+ $schema =
+ "
+ CREATE TABLE {$wpdb->prefix}spatial_index_test (
+ non_spatial bigint(20) unsigned NOT NULL,
+ spatial_value {$geometrycollection_name} NOT NULL,
+ KEY non_spatial (non_spatial),
+ SPATIAL KEY spatial_key (spatial_value)
+ ) {$this->db_engine};
+ ";
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $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_name} NOT NULL,
+ spatial_value2 {$geometrycollection_name} NOT NULL,
+ KEY non_spatial (non_spatial),
+ SPATIAL KEY spatial_key (spatial_value)
+ SPATIAL KEY spatial_key2 (spatial_value2)
+ ) {$this->db_engine};
+ ";
+
+ $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" );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_query_with_backticks_does_not_cause_a_query_to_alter_all_columns_and_indices_to_run_even_if_none_have_changed() {
+ global $wpdb;
+
+ $schema = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT,
+ `references` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY `compound_key` (`id`,`references`($this->max_index_length))
+ )
+ ";
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $wpdb->query( $schema );
+
+ $updates = dbDelta( $schema );
+
+ $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test2" );
+ $compound_key_index = wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key' ) );
+
+ $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
+
+ $this->assertCount( 2, $compound_key_index );
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_index_with_a_reserved_keyword_can_be_created() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ `references` varchar(255) NOT NULL,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id , column_1($this->max_index_length)),
+ KEY compound_key2 (id,`references`($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test" );
+
+ $this->assertCount( 2, wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key2' ), 'AND' ) );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test.references" => "Added column {$wpdb->prefix}dbdelta_test.references",
+ 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `compound_key2` (`id`,`references`($this->max_index_length))",
+ ),
+ $updates
+ );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install() {
+ $updates = dbDelta( wp_get_db_schema() );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_key_and_index_and_fulltext_key_and_fulltext_index_and_unique_key_and_unique_index_indicies() {
+ global $wpdb;
+
+ $schema = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1),
+ INDEX key_2 (column_1($this->max_index_length)),
+ UNIQUE KEY key_3 (column_1($this->max_index_length)),
+ UNIQUE INDEX key_4 (column_1($this->max_index_length)),
+ FULLTEXT INDEX key_5 (column_1),
+ ) {$this->db_engine}
+ ";
+
+ $creates = dbDelta( $schema );
+ $this->assertSame(
+ array(
+ 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`($this->max_index_length))",
+ 1 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_3` (`column_1`($this->max_index_length))",
+ 2 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_4` (`column_1`($this->max_index_length))",
+ 3 => "Added index {$wpdb->prefix}dbdelta_test FULLTEXT KEY `key_5` (`column_1`)",
+ ),
+ $creates
+ );
+
+ $updates = dbDelta( $schema );
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_index_and_key_are_synonyms_and_do_not_recreate_indices() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ INDEX key_1 (column_1($this->max_index_length)),
+ INDEX compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT INDEX fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 20263
+ */
+ public function test_indices_with_prefix_limits_are_created_and_do_not_recreate_indices() {
+ global $wpdb;
+
+ $schema = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1),
+ KEY key_2 (column_1(10)),
+ KEY key_3 (column_2(100),column_1(10)),
+ ) {$this->db_engine}
+ ";
+
+ $creates = dbDelta( $schema );
+ $this->assertSame(
+ array(
+ 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`(10))",
+ 1 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_3` (`column_2`(100),`column_1`(10))",
+ ),
+ $creates
+ );
+
+ $updates = dbDelta( $schema );
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34959
+ */
+ public function test_index_col_names_with_order_do_not_recreate_indices() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length) DESC),
+ KEY compound_key (id,column_1($this->max_index_length) ASC),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34873
+ */
+ public function test_primary_key_with_single_space_does_not_recreate_index() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34869
+ */
+ public function test_index_definitions_with_spaces_do_not_recreate_indices() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 ( column_1($this->max_index_length)),
+ KEY compound_key (id, column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34871
+ */
+ public function test_index_types_are_not_case_sensitive_and_do_not_recreate_indices() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ key key_1 (column_1($this->max_index_length)),
+ key compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34874
+ */
+ public function test_key_names_are_not_case_sensitive_and_do_not_recreate_indices() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY KEY_1 (column_1($this->max_index_length)),
+ KEY compOUND_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY FULLtext_kEY (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34870
+ */
+ public function test_unchanged_key_lengths_do_not_recreate_index() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1({$this->max_index_length})),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ ",
+ false
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 34870
+ */
+ public function test_changed_key_lengths_do_not_recreate_index() {
+ global $wpdb;
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ KEY changing_key_length (column_1(20)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertSame(
+ array(
+ "Added index {$wpdb->prefix}dbdelta_test KEY `changing_key_length` (`column_1`(20))",
+ ),
+ $updates
+ );
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ KEY changing_key_length (column_1(50)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1($this->max_index_length)),
+ KEY compound_key (id,column_1($this->max_index_length)),
+ KEY changing_key_length (column_1(1)),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+
+ $updates = dbDelta(
+ "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test (
+ id bigint(20) NOT NULL AUTO_INCREMENT,
+ column_1 varchar(255) NOT NULL,
+ column_2 text,
+ column_3 blob,
+ PRIMARY KEY (id),
+ KEY key_1 (column_1),
+ KEY compound_key (id,column_1),
+ KEY changing_key_length (column_1),
+ FULLTEXT KEY fulltext_key (column_1)
+ ) {$this->db_engine}
+ "
+ );
+
+ $this->assertEmpty( $updates );
+ }
+
+ /**
+ * @ticket 31679
+ */
+ public function test_column_type_change_with_hyphens_in_name() {
+ global $wpdb;
+
+ $schema = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
+ `foo-bar` varchar(255) DEFAULT NULL
+ )
+ ";
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $wpdb->query( $schema );
+
+ $schema_update = "
+ CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
+ `foo-bar` text DEFAULT NULL
+ )
+ ";
+
+ $updates = dbDelta( $schema_update );
+
+ $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
+
+ $this->assertSame(
+ array(
+ "{$wpdb->prefix}dbdelta_test2.foo-bar" => "Changed type of {$wpdb->prefix}dbdelta_test2.foo-bar from varchar(255) to text",
+ ),
+ $updates
+ );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsdbrealEscapephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/db/realEscape.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/db/realEscape.php 2022-10-28 14:08:20 UTC (rev 54710)
+++ trunk/tests/phpunit/tests/db/realEscape.php 2022-10-28 14:47:13 UTC (rev 54711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3,7 +3,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Test WPDB _real_escape() method.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @group wpdb
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @group wpdb
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers wpdb::_real_escape
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_DB_RealEscape extends WP_UnitTestCase {
</span></span></pre></div>
<a id="trunktestsphpunittestsdbdeltaphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: trunk/tests/phpunit/tests/dbdelta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dbdelta.php 2022-10-28 14:08:20 UTC (rev 54710)
+++ trunk/tests/phpunit/tests/dbdelta.php 2022-10-28 14:47:13 UTC (rev 54711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,1096 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-
-/**
- * Test dbDelta()
- *
- * @group upgrade
- * @group dbdelta
- */
-class Tests_dbDelta extends WP_UnitTestCase {
-
- /**
- * The maximum size of an index with utf8mb4 collation and charset with a standard
- * byte limit of 767. floor(767/4) = 191 characters.
- */
- protected $max_index_length = 191;
-
- /**
- * Database engine used for creating tables.
- *
- * Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead.
- */
- protected $db_engine = '';
-
- /**
- * The database server version.
- *
- * @var string
- */
- private static $db_version;
-
- /**
- * Full database server information.
- *
- * @var string
- */
- private static $db_server_info;
-
- /**
- * Make sure the upgrade code is loaded before the tests are run.
- */
- public static function set_up_before_class() {
-
- global $wpdb;
-
- parent::set_up_before_class();
-
- require_once ABSPATH . 'wp-admin/includes/upgrade.php';
-
- self::$db_version = $wpdb->db_version();
- self::$db_server_info = $wpdb->db_server_info();
- }
-
- /**
- * Create a custom table to be used in each test.
- */
- public function set_up() {
-
- global $wpdb;
-
- if ( version_compare( self::$db_version, '5.7', '<' ) ) {
- // Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead.
- $this->db_engine = 'ENGINE=MyISAM';
- }
-
- $wpdb->query(
- $wpdb->prepare(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (" .
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- 'id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1(%d)),
- KEY compound_key (id,column_1(%d)),
- FULLTEXT KEY fulltext_key (column_1)' .
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- ") {$this->db_engine}
- ",
- $this->max_index_length,
- $this->max_index_length
- )
- );
-
- // This has to be called after the `CREATE TABLE` above as the `_create_temporary_tables` filter
- // causes it to create a temporary table, and a temporary table cannot use a FULLTEXT index.
- parent::set_up();
- }
-
- /**
- * Delete the custom table on teardown.
- */
- public function tear_down() {
-
- global $wpdb;
-
- parent::tear_down();
-
- // This has to be called after the parent `tearDown()` method.
- $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test" );
- }
-
- /**
- * Test table creation.
- */
- public function test_creating_a_table() {
-
- remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
- remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
-
- global $wpdb;
-
- $updates = dbDelta(
- "CREATE TABLE {$wpdb->prefix}dbdelta_create_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- PRIMARY KEY (id)
- );"
- );
-
- $expected = array(
- "{$wpdb->prefix}dbdelta_create_test" => "Created table {$wpdb->prefix}dbdelta_create_test",
- );
-
- $this->assertSame( $expected, $updates );
-
- $this->assertSame(
- "{$wpdb->prefix}dbdelta_create_test",
- $wpdb->get_var(
- $wpdb->prepare(
- 'SHOW TABLES LIKE %s',
- $wpdb->esc_like( "{$wpdb->prefix}dbdelta_create_test" )
- )
- )
- );
-
- $wpdb->query( "DROP TABLE {$wpdb->prefix}dbdelta_create_test" );
- }
-
- /**
- * Test that it does nothing for an existing table.
- */
- public function test_existing_table() {
-
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- "
- );
-
- $this->assertSame( array(), $updates );
- }
-
- /**
- * Test the column type is updated.
- */
- public function test_column_type_change() {
-
- global $wpdb;
-
- // id: bigint(20) => int(11)
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id int(11) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- "
- );
-
- $bigint_display_width = '(20)';
-
- /*
- * MySQL 8.0.17 or later does not support display width for integer data types,
- * so if display width is the only difference, it can be safely ignored.
- * Note: This is specific to MySQL and does not affect MariaDB.
- */
- if ( version_compare( self::$db_version, '8.0.17', '>=' )
- && ! str_contains( self::$db_server_info, 'MariaDB' )
- ) {
- $bigint_display_width = '';
- }
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.id"
- => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$bigint_display_width} to int(11)",
- ),
- $updates
- );
- }
-
- /**
- * Test new column added.
- */
- public function test_column_added() {
-
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- extra_col longtext,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- "
- );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.extra_col"
- => "Added column {$wpdb->prefix}dbdelta_test.extra_col",
- ),
- $updates
- );
-
- $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
- $this->assertTableHasPrimaryKey( 'id', $wpdb->prefix . 'dbdelta_test' );
- }
-
- /**
- * Test that it does nothing when a column is removed.
- *
- * @ticket 26801
- */
- public function test_columns_arent_removed() {
-
- global $wpdb;
-
- // No column column_1.
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- "
- );
-
- $this->assertSame( array(), $updates );
-
- $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
- }
-
- /**
- * Test that nothing happens with $execute is false.
- */
- public function test_no_execution() {
-
- global $wpdb;
-
- // Added column extra_col.
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- extra_col longtext,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- ",
- false // Don't execute.
- );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.extra_col"
- => "Added column {$wpdb->prefix}dbdelta_test.extra_col",
- ),
- $updates
- );
-
- $this->assertTableHasNotColumn( 'extra_col', $wpdb->prefix . 'dbdelta_test' );
- }
-
- /**
- * Test inserting into the database
- */
- public function test_insert_into_table() {
- global $wpdb;
-
- $insert = dbDelta(
- "INSERT INTO {$wpdb->prefix}dbdelta_test (column_1) VALUES ('wcphilly2015')"
- );
-
- $this->assertSame(
- array(),
- $insert
- );
-
- $this->assertTableRowHasValue( 'column_1', 'wcphilly2015', $wpdb->prefix . 'dbdelta_test' );
-
- }
-
- /**
- * Test that FULLTEXT indexes are detected.
- *
- * @ticket 14445
- */
- public function test_fulltext_index() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- )
- ",
- false
- );
-
- $this->assertEmpty( $updates );
- }
-
- //
- // Assertions.
- //
-
- /**
- * Assert that a table has a row with a value in a field.
- *
- * @param string $column The field name.
- * @param string $value The field value.
- * @param string $table The database table name.
- */
- protected function assertTableRowHasValue( $column, $value, $table ) {
- global $wpdb;
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $table_row = $wpdb->get_row( "select $column from {$table} where $column = '$value'" );
-
- $expected = (object) array(
- $column => $value,
- );
-
- $this->assertEquals( $expected, $table_row );
- }
-
- /**
- * Assert that a table has a column.
- *
- * @param string $column The field name.
- * @param string $table The database table name.
- */
- protected function assertTableHasColumn( $column, $table ) {
- global $wpdb;
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $table_fields = $wpdb->get_results( "DESCRIBE $table" );
-
- $this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
- }
-
- /**
- * Assert that a table has a primary key.
- *
- * Checks for single-column primary keys. May not work for multi-column primary keys.
- *
- * @param string $column The column for the primary key.
- * @param string $table The database table name.
- */
- protected function assertTableHasPrimaryKey( $column, $table ) {
- global $wpdb;
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $table_indices = $wpdb->get_results( "SHOW INDEX FROM $table" );
-
- $this->assertCount(
- 1,
- wp_list_filter(
- $table_indices,
- array(
- 'Key_name' => 'PRIMARY',
- 'Column_name' => $column,
- ),
- 'AND'
- )
- );
- }
-
- /**
- * Assert that a table doesn't have a column.
- *
- * @param string $column The field name.
- * @param string $table The database table name.
- */
- protected function assertTableHasNotColumn( $column, $table ) {
-
- global $wpdb;
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $table_fields = $wpdb->get_results( "DESCRIBE $table" );
-
- $this->assertCount( 0, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
- }
-
- /**
- * @ticket 31869
- */
- public function test_truncated_index() {
- global $wpdb;
-
- if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
- $this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
- }
-
- // This table needs to be actually created.
- remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
- remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
-
- $table_name = "{$wpdb->prefix}test_truncated_index";
-
- $create = "
- CREATE TABLE $table_name (
- a varchar(255) COLLATE utf8mb4_unicode_ci,
- KEY a_key (a)
- ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
-
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $wpdb->query( $create );
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $index = $wpdb->get_row( "SHOW INDEXES FROM $table_name WHERE Key_name='a_key';" );
-
- $actual = dbDelta( $create, false );
-
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $wpdb->query( "DROP TABLE IF EXISTS $table_name;" );
-
- if ( 191 !== $index->Sub_part ) {
- $this->markTestSkipped( 'This test requires the index to be truncated.' );
- }
-
- $this->assertSame( array(), $actual );
- }
-
- /**
- * @ticket 36748
- */
- public function test_dont_downsize_text_fields() {
- global $wpdb;
-
- $result = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 tinytext,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertSame( array(), $result );
- }
-
- /**
- * @ticket 36748
- */
- public function test_dont_downsize_blob_fields() {
- global $wpdb;
-
- $result = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 tinyblob,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertSame( array(), $result );
- }
-
- /**
- * @ticket 36748
- */
- public function test_upsize_text_fields() {
- global $wpdb;
-
- $result = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 bigtext,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.column_2"
- => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext",
- ),
- $result
- );
- }
-
- /**
- * @ticket 36748
- */
- public function test_upsize_blob_fields() {
- global $wpdb;
-
- $result = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 mediumblob,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.column_3"
- => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob",
- ),
- $result
- );
- }
-
- /**
- * @ticket 20263
- */
- public function test_query_with_backticks_does_not_throw_an_undefined_index_warning() {
- global $wpdb;
-
- $schema = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `column_1` varchar(255) NOT NULL,
- PRIMARY KEY (id),
- KEY compound_key (id,column_1($this->max_index_length))
- )
- ";
-
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $wpdb->query( $schema );
-
- $updates = dbDelta( $schema, false );
-
- $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 36948
- */
- public function test_spatial_indices() {
- global $wpdb;
-
- if ( version_compare( self::$db_version, '5.4', '<' ) ) {
- $this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' );
- }
-
- $geometrycollection_name = 'geometrycollection';
-
- if ( version_compare( self::$db_version, '8.0.11', '>=' )
- && ! str_contains( self::$db_server_info, 'MariaDB' )
- ) {
- /*
- * MySQL 8.0.11 or later uses GeomCollection data type name
- * as the preferred synonym for GeometryCollection.
- * Note: This is specific to MySQL and does not affect MariaDB.
- */
- $geometrycollection_name = 'geomcollection';
- }
-
- $schema =
- "
- CREATE TABLE {$wpdb->prefix}spatial_index_test (
- non_spatial bigint(20) unsigned NOT NULL,
- spatial_value {$geometrycollection_name} NOT NULL,
- KEY non_spatial (non_spatial),
- SPATIAL KEY spatial_key (spatial_value)
- ) {$this->db_engine};
- ";
-
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $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_name} NOT NULL,
- spatial_value2 {$geometrycollection_name} NOT NULL,
- KEY non_spatial (non_spatial),
- SPATIAL KEY spatial_key (spatial_value)
- SPATIAL KEY spatial_key2 (spatial_value2)
- ) {$this->db_engine};
- ";
-
- $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" );
- }
-
- /**
- * @ticket 20263
- */
- public function test_query_with_backticks_does_not_cause_a_query_to_alter_all_columns_and_indices_to_run_even_if_none_have_changed() {
- global $wpdb;
-
- $schema = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `references` varchar(255) NOT NULL,
- PRIMARY KEY (`id`),
- KEY `compound_key` (`id`,`references`($this->max_index_length))
- )
- ";
-
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $wpdb->query( $schema );
-
- $updates = dbDelta( $schema );
-
- $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test2" );
- $compound_key_index = wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key' ) );
-
- $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
-
- $this->assertCount( 2, $compound_key_index );
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 20263
- */
- public function test_index_with_a_reserved_keyword_can_be_created() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- `references` varchar(255) NOT NULL,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id , column_1($this->max_index_length)),
- KEY compound_key2 (id,`references`($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test" );
-
- $this->assertCount( 2, wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key2' ), 'AND' ) );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test.references" => "Added column {$wpdb->prefix}dbdelta_test.references",
- 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `compound_key2` (`id`,`references`($this->max_index_length))",
- ),
- $updates
- );
- }
-
- /**
- * @ticket 20263
- */
- public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install() {
- $updates = dbDelta( wp_get_db_schema() );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 20263
- */
- public function test_key_and_index_and_fulltext_key_and_fulltext_index_and_unique_key_and_unique_index_indicies() {
- global $wpdb;
-
- $schema = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1),
- INDEX key_2 (column_1($this->max_index_length)),
- UNIQUE KEY key_3 (column_1($this->max_index_length)),
- UNIQUE INDEX key_4 (column_1($this->max_index_length)),
- FULLTEXT INDEX key_5 (column_1),
- ) {$this->db_engine}
- ";
-
- $creates = dbDelta( $schema );
- $this->assertSame(
- array(
- 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`($this->max_index_length))",
- 1 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_3` (`column_1`($this->max_index_length))",
- 2 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_4` (`column_1`($this->max_index_length))",
- 3 => "Added index {$wpdb->prefix}dbdelta_test FULLTEXT KEY `key_5` (`column_1`)",
- ),
- $creates
- );
-
- $updates = dbDelta( $schema );
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 20263
- */
- public function test_index_and_key_are_synonyms_and_do_not_recreate_indices() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- INDEX key_1 (column_1($this->max_index_length)),
- INDEX compound_key (id,column_1($this->max_index_length)),
- FULLTEXT INDEX fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 20263
- */
- public function test_indices_with_prefix_limits_are_created_and_do_not_recreate_indices() {
- global $wpdb;
-
- $schema = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1),
- KEY key_2 (column_1(10)),
- KEY key_3 (column_2(100),column_1(10)),
- ) {$this->db_engine}
- ";
-
- $creates = dbDelta( $schema );
- $this->assertSame(
- array(
- 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`(10))",
- 1 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_3` (`column_2`(100),`column_1`(10))",
- ),
- $creates
- );
-
- $updates = dbDelta( $schema );
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34959
- */
- public function test_index_col_names_with_order_do_not_recreate_indices() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length) DESC),
- KEY compound_key (id,column_1($this->max_index_length) ASC),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34873
- */
- public function test_primary_key_with_single_space_does_not_recreate_index() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34869
- */
- public function test_index_definitions_with_spaces_do_not_recreate_indices() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 ( column_1($this->max_index_length)),
- KEY compound_key (id, column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34871
- */
- public function test_index_types_are_not_case_sensitive_and_do_not_recreate_indices() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- key key_1 (column_1($this->max_index_length)),
- key compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34874
- */
- public function test_key_names_are_not_case_sensitive_and_do_not_recreate_indices() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY KEY_1 (column_1($this->max_index_length)),
- KEY compOUND_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY FULLtext_kEY (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34870
- */
- public function test_unchanged_key_lengths_do_not_recreate_index() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1({$this->max_index_length})),
- KEY compound_key (id,column_1($this->max_index_length)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- ",
- false
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 34870
- */
- public function test_changed_key_lengths_do_not_recreate_index() {
- global $wpdb;
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- KEY changing_key_length (column_1(20)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertSame(
- array(
- "Added index {$wpdb->prefix}dbdelta_test KEY `changing_key_length` (`column_1`(20))",
- ),
- $updates
- );
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- KEY changing_key_length (column_1(50)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1($this->max_index_length)),
- KEY compound_key (id,column_1($this->max_index_length)),
- KEY changing_key_length (column_1(1)),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
-
- $updates = dbDelta(
- "
- CREATE TABLE {$wpdb->prefix}dbdelta_test (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- column_1 varchar(255) NOT NULL,
- column_2 text,
- column_3 blob,
- PRIMARY KEY (id),
- KEY key_1 (column_1),
- KEY compound_key (id,column_1),
- KEY changing_key_length (column_1),
- FULLTEXT KEY fulltext_key (column_1)
- ) {$this->db_engine}
- "
- );
-
- $this->assertEmpty( $updates );
- }
-
- /**
- * @ticket 31679
- */
- public function test_column_type_change_with_hyphens_in_name() {
- global $wpdb;
-
- $schema = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
- `foo-bar` varchar(255) DEFAULT NULL
- )
- ";
-
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $wpdb->query( $schema );
-
- $schema_update = "
- CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
- `foo-bar` text DEFAULT NULL
- )
- ";
-
- $updates = dbDelta( $schema_update );
-
- $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
-
- $this->assertSame(
- array(
- "{$wpdb->prefix}dbdelta_test2.foo-bar" => "Changed type of {$wpdb->prefix}dbdelta_test2.foo-bar from varchar(255) to text",
- ),
- $updates
- );
- }
-}
</del></span></pre>
</div>
</div>
</body>
</html>