Skip to content

Commit 99d2200

Browse files
authored
feat: add backend endpoint to set wiki DB version (#1012)
successor of #1010 Adds the backend endpoint `/backend/setWikiDbVersion` as defined in https://phabricator.wikimedia.org/T410394
1 parent 2156e09 commit 99d2200

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Backend;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Wiki;
7+
use Illuminate\Http\Request;
8+
9+
class WikiDbVersionController extends Controller {
10+
// keep in sync with App\Services\MediaWikiHostResolver
11+
private const DB_VERSION_TO_MW_VERSION = [
12+
'mw1.39-wbs1' => '139',
13+
'mw1.43-wbs1' => '143',
14+
];
15+
16+
public function updateWikiDbVersion(Request $request): \Illuminate\Http\JsonResponse {
17+
$validated = $request->validate([
18+
'domain' => 'required|string',
19+
'dbVersion' => 'required|string',
20+
]);
21+
22+
$domain = $validated['domain'];
23+
$targetDbVersion = $validated['dbVersion'];
24+
25+
try {
26+
$wiki = Wiki::with('wikiDb')->firstWhere('domain', $domain);
27+
28+
if (!$wiki) {
29+
return response()->json(['error' => "No wiki found with domain: '{$domain}'"], 404);
30+
}
31+
32+
if (!array_key_exists($targetDbVersion, self::DB_VERSION_TO_MW_VERSION)) {
33+
return response()->json(['error' => "Invalid database version string: '{$targetDbVersion}'"], 400);
34+
}
35+
36+
$wiki->wikiDb->version = $targetDbVersion;
37+
$wiki->wikiDb->save();
38+
} catch (\Exception $e) {
39+
return response()->json($e->getMessage(), 500);
40+
}
41+
42+
return response()->json(['result' => 'success'], 200);
43+
}
44+
}

app/Services/MediaWikiHostResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class UnknownWikiDomainException extends Exception {}
1717

1818
class MediaWikiHostResolver {
1919
// TODO: Move this mapping to a config file so that MW updates do not require code changes here.
20+
// keep in sync with App\Http\Controllers\Backend\WikiDbVersionController
2021
/** @var array<string, string> Map of DB version strings to MediaWiki version strings */
2122
private const DB_VERSION_TO_MW_VERSION = [
2223
'mw1.39-wbs1' => '139',

routes/backend.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
$router->get('healthz', fn () => "It's Alive");
1212
$router->get('getWikiHostsForDomain', ['uses' => 'MediaWikiHostsController@getWikiHostsForDomain']);
1313

14+
// PUT
15+
$router->put('setWikiDbVersion', ['uses' => 'WikiDbVersionController@updateWikiDbVersion']);
16+
1417
$router->group(['prefix' => 'ingress'], function () use ($router) {
1518
// GET
1619
$router->get('getWikiVersionForDomain', ['uses' => 'IngressController@getWikiVersionForDomain']);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Tests\Routes\Backend;
4+
5+
use App\Wiki;
6+
use App\WikiDb;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class WikiDbVersionControllerTest extends TestCase {
11+
use RefreshDatabase;
12+
13+
const VALID_WIKI_DB_VERSION_STRING_139 = 'mw1.39-wbs1';
14+
15+
const VALID_WIKI_DB_VERSION_STRING_143 = 'mw1.43-wbs1';
16+
17+
protected $route = '/backend/setWikiDbVersion';
18+
19+
private function createWiki(string $domain, string $version) {
20+
$wiki = Wiki::factory()->create(['domain' => $domain]);
21+
WikiDb::create([
22+
'name' => $domain,
23+
'user' => 'someUser',
24+
'password' => 'somePassword',
25+
'version' => $version,
26+
'prefix' => 'somePrefix',
27+
'wiki_id' => $wiki->id,
28+
]);
29+
30+
return $wiki;
31+
}
32+
33+
public function testSetWikiDbVersionSuccess() {
34+
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
35+
$wikiDomain = 'coffeebase.wikibase.cloud';
36+
37+
$wiki = $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);
38+
39+
$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
40+
->assertStatus(200)
41+
->assertJson([
42+
'result' => 'success',
43+
]);
44+
45+
$newDbVersion = Wiki::with('wikiDb')->firstWhere('id', $wiki->id)->wikiDb->version;
46+
47+
$this->assertSame($targetDbVersion, $newDbVersion);
48+
}
49+
50+
public function testSetWikiDbVersionWikiNotfound() {
51+
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
52+
$wikiDomain = 'notFound.wikibase.cloud';
53+
54+
$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
55+
->assertStatus(404);
56+
}
57+
58+
public function testSetWikiDbVersionUnknownDbVersion() {
59+
$targetDbVersion = 'unknownVersion';
60+
$wikiDomain = 'coffeebase.wikibase.cloud';
61+
62+
$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);
63+
64+
$this->putJson("{$this->route}?domain={$wikiDomain}&dbVersion={$targetDbVersion}")
65+
->assertStatus(400);
66+
}
67+
68+
public function testSetWikiDbVersionMissingDbVersion() {
69+
$wikiDomain = 'coffeebase.wikibase.cloud';
70+
71+
$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);
72+
73+
$this->putJson("{$this->route}?domain={$wikiDomain}")
74+
->assertStatus(422);
75+
}
76+
77+
public function testSetWikiDbVersionMissingWikiDomain() {
78+
$targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143;
79+
$wikiDomain = 'coffeebase.wikibase.cloud';
80+
81+
$this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139);
82+
83+
$this->putJson("{$this->route}?dbVersion={$targetDbVersion}")
84+
->assertStatus(422);
85+
}
86+
}

0 commit comments

Comments
 (0)