Skip to content
Merged
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
44 changes: 44 additions & 0 deletions app/Http/Controllers/Backend/WikiDbVersionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Wiki;
use Illuminate\Http\Request;

class WikiDbVersionController extends Controller {
// keep in sync with App\Services\MediaWikiHostResolver
private const DB_VERSION_TO_MW_VERSION = [
'mw1.39-wbs1' => '139',
'mw1.43-wbs1' => '143',
];

public function updateWikiDbVersion(Request $request): \Illuminate\Http\JsonResponse {
$validated = $request->validate([
'domain' => 'required|string',
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this light validation; I think we don't want to reuse the domain validation logic from Wiki creation because overtime it may become stricter and then requests here might start failing.

'dbVersion' => 'required|string',
]);

$domain = $validated['domain'];
$targetDbVersion = $validated['dbVersion'];

try {
$wiki = Wiki::with('wikiDb')->firstWhere('domain', $domain);

if (!$wiki) {
return response()->json(['error' => "No wiki found with domain: '{$domain}'"], 404);
}

if (!array_key_exists($targetDbVersion, self::DB_VERSION_TO_MW_VERSION)) {
return response()->json(['error' => "Invalid database version string: '{$targetDbVersion}'"], 400);
}

$wiki->wikiDb->version = $targetDbVersion;
$wiki->wikiDb->save();
} catch (\Exception $e) {
return response()->json($e->getMessage(), 500);
}

return response()->json(['result' => 'success'], 200);
}
}
1 change: 1 addition & 0 deletions app/Services/MediaWikiHostResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UnknownWikiDomainException extends Exception {}

class MediaWikiHostResolver {
// TODO: Move this mapping to a config file so that MW updates do not require code changes here.
// keep in sync with App\Http\Controllers\Backend\WikiDbVersionController
/** @var array<string, string> Map of DB version strings to MediaWiki version strings */
private const DB_VERSION_TO_MW_VERSION = [
'mw1.39-wbs1' => '139',
Expand Down
3 changes: 3 additions & 0 deletions routes/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
$router->get('healthz', fn () => "It's Alive");
$router->get('getWikiHostsForDomain', ['uses' => 'MediaWikiHostsController@getWikiHostsForDomain']);

// PUT
$router->put('setWikiDbVersion', ['uses' => 'WikiDbVersionController@updateWikiDbVersion']);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's good this isn't in wiki on second thoughts because it's actually about changing WikiDbs not Wikis


$router->group(['prefix' => 'ingress'], function () use ($router) {
// GET
$router->get('getWikiVersionForDomain', ['uses' => 'IngressController@getWikiVersionForDomain']);
Expand Down
86 changes: 86 additions & 0 deletions tests/Routes/Backend/WikiDbVersionControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Tests\Routes\Backend;

use App\Wiki;
use App\WikiDb;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WikiDbVersionControllerTest extends TestCase {
use RefreshDatabase;

const VALID_WIKI_DB_VERSION_STRING_139 = 'mw1.39-wbs1';

const VALID_WIKI_DB_VERSION_STRING_143 = 'mw1.43-wbs1';

protected $route = '/backend/setWikiDbVersion';

private function createWiki(string $domain, string $version) {
$wiki = Wiki::factory()->create(['domain' => $domain]);
WikiDb::create([
'name' => $domain,
'user' => 'someUser',
'password' => 'somePassword',
'version' => $version,
'prefix' => 'somePrefix',
'wiki_id' => $wiki->id,
]);

return $wiki;
}

public function testSetWikiDbVersionSuccess() {
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
$wikiDomain = 'coffeebase.wikibase.cloud';

$wiki = $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);

$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
->assertStatus(200)
->assertJson([
'result' => 'success',
]);

$newDbVersion = Wiki::with('wikiDb')->firstWhere('id', $wiki->id)->wikiDb->version;

$this->assertSame($targetDbVersion, $newDbVersion);
}

public function testSetWikiDbVersionWikiNotfound() {
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
$wikiDomain = 'notFound.wikibase.cloud';

$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
->assertStatus(404);
}

public function testSetWikiDbVersionUnknownDbVersion() {
$targetDbVersion = 'unknownVersion';
$wikiDomain = 'coffeebase.wikibase.cloud';

$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);

$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
->assertStatus(400);
}

public function testSetWikiDbVersionMissingDbVersion() {
$wikiDomain = 'coffeebase.wikibase.cloud';

$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);

$this->putJson("{$this->route}?domain={$wikiDomain}")
->assertStatus(422);
}

public function testSetWikiDbVersionMissingWikiDomain() {
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
$wikiDomain = 'coffeebase.wikibase.cloud';

$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);

$this->putJson("{$this->route}?dbVersion={$targetDbVersion}")
->assertStatus(422);
}
}
Loading