[wp-trac] [WordPress Trac] #49654: maybe_drop_table
WordPress Trac
noreply at wordpress.org
Fri Apr 7 15:21:12 UTC 2023
#49654: maybe_drop_table
-------------------------+------------------------------
Reporter: bhubbard | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Database | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
-------------------------+------------------------------
Comment (by bhubbard):
{{{#!php
<?php
class MaybeDropTableTest extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
}
public function test_should_return_true_if_table_not_exists() {
global $wpdb;
$table_name = 'non_existent_table';
$result = maybe_drop_table( $table_name );
$this->assertTrue( $result );
}
public function
test_should_return_false_if_table_exists_and_is_dropped_successfully() {
global $wpdb;
$table_name = $this->factory->post->create();
$result_before_drop = $wpdb->get_var( 'SHOW TABLES LIKE "%' .
$table_name . '%"' );
$result = maybe_drop_table( $table_name );
$result_after_drop = $wpdb->get_var( 'SHOW TABLES LIKE "%' .
$table_name . '%"' );
$this->assertTrue( $result_before_drop == $table_name );
$this->assertNull( $result_after_drop );
$this->assertFalse( $result );
}
public function
test_should_return_false_if_table_exists_but_dropping_fails() {
global $wpdb;
// Create a table that has foreign key constraints to simulate
failing to drop.
$referenced_table = $this->factory->post->create();
$table_name = $wpdb->prefix . 'test_table';
$sql = "
CREATE TABLE {$table_name} (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (post_id) REFERENCES {$wpdb->posts} (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE
utf8mb4_unicode_ci
";
$wpdb->query( $sql );
$result_before_drop = $wpdb->get_var( 'SHOW TABLES LIKE "%' .
$table_name . '%"' );
$result = maybe_drop_table( $table_name );
$result_after_drop = $wpdb->get_var( 'SHOW TABLES LIKE "%' .
$table_name . '%"' );
$this->assertTrue( $result_before_drop == $table_name );
$this->assertTrue( $result_after_drop == $table_name );
$this->assertTrue( $result );
}
}
The tests above cover the following scenarios:
The table does not exist, and the function returns true.
The table exists, but dropping it is successful, and the function returns
false.
The table exists, but dropping it fails, and the function returns true.
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/49654#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list