package database import ( "context" "database/sql" "fmt" "time" ) type SnapshotRepository struct { db *DB } func NewSnapshotRepository(db *DB) *SnapshotRepository { return &SnapshotRepository{db: db} } func (r *SnapshotRepository) Create(ctx context.Context, tx *sql.Tx, snapshot *Snapshot) error { query := ` INSERT INTO snapshots (id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count) VALUES (?, ?, ?, ?, ?, ?, ?) ` var err error if tx != nil { _, err = tx.ExecContext(ctx, query, snapshot.ID, snapshot.Hostname, snapshot.VaultikVersion, snapshot.CreatedTS.Unix(), snapshot.FileCount, snapshot.ChunkCount, snapshot.BlobCount) } else { _, err = r.db.conn.ExecContext(ctx, query, snapshot.ID, snapshot.Hostname, snapshot.VaultikVersion, snapshot.CreatedTS.Unix(), snapshot.FileCount, snapshot.ChunkCount, snapshot.BlobCount) } if err != nil { return fmt.Errorf("inserting snapshot: %w", err) } return nil } func (r *SnapshotRepository) UpdateCounts(ctx context.Context, tx *sql.Tx, snapshotID string, fileCount, chunkCount, blobCount int64) error { query := ` UPDATE snapshots SET file_count = ?, chunk_count = ?, blob_count = ? WHERE id = ? ` var err error if tx != nil { _, err = tx.ExecContext(ctx, query, fileCount, chunkCount, blobCount, snapshotID) } else { _, err = r.db.conn.ExecContext(ctx, query, fileCount, chunkCount, blobCount, snapshotID) } if err != nil { return fmt.Errorf("updating snapshot: %w", err) } return nil } func (r *SnapshotRepository) GetByID(ctx context.Context, snapshotID string) (*Snapshot, error) { query := ` SELECT id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count FROM snapshots WHERE id = ? ` var snapshot Snapshot var createdTSUnix int64 err := r.db.conn.QueryRowContext(ctx, query, snapshotID).Scan( &snapshot.ID, &snapshot.Hostname, &snapshot.VaultikVersion, &createdTSUnix, &snapshot.FileCount, &snapshot.ChunkCount, &snapshot.BlobCount, ) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, fmt.Errorf("querying snapshot: %w", err) } snapshot.CreatedTS = time.Unix(createdTSUnix, 0) return &snapshot, nil } func (r *SnapshotRepository) ListRecent(ctx context.Context, limit int) ([]*Snapshot, error) { query := ` SELECT id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count FROM snapshots ORDER BY created_ts DESC LIMIT ? ` rows, err := r.db.conn.QueryContext(ctx, query, limit) if err != nil { return nil, fmt.Errorf("querying snapshots: %w", err) } defer CloseRows(rows) var snapshots []*Snapshot for rows.Next() { var snapshot Snapshot var createdTSUnix int64 err := rows.Scan( &snapshot.ID, &snapshot.Hostname, &snapshot.VaultikVersion, &createdTSUnix, &snapshot.FileCount, &snapshot.ChunkCount, &snapshot.BlobCount, ) if err != nil { return nil, fmt.Errorf("scanning snapshot: %w", err) } snapshot.CreatedTS = time.Unix(createdTSUnix, 0) snapshots = append(snapshots, &snapshot) } return snapshots, rows.Err() }