Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions app/Console/Commands/Wiki/DropDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Console\Commands\Wiki;

use App\Wiki;
use App\WikiDb;
use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\App;

class dropDatabase extends Command {
protected $signature = 'wbs-wiki:dropDatabase {wikiDomain}';

protected $description = 'Drops the MediaWiki database of a wiki and soft-deletes it.';

public function handle(): int {
$wikiDomain = trim($this->argument('wikiDomain'));
$wiki = Wiki::with('wikidb')->firstWhere('domain', $wikiDomain);

if (!$wiki) {
$this->error("Wiki not found by domain '$wikiDomain'");

return 1;
}

if (!$this->dropWikiDb($wiki->wikidb)) {
$this->error('Failed to drop the mediawiki database of wiki.');
$this->error($wiki);

return 2;
}

$this->info('MediaWiki database dropped.');

$wiki->delete();

$this->info('Wiki soft-deleted.');
$this->info($wiki);

return 0;
}

private function dropWikiDb(WikiDb $wikiDb): bool {
$connection = $this->getWikiDbConnection($wikiDb);

if (!$connection instanceof \Illuminate\Database\Connection) {
throw new \RuntimeException('Must be run on a PDO based DB connection');
}

$mediawikiPdo = $connection->getPdo();
$statement = $mediawikiPdo->prepare('DROP DATABASE ' . $wikiDb->name);

return $statement->execute([$wikiDb->name]);
}

// Creates a temporary DB connection with wiki scoped credentials
private function getWikiDbConnection(WikiDb $wikiDb): mixed {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that you managed to make this work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

me too - there seems to be quite the difference though between calling config() and app->config and i didn't find out what it is yet (i.e., with config() this didn't work iirc)

$wikiDbConnectionConfig = array_replace(
app()->config->get('database.connections.mw'),
[
'database' => $wikiDb->name,
'username' => $wikiDb->user,
'password' => $wikiDb->password,
]
);

app()->config->set('database.connections.wikiTemp', $wikiDbConnectionConfig);

return App::make(DatabaseManager::class)->connection('wikiTemp');
}
}
19 changes: 19 additions & 0 deletions tests/Commands/Wiki/DropDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Commands;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class DropDatabaseTest extends TestCase {
use DatabaseTransactions;

public function testWikiNotFound() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, integration testing this is almost always more hassle than it's worth

$this->artisan(
'wbs-wiki:dropDatabase', [
'wikiDomain' => 'imaginarywiki.wbaas.dev',
])
->assertExitCode(1)
->assertFailed();
}
}
Loading