feat(db): add storage backend metadata columns
Add storage_backend columns to track which backend (minio or filesystem) holds each file, enabling dual-running during migration. Migration 017_file_storage_metadata.sql: - item_files.storage_backend TEXT NOT NULL DEFAULT 'minio' - revisions.file_storage_backend TEXT NOT NULL DEFAULT 'minio' DB repository changes: - Revision struct: add FileStorageBackend field - ItemFile struct: add StorageBackend field - All INSERT queries include the new columns - All SELECT queries read them (COALESCE for pre-migration compat) - CreateRevisionFromExisting copies the backend from source revision - Default to 'minio' when field is empty (backward compat) Existing rows default to 'minio'. New uploads will write 'filesystem' when the filesystem backend is active. Closes #128
This commit is contained in:
@@ -8,13 +8,14 @@ import (
|
||||
|
||||
// ItemFile represents a file attachment on an item.
|
||||
type ItemFile struct {
|
||||
ID string
|
||||
ItemID string
|
||||
Filename string
|
||||
ContentType string
|
||||
Size int64
|
||||
ObjectKey string
|
||||
CreatedAt time.Time
|
||||
ID string
|
||||
ItemID string
|
||||
Filename string
|
||||
ContentType string
|
||||
Size int64
|
||||
ObjectKey string
|
||||
StorageBackend string // "minio" or "filesystem"
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// ItemFileRepository provides item_files database operations.
|
||||
@@ -29,11 +30,14 @@ func NewItemFileRepository(db *DB) *ItemFileRepository {
|
||||
|
||||
// Create inserts a new item file record.
|
||||
func (r *ItemFileRepository) Create(ctx context.Context, f *ItemFile) error {
|
||||
if f.StorageBackend == "" {
|
||||
f.StorageBackend = "minio"
|
||||
}
|
||||
err := r.db.pool.QueryRow(ctx,
|
||||
`INSERT INTO item_files (item_id, filename, content_type, size, object_key)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`INSERT INTO item_files (item_id, filename, content_type, size, object_key, storage_backend)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at`,
|
||||
f.ItemID, f.Filename, f.ContentType, f.Size, f.ObjectKey,
|
||||
f.ItemID, f.Filename, f.ContentType, f.Size, f.ObjectKey, f.StorageBackend,
|
||||
).Scan(&f.ID, &f.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating item file: %w", err)
|
||||
@@ -44,7 +48,8 @@ func (r *ItemFileRepository) Create(ctx context.Context, f *ItemFile) error {
|
||||
// ListByItem returns all file attachments for an item.
|
||||
func (r *ItemFileRepository) ListByItem(ctx context.Context, itemID string) ([]*ItemFile, error) {
|
||||
rows, err := r.db.pool.Query(ctx,
|
||||
`SELECT id, item_id, filename, content_type, size, object_key, created_at
|
||||
`SELECT id, item_id, filename, content_type, size, object_key,
|
||||
COALESCE(storage_backend, 'minio'), created_at
|
||||
FROM item_files WHERE item_id = $1 ORDER BY created_at`,
|
||||
itemID,
|
||||
)
|
||||
@@ -56,7 +61,7 @@ func (r *ItemFileRepository) ListByItem(ctx context.Context, itemID string) ([]*
|
||||
var files []*ItemFile
|
||||
for rows.Next() {
|
||||
f := &ItemFile{}
|
||||
if err := rows.Scan(&f.ID, &f.ItemID, &f.Filename, &f.ContentType, &f.Size, &f.ObjectKey, &f.CreatedAt); err != nil {
|
||||
if err := rows.Scan(&f.ID, &f.ItemID, &f.Filename, &f.ContentType, &f.Size, &f.ObjectKey, &f.StorageBackend, &f.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning item file: %w", err)
|
||||
}
|
||||
files = append(files, f)
|
||||
@@ -68,10 +73,11 @@ func (r *ItemFileRepository) ListByItem(ctx context.Context, itemID string) ([]*
|
||||
func (r *ItemFileRepository) Get(ctx context.Context, id string) (*ItemFile, error) {
|
||||
f := &ItemFile{}
|
||||
err := r.db.pool.QueryRow(ctx,
|
||||
`SELECT id, item_id, filename, content_type, size, object_key, created_at
|
||||
`SELECT id, item_id, filename, content_type, size, object_key,
|
||||
COALESCE(storage_backend, 'minio'), created_at
|
||||
FROM item_files WHERE id = $1`,
|
||||
id,
|
||||
).Scan(&f.ID, &f.ItemID, &f.Filename, &f.ContentType, &f.Size, &f.ObjectKey, &f.CreatedAt)
|
||||
).Scan(&f.ID, &f.ItemID, &f.Filename, &f.ContentType, &f.Size, &f.ObjectKey, &f.StorageBackend, &f.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting item file: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user