74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"git.nakama.town/fmartingr/hako/internal/model"
|
|
"github.com/huandu/go-sqlbuilder"
|
|
)
|
|
|
|
// ArchiverConfigStore handles database operations for archiver configurations
|
|
type ArchiverConfigStore struct {
|
|
readDB *sql.DB
|
|
writeDB *sql.DB
|
|
}
|
|
|
|
// NewArchiverConfigStore creates a new ArchiverConfigStore
|
|
func NewArchiverConfigStore(readDB, writeDB *sql.DB) *ArchiverConfigStore {
|
|
return &ArchiverConfigStore{
|
|
readDB: readDB,
|
|
writeDB: writeDB,
|
|
}
|
|
}
|
|
|
|
// Get retrieves archiver configuration by key
|
|
func (s *ArchiverConfigStore) Get(ctx context.Context, key string) (*model.ArchiverConfig, error) {
|
|
sb := sqlbuilder.NewSelectBuilder()
|
|
sb.Select("extractor_key", "config_json", "updated_at")
|
|
sb.From("extractor_configs")
|
|
sb.Where(sb.Equal("extractor_key", key))
|
|
|
|
query, args := sb.Build()
|
|
row := s.readDB.QueryRowContext(ctx, query, args...)
|
|
|
|
var config model.ArchiverConfig
|
|
var updatedAt string
|
|
|
|
err := row.Scan(&config.ArchiverKey, &config.ConfigJSON, &updatedAt)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil // Not found is not an error
|
|
}
|
|
return nil, fmt.Errorf("failed to get archiver config: %w", err)
|
|
}
|
|
|
|
// Parse timestamp
|
|
config.UpdatedAt, _ = parseTimestamp(updatedAt)
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// Upsert creates or updates archiver configuration
|
|
func (s *ArchiverConfigStore) Upsert(ctx context.Context, config *model.ArchiverConfig) error {
|
|
// SQLite supports INSERT OR REPLACE
|
|
query := `INSERT OR REPLACE INTO extractor_configs (extractor_key, config_json, updated_at) VALUES (?, ?, ?)`
|
|
_, err := s.writeDB.ExecContext(ctx, query, config.ArchiverKey, config.ConfigJSON, config.UpdatedAt.Format("2006-01-02 15:04:05"))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to upsert archiver config: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete removes archiver configuration by key
|
|
func (s *ArchiverConfigStore) Delete(ctx context.Context, key string) error {
|
|
query := `DELETE FROM extractor_configs WHERE extractor_key = ?`
|
|
_, err := s.writeDB.ExecContext(ctx, query, key)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete archiver config: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|