mirror of
https://github.com/flarum/core.git
synced 2025-07-23 09:41:26 +02:00
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.
This commit is contained in:
committed by
GitHub
parent
8fd1cc4f0d
commit
0569da23e1
@@ -14,6 +14,7 @@ use Flarum\Install\AdminUser;
|
|||||||
use Flarum\Install\BaseUrl;
|
use Flarum\Install\BaseUrl;
|
||||||
use Flarum\Install\DatabaseConfig;
|
use Flarum\Install\DatabaseConfig;
|
||||||
use Flarum\Install\Installation;
|
use Flarum\Install\Installation;
|
||||||
|
use Flarum\Install\Steps\ConnectToDatabase;
|
||||||
use Flarum\Testing\integration\UsesTmpDir;
|
use Flarum\Testing\integration\UsesTmpDir;
|
||||||
|
|
||||||
class SetupScript
|
class SetupScript
|
||||||
@@ -62,6 +63,11 @@ class SetupScript
|
|||||||
*/
|
*/
|
||||||
protected $pref;
|
protected $pref;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var DatabaseConfig
|
||||||
|
*/
|
||||||
|
private $dbConfig;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->host = getenv('DB_HOST') ?: 'localhost';
|
$this->host = getenv('DB_HOST') ?: 'localhost';
|
||||||
@@ -77,6 +83,7 @@ class SetupScript
|
|||||||
$tmp = $this->tmpDir();
|
$tmp = $this->tmpDir();
|
||||||
|
|
||||||
echo "Connecting to database $this->name at $this->host:$this->port.\n";
|
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 "Logging in as $this->user with password '$this->pass'.\n";
|
||||||
echo "Table prefix: '$this->pref'\n";
|
echo "Table prefix: '$this->pref'\n";
|
||||||
echo "\nStoring test config in '$tmp'\n";
|
echo "\nStoring test config in '$tmp'\n";
|
||||||
@@ -91,6 +98,12 @@ class SetupScript
|
|||||||
|
|
||||||
echo "\nOff we go...\n";
|
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();
|
$this->setupTmpDir();
|
||||||
|
|
||||||
$installation = new Installation(
|
$installation = new Installation(
|
||||||
@@ -98,7 +111,7 @@ class SetupScript
|
|||||||
'base' => $tmp,
|
'base' => $tmp,
|
||||||
'public' => "$tmp/public",
|
'public' => "$tmp/public",
|
||||||
'storage' => "$tmp/storage",
|
'storage' => "$tmp/storage",
|
||||||
'vendor' => getcwd().'/vendor',
|
'vendor' => getcwd() . '/vendor',
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -106,9 +119,7 @@ class SetupScript
|
|||||||
->configPath('config.php')
|
->configPath('config.php')
|
||||||
->debugMode(true)
|
->debugMode(true)
|
||||||
->baseUrl(BaseUrl::fromString('http://localhost'))
|
->baseUrl(BaseUrl::fromString('http://localhost'))
|
||||||
->databaseConfig(
|
->databaseConfig($this->dbConfig)
|
||||||
new DatabaseConfig('mysql', $this->host, $this->port, $this->name, $this->user, $this->pass, $this->pref)
|
|
||||||
)
|
|
||||||
->adminUser(new AdminUser(
|
->adminUser(new AdminUser(
|
||||||
'admin',
|
'admin',
|
||||||
'password',
|
'password',
|
||||||
@@ -123,4 +134,16 @@ class SetupScript
|
|||||||
|
|
||||||
echo "Installation complete\n";
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user