Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference test table directly in label2test table #2390

Merged
merged 1 commit into from
Aug 27, 2024
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
1 change: 0 additions & 1 deletion app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ public function upgrade()

self::delete_unused_rows('dailyupdatefile', 'dailyupdateid', 'dailyupdate');
self::delete_unused_rows('test2image', 'outputid', 'testoutput');
self::delete_unused_rows('label2test', 'outputid', 'testoutput');

$xml .= add_XML_value('alert', 'Database cleanup complete.');
}
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @property int $id
Expand All @@ -25,4 +26,12 @@ class Label extends Model
protected $casts = [
'id' => 'integer',
];

/**
* @return BelongsToMany<Test>
*/
public function tests(): BelongsToMany
{
return $this->belongsToMany(Test::class, 'label2test', 'labelid', 'testid');
}
}
34 changes: 22 additions & 12 deletions app/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Config;

Expand All @@ -32,6 +33,10 @@ class Test extends Model
public $timestamps = false;

protected $table = 'build2test';

/**
* @deprecated 08/24/2024 This member variable is deprecated. Use the labels() Eloquent relationship instead.
*/
protected $labels = null;

// TODO: Put these in an enum somewhere
Expand Down Expand Up @@ -94,6 +99,14 @@ public function testMeasurements(): HasMany
return $this->hasMany(TestMeasurement::class, 'testid');
}

/**
* @return BelongsToMany<\App\Models\Label>
*/
public function labels(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Label::class, 'label2test', 'testid', 'labelid');
}

/**
* Add a label to this buildtest.
**/
Expand All @@ -107,20 +120,17 @@ public function addLabel(Label $label): void

/**
* Get the collection of labels for this buildtest.
*
* @deprecated 08/24/2024 The legacy Label class is deprecated. Use the labels() Eloquent relationship instead.
**/
public function getLabels()
{
if (is_null($this->labels)) {
$this->labels = collect();
$testlabel_models = TestLabel::where([
['buildid', '=', $this->buildid],
['outputid', '=', $this->outputid],
])->get();
foreach ($testlabel_models as $testlabel_model) {
foreach ($this->labels()->get() as $eloquent_label) {
$label = new Label();
$label->Id = $testlabel_model->labelid;
$text = $label->GetText();
$this->labels->put($text, $label);
$label->Id = $eloquent_label->id;
$this->labels->put($eloquent_label->text, $label);
}
}
return $this->labels;
Expand Down Expand Up @@ -210,10 +220,10 @@ public static function marshal($data, $buildid, $projectid, $projectshowtesttime
}
}

if (config('database.default') == 'pgsql' && $marshaledData['buildtestid']) {
$buildtest = Test::where('id', '=', $data['buildtestid'])->first();
if ($buildtest) {
$marshaledData['labels'] = $buildtest->getLabels()->keys()->all();
if ($marshaledData['buildtestid'] ?? false) {
$test = Test::find((int) $data['buildtestid']);
if ($test !== null) {
$marshaledData['labels'] = $test->labels()->get(['text']);
}
} else {
if (!empty($data['labels'])) {
Expand Down
17 changes: 0 additions & 17 deletions app/Models/TestLabel.php

This file was deleted.

3 changes: 1 addition & 2 deletions app/Utils/TestCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public function create(Build $build): void
$build->AddTest($buildtest);

foreach ($this->labels as $label) {
$label->TestId = $outputid;
$label->TestBuildId = (int) $build->Id;
$label->Test = $buildtest;
$label->Insert();
$buildtest->addLabel($label);
}
Expand Down
5 changes: 1 addition & 4 deletions app/cdash/app/Controller/Api/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,7 @@ public function generateBuildResponseFromRow(array $build_array): array|false
b2t.status,
b2t.newstatus
FROM build2test AS b2t
INNER JOIN label2test AS l2t ON (
l2t.outputid=b2t.outputid
AND l2t.buildid=b2t.buildid
)
INNER JOIN label2test AS l2t ON l2t.testid = b2t.id
WHERE
b2t.buildid = ?
AND l2t.labelid IN $placeholders
Expand Down
3 changes: 1 addition & 2 deletions app/cdash/app/Controller/Api/QueryTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ public function getResponse()
label2test
WHERE
label.id=label2test.labelid
AND label2test.outputid=build2test.outputid
AND label2test.buildid=b.id
AND label2test.testid=build2test.id
) AS labelstring
$output_select
FROM build AS b
Expand Down
4 changes: 2 additions & 2 deletions app/cdash/app/Controller/Api/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function getResponse()
$groupby_sql = '';
if ($this->project->DisplayLabels && config('database.default') != 'pgsql') {
$labeljoin_sql = '
LEFT JOIN label2test AS l2t ON (l2t.outputid=bt.outputid)
LEFT JOIN label2test AS l2t ON (l2t.testid=bt.id)
LEFT JOIN label AS l ON (l.id=l2t.labelid)';
$label_sql = ", GROUP_CONCAT(DISTINCT l.text SEPARATOR ', ') AS labels";
$groupby_sql = ' GROUP BY bt.id';
Expand Down Expand Up @@ -392,7 +392,7 @@ public function getResponse()
$numTimeFailed++;
}

$labels_found = (config('database.default') != 'pgsql' && !empty($marshaledTest['labels']));
$labels_found = !empty($marshaledTest['labels']);

$marshaledTest['measurements'] = $test_measurements[$marshaledTest['buildtestid']];
if ($response['hasprocessors']) {
Expand Down
9 changes: 6 additions & 3 deletions app/cdash/app/Model/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,9 +1634,12 @@ public function GetLabels($labelarray = []): array|false

if (empty($labelarray) || isset($labelarray['test']['errors'])) {
$sql .=
' OR label.id IN
(SELECT labelid AS id FROM label2test
WHERE label2test.buildid = :buildid)';
' OR label.id IN (
SELECT labelid AS id
FROM label2test
INNER JOIN build2test ON build2test.id = label2test.testid
WHERE build2test.buildid = :buildid
)';
}
if (empty($labelarray) || isset($labelarray['coverage']['errors'])) {
$sql .=
Expand Down
6 changes: 3 additions & 3 deletions app/cdash/app/Model/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
=========================================================================*/
namespace CDash\Model;

use App\Models\Test;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\Label as EloquentLabel;
Expand All @@ -30,8 +31,7 @@ class Label
public $CoverageFileId;
public int $CoverageFileBuildId = 0;
public $DynamicAnalysisId;
public $TestId;
public int $TestBuildId = 0;
public ?Test $Test = null;

public function SetText(?string $text): void
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public function Insert()

$this->InsertAssociation('label2dynamicanalysis', 'dynamicanalysisid', intval($this->DynamicAnalysisId));

$this->InsertAssociation('label2test', 'buildid', $this->TestBuildId, 'outputid', intval($this->TestId));
$this->Test?->labels()->syncWithoutDetaching([$this->Id]);

// TODO: Implement this:
//
Expand Down
5 changes: 3 additions & 2 deletions app/cdash/app/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,10 @@ public function GetLabels($days): array|false
AND build.starttime>?
) UNION (
SELECT labelid AS id
FROM label2test, build
FROM label2test, build, build2test
WHERE
label2test.buildid=build.id
build2test.buildid=build.id
AND build2test.id=label2test.testid
AND build.projectid=?
AND build.starttime>?
) UNION (
Expand Down
4 changes: 2 additions & 2 deletions app/cdash/include/filterdataFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function getSqlField($field)
break;

case 'label': {
$sql_field = "(SELECT $this->TextConcat FROM label, label2test WHERE label2test.outputid = build2test.outputid AND label2test.labelid = label.id AND label2test.buildid = b.id)";
$sql_field = "(SELECT $this->TextConcat FROM label, label2test WHERE label2test.testid = build2test.id AND label2test.labelid = label.id)";
}
break;

Expand Down Expand Up @@ -593,7 +593,7 @@ public function getSqlField($field)
break;

case 'label': {
$sql_field = "(SELECT $this->TextConcat FROM label, label2test WHERE label.id=label2test.labelid AND label2test.outputid=bt.outputid)";
$sql_field = "(SELECT $this->TextConcat FROM label, label2test WHERE label.id=label2test.labelid AND label2test.testid=bt.id)";
}
break;

Expand Down
9 changes: 4 additions & 5 deletions app/cdash/tests/test_multiplesubprojects.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,11 @@ public function testMultipleSubprojects()

// Adding tests to ensure that labels associated with subprojects and tests were saved
$sql = "
SELECT text
SELECT label.text
FROM label2test
JOIN label
ON
id=labelid
WHERE buildid=:buildid;
INNER JOIN label ON label.id=label2test.labelid
INNER JOIN build2test ON build2test.id = label2test.testid
WHERE build2test.buildid=:buildid;
";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':buildid', $build['id'], PDO::PARAM_INT);
Expand Down
11 changes: 4 additions & 7 deletions app/cdash/tests/test_querytests.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,23 @@ public function testQueryTests()
// Make sure cases with more than one label are handled appropriately.
$query_result = DB::select("
SELECT
t.id AS outputid,
l2t.buildid AS buildid
l2t.testid AS testid
FROM
label l,
label2test l2t,
testoutput t
label2test l2t
WHERE
l.id = l2t.labelid
AND l2t.outputid = t.id
AND l.text = 'Claps'
LIMIT 1
")[0];
DB::insert("INSERT INTO label (text) VALUES ('TestLabel')");
$labelid = (int) DB::select("SELECT id FROM label WHERE text = 'TestLabel'")[0]->id;
DB::insert('INSERT INTO label2test (labelid, buildid, outputid) VALUES (?, ?, ?)', [$labelid, $query_result->buildid, $query_result->outputid]);
DB::insert('INSERT INTO label2test (labelid, testid) VALUES (?, ?)', [$labelid, $query_result->testid]);
$this->get($this->url . '/api/v1/queryTests.php?project=Trilinos&filtercount=1&showfilters=1&field1=label&compare1=63&value1=Claps');
$content = $this->getBrowser()->getContent();
$jsonobj = json_decode($content, true);
$this->assertEqual($jsonobj['builds'][0]['labels'], 'Claps, TestLabel');
DB::insert('DELETE FROM label2test WHERE labelid = ? AND buildid = ? AND outputid = ?', [$labelid, $query_result->buildid, $query_result->outputid]);
DB::insert('DELETE FROM label2test WHERE labelid = ? AND testid = ?', [$labelid, $query_result->testid]);
DB::delete("DELETE FROM label WHERE text = 'TestLabel'");


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('label2test', function (Blueprint $table) {
$table->dropPrimary();
$table->dropUnique(['outputid', 'buildid', 'labelid']);
$table->unsignedInteger('testid')
->nullable();
});

if (config('database.default') === 'pgsql') {
DB::update('
UPDATE label2test
SET testid = build2test.id
FROM build2test
WHERE
build2test.buildid = label2test.buildid
AND build2test.outputid = label2test.outputid
');
} else {
DB::update('
UPDATE label2test, build2test
SET label2test.testid = build2test.id
WHERE
build2test.buildid = label2test.buildid
AND build2test.outputid = label2test.outputid
');
}

$rows_deleted = DB::delete('DELETE FROM label2test WHERE testid IS NULL');
if ($rows_deleted > 0) {
echo "Deleted $rows_deleted invalid rows from label2test.";
}

Schema::table('label2test', function (Blueprint $table) {
$table->dropForeign(['buildid']);
$table->dropColumn(['buildid', 'outputid']);
$table->unsignedInteger('testid')
->nullable(false)
->change();
$table->foreign('testid')
->references('id')
->on('build2test')
->cascadeOnDelete();
$table->foreign('labelid')
->references('id')
->on('label')
->cascadeOnDelete();
$table->unique(['labelid', 'testid']);
$table->unique(['testid', 'labelid']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('label2test', function (Blueprint $table) {
$table->integer('buildid')
->nullable();
$table->bigInteger('outputid')
->nullable();
});

if (config('database.default') === 'pgsql') {
DB::update('
UPDATE label2test
SET
buildid = build2test.buildid,
outputid = build2test.outputid
FROM build2test
WHERE build2test.id = label2test.testid
');
} else {
DB::update('
UPDATE label2test, build2test
SET
label2test.buildid = build2test.buildid,
label2test.outputid = build2test.outputid
WHERE build2test.id = label2test.testid
');
}

Schema::table('label2test', function (Blueprint $table) {
$table->dropForeign(['testid']);
$table->dropColumn('testid');
$table->primary(['labelid', 'buildid', 'outputid']);
$table->dropForeign(['labelid']);
});
}
};
Loading