From 99ae3e7007bc07c11812840ee1ff22927a9db5fc Mon Sep 17 00:00:00 2001 From: mrxkon Date: Tue, 12 Jan 2021 20:04:21 +0200 Subject: [PATCH 1/2] drop test tables on cleanup Fixes #110 Adds functionality via `mysqli` on functions & cleanup to remove test tables from the database. --- cleanup.php | 3 +++ functions.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/cleanup.php b/cleanup.php index f2e03cf..0a794e3 100644 --- a/cleanup.php +++ b/cleanup.php @@ -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( diff --git a/functions.php b/functions.php index 62cfe8c..947504b 100644 --- a/functions.php +++ b/functions.php @@ -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(); +} \ No newline at end of file From 529f43d8cd60a9ad0b6e5058b6df6d6749ed9d1b Mon Sep 17 00:00:00 2001 From: mrxkon Date: Tue, 12 Jan 2021 20:09:13 +0200 Subject: [PATCH 2/2] new line eof new line eof --- functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.php b/functions.php index 947504b..ffa3cdf 100644 --- a/functions.php +++ b/functions.php @@ -269,4 +269,4 @@ function cleanup_db() { } $test_db->close(); -} \ No newline at end of file +}