diff --git a/app/Http/Controllers/Backend/WikiDbVersionController.php b/app/Http/Controllers/Backend/WikiDbVersionController.php new file mode 100644 index 00000000..3ecf214b --- /dev/null +++ b/app/Http/Controllers/Backend/WikiDbVersionController.php @@ -0,0 +1,44 @@ + '139', + 'mw1.43-wbs1' => '143', + ]; + + public function updateWikiDbVersion(Request $request): \Illuminate\Http\JsonResponse { + $validated = $request->validate([ + 'domain' => 'required|string', + '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); + } +} diff --git a/app/Services/MediaWikiHostResolver.php b/app/Services/MediaWikiHostResolver.php index a07e6f9a..342ec737 100644 --- a/app/Services/MediaWikiHostResolver.php +++ b/app/Services/MediaWikiHostResolver.php @@ -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 Map of DB version strings to MediaWiki version strings */ private const DB_VERSION_TO_MW_VERSION = [ 'mw1.39-wbs1' => '139', diff --git a/routes/backend.php b/routes/backend.php index 478a4036..0be47914 100644 --- a/routes/backend.php +++ b/routes/backend.php @@ -11,6 +11,9 @@ $router->get('healthz', fn () => "It's Alive"); $router->get('getWikiHostsForDomain', ['uses' => 'MediaWikiHostsController@getWikiHostsForDomain']); +// PUT +$router->put('setWikiDbVersion', ['uses' => 'WikiDbVersionController@updateWikiDbVersion']); + $router->group(['prefix' => 'ingress'], function () use ($router) { // GET $router->get('getWikiVersionForDomain', ['uses' => 'IngressController@getWikiVersionForDomain']); diff --git a/tests/Routes/Backend/WikiDbVersionControllerTest.php b/tests/Routes/Backend/WikiDbVersionControllerTest.php new file mode 100644 index 00000000..6be65e96 --- /dev/null +++ b/tests/Routes/Backend/WikiDbVersionControllerTest.php @@ -0,0 +1,86 @@ +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); + } +}