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;
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
mapAndCopytoinsertFromAuxTablesIntoMainTableor something.We could create a VIEW in a migration, something like:
then the SQL in the code would just be