Opened 10 years ago
Closed 10 years ago
#37046 closed defect (bug) (fixed)
Unit test initial setup fails if plugins added tables with foreign key constraints
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.6 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Build/Test Tools | Keywords: | |
| Focuses: | Cc: |
Description
When setting up a new test run, the script would drop all tables and recreate them from scratch. See here: https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/install.php?marks=43-45#L43
Relevant code copied:
foreach ( $wpdb->tables() as $table => $prefixed_table ) {
$wpdb->query( "DROP TABLE IF EXISTS $prefixed_table" );
}
If there's a foreign key constraint on a table that has not been dropped yet that references one that's being dropped, MySQL (and MariaDB) will return an error about the foreign key constraint failing, and won't actually drop the table.
This can lead to dirty state for the next test.
Attachments (1)
Change History (5)
#2
@
10 years ago
- Owner set to pento
- Status changed from new to reviewing
This looks good to me, @pento thoughts?
Note: See
TracTickets for help on using
tickets.
Use case: testing a plugin that makes use of the WP testing library.
When the plugin's install script runs (either on init or hooked into activation), it creates a new table, for example:
$wpdb->query( "CREATE TABLE {$wpdb->prefix}tablename ( id BIGINT(20) UNSIGNED AUTO_INCREMENT, post_id BIGINT(20) UNSIGNED, some_custom_value VARCHAR(191), PRIMARY KEY (`id`), KEY (`post_id`), KEY (`some_custom_value`), FOREIGN KEY (post_id) REFERENCES {$wpdb->posts}(ID) ON DELETE CASCADE ) ENGINE=INNODB {$collate}");where
$collateis$collate = ''; if ( $wpdb->has_cap( 'collation' ) ) { if ( ! empty( $wpdb->charset ) ) { $collate .= "DEFAULT CHARACTER SET $wpdb->charset"; } if ( ! empty( $wpdb->collate ) ) { $collate .= " COLLATE $wpdb->collate"; } }Next time I want to run the tests, the
poststable will not have been deleted. This only works iftablenamecomes later in alphabetical order thanpostsin this context.Patch incoming.