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

Use postgresql recursive feature for getting scheduled product #5398

Merged
merged 1 commit into from
Dec 15, 2023
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
13 changes: 12 additions & 1 deletion lib/OpenQA/Schema/Result/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,18 @@ sub cancel_whole_clone_chain ($self, @args) {
# returns the related scheduled product; if the job has not been created by one the origin job is checked
sub related_scheduled_product_id ($self) {
if (my $sp_id = $self->scheduled_product_id) { return $sp_id }
if (my $origin = $self->origin) { return $origin->related_scheduled_product_id }
return $self->{_orig_scheduled_product_id} if exists $self->{_orig_scheduled_product_id};
my $sth = $self->result_source->schema->storage->dbh->prepare(<<~'EOM');
with recursive orig_id as (
select ? as orig_id, ? as prod
union all
select id as orig_id, scheduled_product_id as prod from jobs join orig_id on orig_id.orig_id = jobs.clone_id)
select prod from orig_id where prod is not null
EOM
$sth->bind_param(1, $self->id, SQL_BIGINT);
$sth->bind_param(2, undef, SQL_BIGINT);
$sth->execute;
$self->{_orig_scheduled_product_id} = $sth->fetchrow_array;
}

=head2 done
Expand Down
12 changes: 12 additions & 0 deletions t/09-job_clone.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ my $testsuites = $schema->resultset("TestSuites");
my $jobs = $schema->resultset("Jobs");

my $rset = $t->app->schema->resultset("Jobs");

subtest 'multiple clones' => sub {
$schema->txn_begin;
my $orig = $rset->find(99946);
my $sp = $schema->resultset('ScheduledProducts')->create({id => 23, settings => []});
$orig->update({scheduled_product_id => $sp->id});
my $last = $rset->find(99946);
my $product_id = $last->related_scheduled_product_id;
is $product_id, 23, 'Found original scheduled_product_id of clone';
$schema->txn_rollback;
};

my $minimalx = $rset->find(99926);
my $clones = $minimalx->duplicate();
my $clone = $rset->find($clones->{$minimalx->id}->{clone});
Expand Down
Loading