From fc705dad2ef1e4accc847e5b702ce4f9455dcfc8 Mon Sep 17 00:00:00 2001 From: William Allen <16820599+williamjallen@users.noreply.github.com> Date: Tue, 15 Aug 2023 13:45:18 -0400 Subject: [PATCH] Migrate `CoverageSummaryDiff` model to Eloquent --- app/Models/CoverageSummaryDiff.php | 28 +++++++++ app/cdash/app/Model/CoverageSummary.php | 17 +++--- app/cdash/app/Model/CoverageSummaryDiff.php | 68 --------------------- phpstan-baseline.neon | 49 ++------------- 4 files changed, 42 insertions(+), 120 deletions(-) create mode 100644 app/Models/CoverageSummaryDiff.php delete mode 100644 app/cdash/app/Model/CoverageSummaryDiff.php diff --git a/app/Models/CoverageSummaryDiff.php b/app/Models/CoverageSummaryDiff.php new file mode 100644 index 0000000000..6056899407 --- /dev/null +++ b/app/Models/CoverageSummaryDiff.php @@ -0,0 +1,28 @@ + + */ +class CoverageSummaryDiff extends Model +{ + protected $table = 'coveragesummarydiff'; + + public $timestamps = false; + + protected $primaryKey = 'buildid'; + + protected $fillable = [ + 'buildid', + 'loctested', + 'locuntested', + ]; +} diff --git a/app/cdash/app/Model/CoverageSummary.php b/app/cdash/app/Model/CoverageSummary.php index d465c9395f..2fd960e9d0 100644 --- a/app/cdash/app/Model/CoverageSummary.php +++ b/app/cdash/app/Model/CoverageSummary.php @@ -15,6 +15,7 @@ =========================================================================*/ namespace CDash\Model; +use App\Models\CoverageSummaryDiff; use CDash\Database; use Illuminate\Support\Facades\DB; @@ -326,17 +327,17 @@ public function ComputeDifference($previous_parentid=null): bool $previousloctested = intval($previouscoverage['loctested']); $previouslocuntested = intval($previouscoverage['locuntested']); - $summaryDiff = new CoverageSummaryDiff(); - $summaryDiff->BuildId = $this->BuildId; $loctesteddiff = $loctested - $previousloctested; $locuntesteddiff = $locuntested - $previouslocuntested; - // Don't log if no diff unless an entry already exists - // for this build. - if ($summaryDiff->Exists() || $loctesteddiff !== 0 || $locuntesteddiff !== 0) { - $summaryDiff->LocTested = $loctesteddiff; - $summaryDiff->LocUntested = $locuntesteddiff; - $summaryDiff->Insert(); + // Don't log if no diff unless an entry already exists for this build. + if (CoverageSummaryDiff::where(['buildid' => $this->BuildId])->exists() || $loctesteddiff !== 0 || $locuntesteddiff !== 0) { + CoverageSummaryDiff::updateOrCreate([ + 'buildid' => $this->BuildId, + ], [ + 'loctested' => $loctesteddiff, + 'locuntested' => $locuntesteddiff, + ]); } } diff --git a/app/cdash/app/Model/CoverageSummaryDiff.php b/app/cdash/app/Model/CoverageSummaryDiff.php deleted file mode 100644 index 6d953a6940..0000000000 --- a/app/cdash/app/Model/CoverageSummaryDiff.php +++ /dev/null @@ -1,68 +0,0 @@ -executePreparedSingleRow(' - SELECT COUNT(1) AS c - FROM coveragesummarydiff - WHERE buildid=? - ', [intval($this->BuildId)]); - - if (intval($row['c']) > 0) { - // UPDATE instead of INSERT if a row already exists. - $db->executePrepared(' - UPDATE coveragesummarydiff - SET loctested=?, locuntested=? - WHERE buildid=? - ', [intval($this->LocTested), intval($this->LocUntested), intval($this->BuildId)]); - } else { - $db->executePrepared(' - INSERT INTO coveragesummarydiff (buildid, loctested, locuntested) - VALUES (?, ?, ?) - ', [intval($this->BuildId), intval($this->LocTested), intval($this->LocUntested)]); - } - add_last_sql_error('CoverageSummary:ComputeDifference'); - } - - /** Return whether or not a CoverageSummaryDiff exists for this build. */ - public function Exists(): bool - { - if (!$this->BuildId) { - return false; - } - - $db = Database::getInstance(); - $exists_result = $db->executePreparedSingleRow(' - SELECT COUNT(1) AS e - FROM coveragesummarydiff - WHERE buildid=? - ', [intval($this->BuildId)]); - - return !empty($exists_result); - } -} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fdd9913d5d..4c392fbcc8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6950,6 +6950,11 @@ parameters: count: 11 path: app/cdash/app/Model/CoverageSummary.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\\\:\\:exists\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Model/CoverageSummary.php + - message: "#^Method CDash\\\\Model\\\\CoverageSummary\\:\\:AddCoverage\\(\\) has parameter \\$coverage with no type specified\\.$#" count: 1 @@ -7035,50 +7040,6 @@ parameters: count: 1 path: app/cdash/app/Model/CoverageSummary.php - - - message: """ - #^Call to deprecated function add_last_sql_error\\(\\)\\: - 04/22/2023$# - """ - count: 1 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: """ - #^Call to deprecated method executePrepared\\(\\) of class CDash\\\\Database\\: - 04/22/2023 Use Laravel query builder or Eloquent instead$# - """ - count: 2 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: """ - #^Call to deprecated method executePreparedSingleRow\\(\\) of class CDash\\\\Database\\: - 04/22/2023 Use Laravel query builder or Eloquent instead$# - """ - count: 2 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 1 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: "#^Property CDash\\\\Model\\\\CoverageSummaryDiff\\:\\:\\$BuildId has no type specified\\.$#" - count: 1 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: "#^Property CDash\\\\Model\\\\CoverageSummaryDiff\\:\\:\\$LocTested has no type specified\\.$#" - count: 1 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - - - message: "#^Property CDash\\\\Model\\\\CoverageSummaryDiff\\:\\:\\$LocUntested has no type specified\\.$#" - count: 1 - path: app/cdash/app/Model/CoverageSummaryDiff.php - - message: """ #^Call to deprecated function add_last_sql_error\\(\\)\\: