Skip to content

Ideas to improve readability of auxtable code #5185

@theosanderson

Description

@theosanderson

If we were to keep the aux tables we should try to improve the readability of the code.

I spent a bit of time looking at https://github.com/loculus-project/loculus/blob/main/backend/src/main/kotlin/org/loculus/backend/service/submission/UploadDatabaseService.kt to try to think about what the options would be for that.

Perosnally I don't find the big SQL too horrendous to read but I acknowledge options will differ.

We should rename mapAndCopy to insertFromAuxTablesIntoMainTable or something.


We could create a VIEW in a migration, something like:

CREATE VIEW sequence_entries_staging AS
SELECT
    m.accession,
    m.version,
    m.organism,
    m.submission_id,
    m.submitter,
    m.group_id,
    m.uploaded_at AS submitted_at,
    m.upload_id,
    jsonb_build_object(
        'metadata', m.metadata,
        'files', m.files,
        'unalignedNucleotideSequences', 
        COALESCE(
            jsonb_object_agg(
                s.segment_name,
                s.compressed_sequence_data::jsonb
            ) FILTER (WHERE s.segment_name IS NOT NULL),
            '{}'::jsonb
        )
    ) AS original_data
FROM
    metadata_upload_aux_table m
LEFT JOIN
    sequence_upload_aux_table s
    ON m.upload_id = s.upload_id 
    AND m.submission_id = s.submission_id
GROUP BY
    m.upload_id,
    m.accession,
    m.version,
    m.organism,
    m.submission_id,
    m.submitter,
    m.group_id,
    m.uploaded_at,
    m.metadata,
    m.files;

then the SQL in the code would just be

INSERT INTO sequence_entries (
    accession,
    version,
    organism,
    submission_id,
    submitter,
    group_id,
    submitted_at,
    original_data
)
SELECT
    accession,
    version,
    organism,
    submission_id,
    submitter,
    group_id,
    submitted_at,
    original_data
FROM sequence_entry_staging
WHERE upload_id = ?
RETURNING accession, version, submission_id;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions