From cf67283f1701034f27c682821c819701dd6b9939 Mon Sep 17 00:00:00 2001 From: Chaoran Chen Date: Mon, 18 Mar 2024 16:39:24 +0100 Subject: [PATCH] refactor(backend): simplify UploadDatabaseService (#1367) --- .../submission/UploadDatabaseService.kt | 105 +++++++----------- .../main/resources/db/migration/V1__init.sql | 2 +- 2 files changed, 42 insertions(+), 65 deletions(-) diff --git a/backend/src/main/kotlin/org/loculus/backend/service/submission/UploadDatabaseService.kt b/backend/src/main/kotlin/org/loculus/backend/service/submission/UploadDatabaseService.kt index 10f6f70c2..f11f21835 100644 --- a/backend/src/main/kotlin/org/loculus/backend/service/submission/UploadDatabaseService.kt +++ b/backend/src/main/kotlin/org/loculus/backend/service/submission/UploadDatabaseService.kt @@ -17,7 +17,6 @@ import org.loculus.backend.api.Status import org.loculus.backend.api.SubmissionIdMapping import org.loculus.backend.model.SubmissionId import org.loculus.backend.model.SubmissionParams -import org.loculus.backend.model.UploadType import org.loculus.backend.service.GenerateAccessionFromNumberService import org.loculus.backend.service.datauseterms.DataUseTermsDatabaseService import org.loculus.backend.service.submission.MetadataUploadAuxTable.accessionColumn @@ -123,8 +122,47 @@ class UploadDatabaseService( "mapping and copying sequences with UploadId $uploadId and uploadType: $submissionParams.uploadType" } + val mapAndCopySql = """ + INSERT INTO sequence_entries ( + accession, + version, + organism, + submission_id, + submitter, + group_name, + submitted_at, + original_data, + status + ) + SELECT + m.accession, + m.version, + m.organism, + m.submission_id, + m.submitter, + m.group_name, + m.uploaded_at, + jsonb_build_object( + 'metadata', m.metadata, + 'unalignedNucleotideSequences', jsonb_object_agg(s.segment_name, s.compressed_sequence_data) + ), + '${Status.RECEIVED.name}' + FROM + metadata_upload_aux_table m + JOIN + sequence_upload_aux_table s ON m.upload_id = s.upload_id AND m.submission_id = s.submission_id + WHERE m.upload_id = ? + GROUP BY + m.upload_id, + m.organism, + m.submission_id, + m.submitter, + m.group_name, + m.uploaded_at + RETURNING accession, version, submission_id; + """.trimIndent() val insertionResult = exec( - generateMapAndCopyStatement(submissionParams.uploadType), + mapAndCopySql, listOf( Pair(VarCharColumnType(), uploadId), ), @@ -221,72 +259,11 @@ class UploadDatabaseService( }, ) { it[accessionColumn] = accession + it[versionColumn] = 1 } } } - private fun generateMapAndCopyStatement(uploadType: UploadType): String { - val commonColumns = StringBuilder().apply { - append("accession,") - if (uploadType == UploadType.REVISION) { - append("version,") - } - append( - """ - organism, - submission_id, - submitter, - group_name, - submitted_at, - original_data, - status - """, - ) - }.toString() - - val specificColumns = if (uploadType == UploadType.ORIGINAL) { - """ - m.accession, - """.trimIndent() - } else { - """ - m.accession, - m.version, - """.trimIndent() - } - - return """ - INSERT INTO sequence_entries ( - $commonColumns - ) - SELECT - $specificColumns - m.organism, - m.submission_id, - m.submitter, - m.group_name, - m.uploaded_at, - jsonb_build_object( - 'metadata', m.metadata, - 'unalignedNucleotideSequences', jsonb_object_agg(s.segment_name, s.compressed_sequence_data) - ), - '${Status.RECEIVED.name}' - FROM - metadata_upload_aux_table m - JOIN - sequence_upload_aux_table s ON m.upload_id = s.upload_id AND m.submission_id = s.submission_id - WHERE m.upload_id = ? - GROUP BY - m.upload_id, - m.organism, - m.submission_id, - m.submitter, - m.group_name, - m.uploaded_at - RETURNING accession, version, submission_id; - """.trimIndent() - } - fun getNextSequenceNumbers(numberOfNewEntries: Int) = transaction { val nextValues = exec( "SELECT nextval('accession_sequence') FROM generate_series(1, ?)", diff --git a/backend/src/main/resources/db/migration/V1__init.sql b/backend/src/main/resources/db/migration/V1__init.sql index c09c196b3..277e0d971 100644 --- a/backend/src/main/resources/db/migration/V1__init.sql +++ b/backend/src/main/resources/db/migration/V1__init.sql @@ -14,7 +14,7 @@ create table groups_table ( create table sequence_entries ( accession text not null, - version bigint not null default 1, + version bigint not null, organism text not null, submission_id text not null, submitter text not null,