Skip to content

Snippets orphan removal #2146

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

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

-- This migration script adds cascade deletions on scan_results table and it's children tables.

ALTER TABLE snippet_findings
DROP CONSTRAINT snippet_findings_scan_summary_id_fkey,
ADD CONSTRAINT snippet_findings_scan_summary_id_fkey
FOREIGN KEY (scan_summary_id) REFERENCES scan_summaries (id) ON DELETE CASCADE;

ALTER TABLE snippet_findings_snippets
DROP CONSTRAINT snippet_findings_snippets_snippet_finding_id_fkey,
ADD CONSTRAINT snippet_findings_snippets_snippet_finding_id_fkey
FOREIGN KEY (snippet_finding_id) REFERENCES snippet_findings (id) ON DELETE CASCADE;

ALTER TABLE license_findings
DROP CONSTRAINT license_findings_scan_summary_id_fkey,
ADD CONSTRAINT license_findings_scan_summary_id_fkey
FOREIGN KEY (scan_summary_id) REFERENCES scan_summaries (id) ON DELETE CASCADE;

ALTER TABLE copyright_findings
DROP CONSTRAINT copyright_findings_scan_summary_id_fkey,
ADD CONSTRAINT copyright_findings_scan_summary_id_fkey
FOREIGN KEY (scan_summary_id) REFERENCES scan_summaries (id) ON DELETE CASCADE;
37 changes: 37 additions & 0 deletions services/hierarchy/src/main/kotlin/OrphanRemovalService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ import org.eclipse.apoapsis.ortserver.dao.repositories.repositoryconfiguration.P
import org.eclipse.apoapsis.ortserver.dao.repositories.repositoryconfiguration.PackageCurationDataTable
import org.eclipse.apoapsis.ortserver.dao.repositories.repositoryconfiguration.PackageCurationsTable
import org.eclipse.apoapsis.ortserver.dao.repositories.repositoryconfiguration.PackageLicenseChoicesTable
import org.eclipse.apoapsis.ortserver.dao.repositories.scannerrun.ScannerRunsScanResultsTable
import org.eclipse.apoapsis.ortserver.dao.repositories.scannerrun.ScannerRunsScannersTable
import org.eclipse.apoapsis.ortserver.dao.tables.NestedProvenanceSubRepositoriesTable
import org.eclipse.apoapsis.ortserver.dao.tables.NestedProvenancesTable
import org.eclipse.apoapsis.ortserver.dao.tables.NestedRepositoriesTable
import org.eclipse.apoapsis.ortserver.dao.tables.PackageProvenancesTable
import org.eclipse.apoapsis.ortserver.dao.tables.ScanResultsTable
import org.eclipse.apoapsis.ortserver.dao.tables.ScanSummariesTable
import org.eclipse.apoapsis.ortserver.dao.tables.SnippetFindingsSnippetsTable
import org.eclipse.apoapsis.ortserver.dao.tables.SnippetsTable
import org.eclipse.apoapsis.ortserver.dao.tables.shared.DeclaredLicensesTable
import org.eclipse.apoapsis.ortserver.dao.tables.shared.IdentifiersIssuesTable
Expand All @@ -63,6 +67,7 @@ import org.slf4j.LoggerFactory
/**
* Maintenance service to remove orphaned entities.
*/
@Suppress("TooManyFunctions")
class OrphanRemovalService(
private val db: Database
) {
Expand All @@ -82,6 +87,9 @@ class OrphanRemovalService(
logger.info("Deleted {} records from {}", deleteOrphanedIdentifiers(), IdentifiersTable.tableName)
logger.info("Deleted {} records from {}", deleteOrphanedVcsInfo(), VcsInfoTable.tableName)
logger.info("Deleted {} records from {}", deleteOrphanedRemoteArtifacts(), RemoteArtifactsTable.tableName)
logger.info("Deleted {} records from {}", deleteOrphanedScanResults(), ScanResultsTable.tableName)
logger.info("Deleted {} records from {}", deleteOrphanedScanSummaries(), ScanSummariesTable.tableName)
logger.info("Deleted {} records from {}", deleteOrphanedSnippets(), SnippetsTable.tableName)

logger.info("Deleting orphaned children of ORT runs finished.")
}
Expand Down Expand Up @@ -302,4 +310,33 @@ class OrphanRemovalService(
)
}
}

private suspend fun deleteOrphanedScanResults() =
db.dbQuery {
ScanResultsTable.deleteWhere {
id notInSubQuery (
ScannerRunsScanResultsTable.select(ScannerRunsScanResultsTable.scanResultId)
)
}
}

private suspend fun deleteOrphanedScanSummaries() =
db.dbQuery {
ScanSummariesTable.deleteWhere {
id notInSubQuery (
ScanResultsTable.select(ScanResultsTable.scanSummaryId)
)
}
}

private suspend fun deleteOrphanedSnippets() =
db.dbQuery {
SnippetsTable.deleteWhere {
id notInSubQuery (
SnippetFindingsSnippetsTable
.select(SnippetFindingsSnippetsTable.snippetId.alias("id"))
.where(SnippetFindingsSnippetsTable.snippetId.isNotNull())
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition looks strange. Are you sure it is correct? Are snippets not handled by cascade rules?

Choose a reason for hiding this comment

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

SnippetFindingsSnippetsTable is an connection table between snippet_findings and snippets table. So, as far as Snippet can be referenced by more than one scan (at least from DB perspective) it's not safe to delete snippets by cascade delete.

)
}
}
}
Loading
Loading