- Add SQLite database connection management with proper error handling - Implement schema for files, chunks, blobs, and snapshots tables - Create repository pattern for each database table - Add transaction support with proper rollback handling - Integrate database module with fx dependency injection - Make index path configurable via VAULTIK_INDEX_PATH env var - Add fatal error handling for database integrity issues - Update DESIGN.md to clarify file_chunks vs chunk_files distinction - Remove FinalHash from BlobInfo (blobs are content-addressable) - Add file metadata support (mtime, ctime, mode, uid, gid, symlinks)
91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
type Repositories struct {
|
|
db *DB
|
|
Files *FileRepository
|
|
Chunks *ChunkRepository
|
|
Blobs *BlobRepository
|
|
FileChunks *FileChunkRepository
|
|
BlobChunks *BlobChunkRepository
|
|
ChunkFiles *ChunkFileRepository
|
|
Snapshots *SnapshotRepository
|
|
}
|
|
|
|
func NewRepositories(db *DB) *Repositories {
|
|
return &Repositories{
|
|
db: db,
|
|
Files: NewFileRepository(db),
|
|
Chunks: NewChunkRepository(db),
|
|
Blobs: NewBlobRepository(db),
|
|
FileChunks: NewFileChunkRepository(db),
|
|
BlobChunks: NewBlobChunkRepository(db),
|
|
ChunkFiles: NewChunkFileRepository(db),
|
|
Snapshots: NewSnapshotRepository(db),
|
|
}
|
|
}
|
|
|
|
type TxFunc func(ctx context.Context, tx *sql.Tx) error
|
|
|
|
func (r *Repositories) WithTx(ctx context.Context, fn TxFunc) error {
|
|
tx, err := r.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("beginning transaction: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
Fatal("failed to rollback transaction: %v", rollbackErr)
|
|
}
|
|
panic(p)
|
|
} else if err != nil {
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
Fatal("failed to rollback transaction: %v", rollbackErr)
|
|
}
|
|
}
|
|
}()
|
|
|
|
err = fn(ctx, tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (r *Repositories) WithReadTx(ctx context.Context, fn TxFunc) error {
|
|
opts := &sql.TxOptions{
|
|
ReadOnly: true,
|
|
}
|
|
tx, err := r.db.BeginTx(ctx, opts)
|
|
if err != nil {
|
|
return fmt.Errorf("beginning read transaction: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
Fatal("failed to rollback transaction: %v", rollbackErr)
|
|
}
|
|
panic(p)
|
|
} else if err != nil {
|
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
|
Fatal("failed to rollback transaction: %v", rollbackErr)
|
|
}
|
|
}
|
|
}()
|
|
|
|
err = fn(ctx, tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|