From 0569da23e163c78d88edec77a5245f5b48d3bf2c Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Tue, 20 Apr 2021 14:47:07 -0400 Subject: [PATCH] Drop all DB tables before installation during setup. (#9) This ensures a clean state for the extension currently being tested. The alternative requires the user to create and keep track of multiple test databases, or manually delete/re-recreate the database every time they switch between extensions being tested. Now, a simple `composer test:setup` will always reset the test environment to the original state. --- .../src/integration/Setup/SetupScript.php | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/php-packages/testing/src/integration/Setup/SetupScript.php b/php-packages/testing/src/integration/Setup/SetupScript.php index ffde8d9f0..931a6af12 100644 --- a/php-packages/testing/src/integration/Setup/SetupScript.php +++ b/php-packages/testing/src/integration/Setup/SetupScript.php @@ -14,6 +14,7 @@ use Flarum\Install\AdminUser; use Flarum\Install\BaseUrl; use Flarum\Install\DatabaseConfig; use Flarum\Install\Installation; +use Flarum\Install\Steps\ConnectToDatabase; use Flarum\Testing\integration\UsesTmpDir; class SetupScript @@ -62,6 +63,11 @@ class SetupScript */ protected $pref; + /** + * @var DatabaseConfig + */ + private $dbConfig; + public function __construct() { $this->host = getenv('DB_HOST') ?: 'localhost'; @@ -77,6 +83,7 @@ class SetupScript $tmp = $this->tmpDir(); echo "Connecting to database $this->name at $this->host:$this->port.\n"; + echo "Warning: all tables will be dropped to ensure clean state. DO NOT use your production database!\n"; echo "Logging in as $this->user with password '$this->pass'.\n"; echo "Table prefix: '$this->pref'\n"; echo "\nStoring test config in '$tmp'\n"; @@ -91,6 +98,12 @@ class SetupScript echo "\nOff we go...\n"; + $this->dbConfig = new DatabaseConfig('mysql', $this->host, $this->port, $this->name, $this->user, $this->pass, $this->pref); + + echo "\nWiping DB to ensure clean state\n"; + $this->wipeDb(); + echo "Success! Proceeding to installation...\n"; + $this->setupTmpDir(); $installation = new Installation( @@ -98,7 +111,7 @@ class SetupScript 'base' => $tmp, 'public' => "$tmp/public", 'storage' => "$tmp/storage", - 'vendor' => getcwd().'/vendor', + 'vendor' => getcwd() . '/vendor', ]) ); @@ -106,9 +119,7 @@ class SetupScript ->configPath('config.php') ->debugMode(true) ->baseUrl(BaseUrl::fromString('http://localhost')) - ->databaseConfig( - new DatabaseConfig('mysql', $this->host, $this->port, $this->name, $this->user, $this->pass, $this->pref) - ) + ->databaseConfig($this->dbConfig) ->adminUser(new AdminUser( 'admin', 'password', @@ -123,4 +134,16 @@ class SetupScript echo "Installation complete\n"; } + + protected function wipeDb() + { + // Reuse the connection step to include version checks + (new ConnectToDatabase($this->dbConfig, function ($db) { + // Inspired by Laravel's db:wipe + $builder = $db->getSchemaBuilder(); + + $builder->dropAllTables(); + $builder->dropAllViews(); + }))->run(); + } }