Skip to content

Commit 50b8840

Browse files
authored
Store and check update requirement state (#31)
1 parent f074ed5 commit 50b8840

File tree

10 files changed

+80
-11
lines changed

10 files changed

+80
-11
lines changed

app/Console/Commands/PerformUpdate.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public function handle(): int
3535
/** @var Site $site */
3636
$site = Site::findOrFail($this->input->getArgument('siteId'));
3737

38+
if (!$site->update_requirement_state) {
39+
$this->output->writeln('Update requirements not met, aborting!');
40+
41+
return Command::FAILURE;
42+
}
43+
3844
UpdateSite::dispatchSync(
3945
$site,
4046
$targetVersion

app/Console/Commands/QueueUpdates.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function handle(): int
3939
$this->output->writeln('Pushing update jobs');
4040

4141
$sites = Site::query()
42+
->where('update_requirement_state', '=', true)
4243
->where(
4344
'cms_version',
4445
'like',

app/Jobs/CheckSiteHealth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function handle(): void
4242
$this->site->last_seen = Carbon::now();
4343
$this->site->save();
4444

45+
// Check if conditions are met
46+
if (!$this->site->update_requirement_state) {
47+
return;
48+
}
49+
4550
// Check if a newer Joomla version for that site is available
4651
/** @var TufFetcher $tufFetcher */
4752
$tufFetcher = App::make(TufFetcher::class);

app/Jobs/UpdateSite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function handle(): void
5151
return;
5252
}
5353

54+
if (!$healthResult->update_requirement_state) {
55+
Log::info("Site does not meet requirements, abort");
56+
57+
return;
58+
}
59+
5460
// Do not make a major version update
5561
$majorVersionCms = (int) $healthResult->cms_version;
5662
$majorTargetVersion = (int) $this->targetVersion;

app/Models/Site.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class Site extends Model
2020
'db_version',
2121
'cms_version',
2222
'server_os',
23-
'update_patch',
24-
'update_minor',
25-
'update_major'
23+
'update_requirement_state'
2624
];
2725

2826
protected $hidden = [
@@ -33,9 +31,7 @@ protected function casts(): array
3331
{
3432
return [
3533
'last_seen' => 'datetime',
36-
'update_patch' => 'bool',
37-
'update_minor' => 'bool',
38-
'update_major' => 'bool'
34+
'update_requirement_state' => 'bool',
3935
];
4036
}
4137

app/RemoteSite/Responses/HealthCheck.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function __construct(
1111
public string $db_type,
1212
public string $db_version,
1313
public string $cms_version,
14-
public string $server_os
14+
public string $server_os,
15+
public ?bool $update_requirement_state = null
1516
) {
1617
}
1718
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class () extends Migration {
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up(): void
12+
{
13+
Schema::table('sites', function (Blueprint $table) {
14+
$table->boolean('update_requirement_state')->default(false)->nullable(true);
15+
});
16+
}
17+
18+
/**
19+
* Reverse the migrations.
20+
*/
21+
public function down(): void
22+
{
23+
Schema::table('sites', function (Blueprint $table) {
24+
$table->dropColumn('update_requirement_state');
25+
});
26+
}
27+
};

tests/Feature/Api/SiteControllerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
3737
"db_type" => "mysqli",
3838
"db_version" => "1.0.0",
3939
"cms_version" => "1.0.0",
40-
"server_os" => "Joomla OS 1.0.0"
40+
"server_os" => "Joomla OS 1.0.0",
41+
"update_requirement_state" => true
4142
]));
4243

4344
$this->app->bind(Connection::class, fn () => $mock);
@@ -170,7 +171,8 @@ protected function createMockSiteInDb(): Site
170171
"db_version" => "1.0.0",
171172
"cms_version" => "1.0.0",
172173
"server_os" => "Joomla OS 1.0.0",
173-
"last_seen" => Carbon::now()
174+
"last_seen" => Carbon::now(),
175+
"update_requirement_state" => true
174176
]);
175177

176178
$site->key = 'foobar123foobar123foobar123foobar123';

tests/Unit/Jobs/CheckSiteHealthTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function () use ($healthMock) {
8484
$siteMock->id = 1;
8585
$siteMock->url = "http://example.org";
8686
$siteMock->cms_version = "1.0.0";
87+
$siteMock->update_requirement_state = true;
8788

8889
return $siteMock;
8990
}
@@ -95,7 +96,8 @@ protected function getHealthCheckMock($overrides = [])
9596
"db_type" => "mysqli",
9697
"db_version" => "1.0.0",
9798
"cms_version" => "1.0.0",
98-
"server_os" => "Joomla OS 1.0.0"
99+
"server_os" => "Joomla OS 1.0.0",
100+
"update_requirement_state" => true
99101
];
100102

101103
return HealthCheck::from([

tests/Unit/Jobs/UpdateSiteTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ public function testJobQuitsIfNoUpdateIsAvailable()
5959
$this->assertTrue(true);
6060
}
6161

62+
public function testJobQuitsIfRequirementsArentMet()
63+
{
64+
$site = $this->getSiteMock(
65+
[
66+
'checkHealth' => $this->getHealthCheckMock(['update_requirement_state' => false])
67+
]
68+
);
69+
70+
Log::spy();
71+
72+
$object = new UpdateSite($site, "1.0.1");
73+
$object->handle();
74+
75+
Log::shouldHaveReceived('info')
76+
->once()
77+
->withArgs(function ($message) {
78+
return str_contains($message, 'Site does not meet requirements, abort');
79+
});
80+
81+
$this->assertTrue(true);
82+
}
83+
6284
public function testJobQuitsIfWeDetectALoopForAVersion()
6385
{
6486
$site = $this->getSiteMock([], null, 6);
@@ -241,7 +263,8 @@ protected function getHealthCheckMock($overrides = [])
241263
"db_type" => "mysqli",
242264
"db_version" => "1.0.0",
243265
"cms_version" => "1.0.0",
244-
"server_os" => "Joomla OS 1.0.0"
266+
"server_os" => "Joomla OS 1.0.0",
267+
"update_requirement_state" => true
245268
];
246269

247270
return HealthCheck::from([

0 commit comments

Comments
 (0)