Merged in jmathison/v2-upload-batch (pull request #224)

Implement v2 upload batch cleanup

* Implement v2 upload batch cleanup

* Merge remote-tracking branch 'origin/main' into jmathison/v2-upload-batch

* Address upload batch review feedback

* Raise batch worker coverage

* Fix batch cleanup review issues


Approved-by: Jay Brown
This commit is contained in:
Jacob Mathison
2026-05-08 21:42:46 +00:00
parent 7b820b18c2
commit a080ca59d8
35 changed files with 2587 additions and 108 deletions
+41
View File
@@ -88,6 +88,15 @@ VALUES ($1, $2, $3, $4)
ON CONFLICT (clientId, path) DO UPDATE SET path = EXCLUDED.path
RETURNING *;
-- name: CreateFolderIfAbsent :one
-- Insert a folder only when it does not already exist.
-- Batch uploads use this to distinguish newly-created folder paths from
-- pre-existing paths without mutating existing folders.
INSERT INTO folders (path, parentId, clientId, createdBy)
VALUES (@path, @parent_id, @client_id, @created_by)
ON CONFLICT (clientId, path) DO NOTHING
RETURNING *;
-- name: GetDocumentsByFolderEnriched :many
-- Gets documents in a folder with all enriched metadata fields
SELECT
@@ -180,3 +189,35 @@ WITH RECURSIVE folder_tree AS (
JOIN folder_tree ft ON f.parentId = ft.id
)
DELETE FROM folders WHERE id IN (SELECT id FROM folder_tree);
-- name: ListSafeAutoCreatedFolderCandidates :many
-- @sqlc-vet-disable
-- Candidate folders can be deleted only after every batch that touched that
-- folder has finished folder cleanup. The path equality check protects renamed
-- folders from deletion.
SELECT DISTINCT f.id, f.path
FROM folders f
JOIN batch_folder_candidates bfc ON bfc.folder_id = f.id
WHERE bfc.existed_before = false
AND bfc.path = f.path
AND f.path <> '/'
AND NOT EXISTS (
SELECT 1
FROM batch_folder_candidates other_bfc
JOIN batch_uploads bu ON bu.id = other_bfc.batch_id
WHERE other_bfc.folder_id = f.id
AND bu.folder_cleanup_completed_at IS NULL
)
ORDER BY f.path DESC
LIMIT sqlc.arg(limit_count);
-- name: DeleteSafeAutoCreatedFolder :execrows
-- @sqlc-vet-disable
-- Delete one auto-created folder only when it is currently empty and no longer
-- referenced by documents, uploads, or child folders.
DELETE FROM folders f
WHERE f.id = @folder_id
AND f.path <> '/'
AND NOT EXISTS (SELECT 1 FROM documents d WHERE d.folderId = f.id)
AND NOT EXISTS (SELECT 1 FROM documentUploads du WHERE du.folder_id = f.id)
AND NOT EXISTS (SELECT 1 FROM folders child WHERE child.parentId = f.id);