Files
pipelines/internal/pipeline/storage_policy.go
T

104 lines
3.0 KiB
Go

package pipeline
import (
"context"
"database/sql"
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const storagePolicySetting = "storage_retention_v1"
func (s *Store) StoragePolicy(ctx context.Context) (protocol.StoragePolicy, error) {
var value string
var policy protocol.StoragePolicy
err := s.db.QueryRowContext(ctx, "SELECT value FROM server_settings WHERE key=?", storagePolicySetting).Scan(&value)
if err == sql.ErrNoRows {
return policy, nil
}
if err != nil {
return policy, err
}
if err = json.Unmarshal([]byte(value), &policy); err != nil {
return policy, err
}
return policy, policy.Validate()
}
func (s *Store) SaveStoragePolicy(ctx context.Context, p protocol.StoragePolicy) error {
if err := p.Validate(); err != nil {
return err
}
value, err := json.Marshal(p)
if err != nil {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value`, storagePolicySetting, string(value))
return err
}
// PreviewRunDeletion uses the same eligibility checks as deletion. Its byte
// result is only an eligibility check; storage scans measure reclaimable files.
func (s *Store) PreviewRunDeletion(ctx context.Context, id int64, root string) (int64, string, error) {
reason, _, err := deleteRefusal(ctx, s.db, id)
if err != nil || reason != "" {
return 0, reason, err
}
return 0, "", nil
}
// DatabasePath identifies the server-owned database for read-only inventory.
func (s *Store) DatabasePath() string { return s.path }
// StorageOperatorDirectories deliberately has no chat-list limit: an old or
// closed session can still be resumed and must protect its checkout.
func (s *Store) StorageOperatorDirectories(ctx context.Context) ([]string, error) {
rows, err := s.db.QueryContext(ctx, `SELECT DISTINCT directory FROM operator_sessions WHERE directory<>''`)
if err != nil {
return nil, err
}
defer rows.Close()
var paths []string
for rows.Next() {
var path string
if err := rows.Scan(&path); err != nil {
return nil, err
}
paths = append(paths, path)
}
return paths, rows.Err()
}
// RememberStorageWorkspaces keeps inventory coverage stable after run history
// is removed. It stores identities only, never creates directories or runs.
func (s *Store) RememberStorageWorkspaces(ctx context.Context, paths []string) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
for _, path := range paths {
if path != "" {
if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO storage_workspaces(path) VALUES(?)`, path); err != nil {
return err
}
}
}
return tx.Commit()
}
func (s *Store) StorageWorkspacePaths(ctx context.Context) ([]string, error) {
rows, err := s.db.QueryContext(ctx, `SELECT path FROM storage_workspaces ORDER BY path`)
if err != nil {
return nil, err
}
defer rows.Close()
var paths []string
for rows.Next() {
var path string
if err := rows.Scan(&path); err != nil {
return nil, err
}
paths = append(paths, path)
}
return paths, rows.Err()
}