Make WordPress Core

Opened 4 years ago

Last modified 12 months ago

#49654 new enhancement

maybe_drop_table

Reported by: bhubbard's profile bhubbard Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Database Keywords:
Focuses: Cc:

Description

We currently have a function for maybe_create_table, I was looking for one to maybe_drop_table. This would be a very helpful function.

Attachments (1)

49654.patch (1.2 KB) - added by esoj 4 years ago.
Made a quick patch. however untested at the moment

Download all attachments as: .zip

Change History (2)

@esoj
4 years ago

Made a quick patch. however untested at the moment

#1 @bhubbard
12 months ago

<?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.

Note: See TracTickets for help on using tickets.