retention and replace

This commit is contained in:
venture
2026-11-16 10:04:52 +01:00
parent 610192e2d5
commit cc0ddd0dfb
11 changed files with 1153 additions and 5 deletions
+133
View File
@@ -653,6 +653,16 @@ func (q *Queries) DeleteAccount(ctx context.Context, principalID ident.UUID) err
return err
}
const deleteBlob = `-- name: DeleteBlob :exec
DELETE FROM blob
WHERE root = ?1
`
func (q *Queries) DeleteBlob(ctx context.Context, root []byte) error {
_, err := q.db.ExecContext(ctx, deleteBlob, root)
return err
}
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :execrows
DELETE FROM session
WHERE expires_at <= ?1
@@ -1853,6 +1863,60 @@ func (q *Queries) ListSessionsByPrincipal(ctx context.Context, principalID ident
return items, nil
}
const listUnreferencedBlobs = `-- name: ListUnreferencedBlobs :many
SELECT b.root, b.size, b.state, b.role, b.unit_size, b.have, b.tree_state, b.encryption, b.last_verified_at, b.verify_error, b.last_access_at, b.created_at
FROM blob b
WHERE NOT EXISTS (
SELECT 1
FROM document_version v
WHERE v.content_root = b.root
AND v.content_purged_at IS NULL
)
AND NOT EXISTS (
SELECT 1
FROM export_link e
WHERE e.blob_root = b.root
AND e.state <> 'stale'
)
ORDER BY b.created_at
`
func (q *Queries) ListUnreferencedBlobs(ctx context.Context) ([]Blob, error) {
rows, err := q.db.QueryContext(ctx, listUnreferencedBlobs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Blob
for rows.Next() {
var i Blob
if err := rows.Scan(
&i.Root,
&i.Size,
&i.State,
&i.Role,
&i.UnitSize,
&i.Have,
&i.TreeState,
&i.Encryption,
&i.LastVerifiedAt,
&i.VerifyError,
&i.LastAccessAt,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockAccount = `-- name: LockAccount :one
UPDATE account
SET state = 'locked',
@@ -1953,6 +2017,32 @@ func (q *Queries) NextDocumentVersionSeq(ctx context.Context, documentID ident.U
return seq, err
}
const purgeDocumentVersion = `-- name: PurgeDocumentVersion :execrows
UPDATE document_version
SET content_purged_at = ?1
WHERE document_version.id = ?2
AND document_version.content_purged_at IS NULL
AND NOT EXISTS (
SELECT 1
FROM document d
WHERE d.id = document_version.document_id
AND d.head_version_id = document_version.id
)
`
type PurgeDocumentVersionParams struct {
PurgedAt sql.NullInt64
ID ident.UUID
}
func (q *Queries) PurgeDocumentVersion(ctx context.Context, arg PurgeDocumentVersionParams) (int64, error) {
result, err := q.db.ExecContext(ctx, purgeDocumentVersion, arg.PurgedAt, arg.ID)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const recordLogin = `-- name: RecordLogin :one
UPDATE account
SET last_login_at = ?1,
@@ -2322,6 +2412,49 @@ func (q *Queries) UpdateAccountState(ctx context.Context, arg UpdateAccountState
return i, err
}
const updateBlobLocationState = `-- name: UpdateBlobLocationState :exec
UPDATE blob_location
SET state = ?1
WHERE root = ?2
AND backend_id = ?3
`
type UpdateBlobLocationStateParams struct {
State string
Root []byte
BackendID ident.UUID
}
func (q *Queries) UpdateBlobLocationState(ctx context.Context, arg UpdateBlobLocationStateParams) error {
_, err := q.db.ExecContext(ctx, updateBlobLocationState, arg.State, arg.Root, arg.BackendID)
return err
}
const updateBlobVerification = `-- name: UpdateBlobVerification :exec
UPDATE blob
SET state = ?1,
last_verified_at = ?2,
verify_error = ?3
WHERE root = ?4
`
type UpdateBlobVerificationParams struct {
State string
LastVerifiedAt sql.NullInt64
VerifyError sql.NullString
Root []byte
}
func (q *Queries) UpdateBlobVerification(ctx context.Context, arg UpdateBlobVerificationParams) error {
_, err := q.db.ExecContext(ctx, updateBlobVerification,
arg.State,
arg.LastVerifiedAt,
arg.VerifyError,
arg.Root,
)
return err
}
const updateDocumentLocation = `-- name: UpdateDocumentLocation :exec
UPDATE document
SET path = ?1,