Skip to content

Commit

Permalink
drop test tables on cleanup
Browse files Browse the repository at this point in the history
Fixes WordPress#110

Adds functionality via `mysqli` on functions & cleanup to remove test tables from the database.
  • Loading branch information
mrxkon committed Jan 12, 2021
1 parent 03ce121 commit 99ae3e7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
$WPT_TEST_DIR = getenv( 'WPT_TEST_DIR' );
$WPT_RM_TEST_DIR_CMD = getenv( 'WPT_RM_TEST_DIR_CMD' ) ? : 'rm -r ' . $WPT_TEST_DIR;

// Cleanup the Database.
cleanup_db();

// Clean up the preparation directory.
// Only forcefully delete the .git directory, to prevent disasters otherwise.
perform_operations( array(
Expand Down
50 changes: 50 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,53 @@ function get_env_details() {
$env['system_utils']['openssl'] = str_replace( 'OpenSSL ', '', trim( shell_exec( 'openssl version' ) ) );
return $env;
}

/**
* Cleanup the Database.
*/
function cleanup_db() {
$prefix = getenv( 'WPT_TABLE_PREFIX' ) ? getenv( 'WPT_TABLE_PREFIX' ) : 'wptests_';

$tables = array(
'users',
'usermeta',
'posts',
'comments',
'links',
'options',
'postmeta',
'terms',
'term_taxonomy',
'term_relationships',
'termmeta',
'commentmeta',
'blogs',
'blogmeta',
'signups',
'site',
'sitemeta',
'sitecategories',
'registration_log',
);

$test_db = new mysqli(
getenv( 'WPT_DB_HOST' ),
getenv( 'WPT_DB_USER' ),
getenv( 'WPT_DB_PASSWORD' ),
getenv( 'WPT_DB_NAME' )
);

if ( $test_db->connect_errno ) {
error_message( 'Could not connect to database.' );
}

log_message( 'Clearing database.' );

foreach ( $tables as $table ) {
if ( ! $test_db->query( "DROP TABLE IF EXISTS {$prefix}{$table}" ) ) {
error_message( "Aborting. Could not DROP table {$prefix}{$table}." );
}
}

$test_db->close();
}

0 comments on commit 99ae3e7

Please sign in to comment.