Skip to content

refactor: replace latest_checkout table primary key#1681

Merged
gustavobtflores merged 1 commit intokernelci:mainfrom
gustavobtflores:refactor/latest-checkout-model
Dec 16, 2025
Merged

refactor: replace latest_checkout table primary key#1681
gustavobtflores merged 1 commit intokernelci:mainfrom
gustavobtflores:refactor/latest-checkout-model

Conversation

@gustavobtflores
Copy link
Copy Markdown
Contributor

This PR modifies checkout_id in latest_checkout to not be a primary key and introduces a surrogate primary key called id as it is required by Django. This is done to avoid that the database throws an exception for duplicated primary key, we are already checking for duplicity with a UNIQUE constraint and the target is not the checkout_id field.

Reasoning

This happens because we do a upsert when inserting in latest_checkout table, we have a query like:

INSERT INTO latest_checkout (
    checkout_id, origin, tree_name,
    git_repository_url, git_repository_branch, start_time
)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT (origin, tree_name, git_repository_url, git_repository_branch)
DO UPDATE SET
    start_time = EXCLUDED.start_time,
    checkout_id = EXCLUDED.checkout_id
WHERE latest_checkout.start_time < EXCLUDED.start_time

If we try to update a checkout_id that is already in the database, we will get a duplicated primary key exception, where it should just do nothing. We cannot have multiple conflict targets in a single query, so the solution is to just remove it and ensure uniqueness with the UNIQUE constraint.

The UNIQUE constraint cannot be transformed in a composite primary key because it allows NULL values.

Closes #1680

@gustavobtflores gustavobtflores force-pushed the refactor/latest-checkout-model branch from 5f707cf to d31295d Compare December 16, 2025 19:27
@gustavobtflores gustavobtflores self-assigned this Dec 16, 2025
@gustavobtflores gustavobtflores added Backend Most or all of the changes for this issue will be in the backend code. Database Issue that alters only configs of a database itself labels Dec 16, 2025
@gustavobtflores gustavobtflores force-pushed the refactor/latest-checkout-model branch from d31295d to ae4b106 Compare December 16, 2025 19:43
Comment thread backend/kernelCI_app/models.py
@gustavobtflores gustavobtflores force-pushed the refactor/latest-checkout-model branch from ae4b106 to 857aec1 Compare December 16, 2025 20:11
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gustavobtflores gustavobtflores added this pull request to the merge queue Dec 16, 2025
Merged via the queue into kernelci:main with commit ac573c3 Dec 16, 2025
7 checks passed
@gustavobtflores gustavobtflores deleted the refactor/latest-checkout-model branch March 30, 2026 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backend Most or all of the changes for this issue will be in the backend code. Database Issue that alters only configs of a database itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modify checkout_id in latest_checkout to not be a primary key

2 participants