feat: add SQLite single-writer/multi-reader connection pool (#250)
This commit is contained in:
@@ -42,7 +42,7 @@ import (
|
||||
"github.com/kandev/kandev/internal/orchestrator"
|
||||
|
||||
// Database
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
)
|
||||
|
||||
// Command-line flags
|
||||
@@ -183,7 +183,7 @@ func startServices( //nolint:cyclop
|
||||
// ============================================
|
||||
log.Info("Initializing Task Service...")
|
||||
|
||||
dbConn, repos, repoCleanups, err := provideRepositories(cfg, log)
|
||||
dbPool, repos, repoCleanups, err := provideRepositories(cfg, log)
|
||||
if err != nil {
|
||||
log.Error("Failed to initialize repositories", zap.Error(err))
|
||||
return false
|
||||
@@ -225,7 +225,7 @@ func startServices( //nolint:cyclop
|
||||
}()
|
||||
}
|
||||
|
||||
return startAgentInfrastructure(ctx, cfg, log, addCleanup, eventBus, dockerClient, dbConn, repos, services, agentSettingsController, runCleanups)
|
||||
return startAgentInfrastructure(ctx, cfg, log, addCleanup, eventBus, dockerClient, dbPool, repos, services, agentSettingsController, runCleanups)
|
||||
}
|
||||
|
||||
// startAgentInfrastructure initializes the agent lifecycle manager, worktree, orchestrator,
|
||||
@@ -237,7 +237,7 @@ func startAgentInfrastructure(
|
||||
addCleanup func(func() error),
|
||||
eventBus bus.EventBus,
|
||||
dockerClient *docker.Client,
|
||||
dbConn *sqlx.DB,
|
||||
dbPool *db.Pool,
|
||||
repos *Repositories,
|
||||
services *Services,
|
||||
agentSettingsController *agentsettingscontroller.Controller,
|
||||
@@ -257,7 +257,7 @@ func startAgentInfrastructure(
|
||||
// ============================================
|
||||
log.Info("Initializing Worktree Manager...")
|
||||
|
||||
_, worktreeRecreator, worktreeCleanup, err := provideWorktreeManager(dbConn, cfg, log, lifecycleMgr, services.Task)
|
||||
_, worktreeRecreator, worktreeCleanup, err := provideWorktreeManager(dbPool, cfg, log, lifecycleMgr, services.Task)
|
||||
if err != nil {
|
||||
log.Error("Failed to initialize worktree manager", zap.Error(err))
|
||||
return false
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
analyticsrepository "github.com/kandev/kandev/internal/analytics/repository"
|
||||
"github.com/kandev/kandev/internal/common/config"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/persistence"
|
||||
"github.com/kandev/kandev/internal/task/repository"
|
||||
workflowrepository "github.com/kandev/kandev/internal/workflow/repository"
|
||||
@@ -17,15 +16,18 @@ import (
|
||||
userstore "github.com/kandev/kandev/internal/user/store"
|
||||
)
|
||||
|
||||
func provideRepositories(cfg *config.Config, log *logger.Logger) (*sqlx.DB, *Repositories, []func() error, error) {
|
||||
func provideRepositories(cfg *config.Config, log *logger.Logger) (*db.Pool, *Repositories, []func() error, error) {
|
||||
cleanups := make([]func() error, 0, 6)
|
||||
dbConn, cleanup, err := persistence.Provide(cfg, log)
|
||||
pool, cleanup, err := persistence.Provide(cfg, log)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
taskRepoImpl, cleanup, err := repository.Provide(dbConn)
|
||||
writer := pool.Writer()
|
||||
reader := pool.Reader()
|
||||
|
||||
taskRepoImpl, cleanup, err := repository.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -33,42 +35,42 @@ func provideRepositories(cfg *config.Config, log *logger.Logger) (*sqlx.DB, *Rep
|
||||
|
||||
// Workflow repo must be initialized before analytics repo because
|
||||
// analytics creates indexes on the workflow_steps table.
|
||||
workflowRepo, err := workflowrepository.NewWithDB(dbConn)
|
||||
workflowRepo, err := workflowrepository.NewWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
analyticsRepo, cleanup, err := analyticsrepository.Provide(dbConn)
|
||||
analyticsRepo, cleanup, err := analyticsrepository.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
agentSettingsRepo, cleanup, err := settingsstore.Provide(dbConn)
|
||||
agentSettingsRepo, cleanup, err := settingsstore.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
userRepo, cleanup, err := userstore.Provide(dbConn)
|
||||
userRepo, cleanup, err := userstore.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
notificationRepo, cleanup, err := notificationstore.Provide(dbConn)
|
||||
notificationRepo, cleanup, err := notificationstore.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
editorRepo, cleanup, err := editorstore.Provide(dbConn)
|
||||
editorRepo, cleanup, err := editorstore.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
cleanups = append(cleanups, cleanup)
|
||||
|
||||
promptRepo, cleanup, err := promptstore.Provide(dbConn)
|
||||
promptRepo, cleanup, err := promptstore.Provide(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -84,5 +86,5 @@ func provideRepositories(cfg *config.Config, log *logger.Logger) (*sqlx.DB, *Rep
|
||||
Prompts: promptRepo,
|
||||
Workflow: workflowRepo,
|
||||
}
|
||||
return dbConn, repos, cleanups, nil
|
||||
return pool, repos, cleanups, nil
|
||||
}
|
||||
|
||||
@@ -3,10 +3,9 @@ package main
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/lifecycle"
|
||||
"github.com/kandev/kandev/internal/common/config"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/common/constants"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
@@ -58,8 +57,8 @@ func (a *bootMsgAdapter) UpdateMessage(ctx context.Context, message *models.Mess
|
||||
return a.svc.UpdateMessage(ctx, message)
|
||||
}
|
||||
|
||||
func provideWorktreeManager(dbConn *sqlx.DB, cfg *config.Config, log *logger.Logger, lifecycleMgr *lifecycle.Manager, taskSvc *taskservice.Service) (*worktree.Manager, *worktree.Recreator, func() error, error) {
|
||||
manager, cleanup, err := worktree.Provide(dbConn, cfg, log)
|
||||
func provideWorktreeManager(dbPool *db.Pool, cfg *config.Config, log *logger.Logger, lifecycleMgr *lifecycle.Manager, taskSvc *taskservice.Service) (*worktree.Manager, *worktree.Recreator, func() error, error) {
|
||||
manager, cleanup, err := worktree.Provide(dbPool.Writer(), dbPool.Reader(), cfg, log)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package store
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Provide creates the SQLite agent settings store using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(db)
|
||||
// Provide creates the SQLite agent settings store using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -15,21 +15,22 @@ import (
|
||||
)
|
||||
|
||||
type sqliteRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
var _ Repository = (*sqliteRepository)(nil)
|
||||
|
||||
func newSQLiteRepositoryWithDB(dbConn *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(dbConn, false)
|
||||
func newSQLiteRepositoryWithDB(writer, reader *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: dbConn, ownsDB: ownsDB}
|
||||
func newSQLiteRepository(writer, reader *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -112,7 +113,7 @@ func (r *sqliteRepository) CreateAgent(ctx context.Context, agent *models.Agent)
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetAgent(ctx context.Context, id string) (*models.Agent, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, workspace_id, supports_mcp, mcp_config_path, created_at, updated_at
|
||||
FROM agents WHERE id = ?
|
||||
`), id)
|
||||
@@ -120,7 +121,7 @@ func (r *sqliteRepository) GetAgent(ctx context.Context, id string) (*models.Age
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetAgentByName(ctx context.Context, name string) (*models.Agent, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, workspace_id, supports_mcp, mcp_config_path, created_at, updated_at
|
||||
FROM agents WHERE name = ?
|
||||
`), name)
|
||||
@@ -156,7 +157,7 @@ func (r *sqliteRepository) DeleteAgent(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListAgents(ctx context.Context) ([]*models.Agent, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, workspace_id, supports_mcp, mcp_config_path, created_at, updated_at
|
||||
FROM agents ORDER BY created_at DESC
|
||||
`)
|
||||
@@ -179,7 +180,7 @@ func (r *sqliteRepository) ListAgents(ctx context.Context) ([]*models.Agent, err
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetAgentProfileMcpConfig(ctx context.Context, profileID string) (*models.AgentProfileMcpConfig, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT profile_id, enabled, servers_json, meta_json, created_at, updated_at
|
||||
FROM agent_profile_mcp_configs
|
||||
WHERE profile_id = ?
|
||||
@@ -288,7 +289,7 @@ func (r *sqliteRepository) DeleteAgentProfile(ctx context.Context, id string) er
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetAgentProfile(ctx context.Context, id string) (*models.AgentProfile, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, agent_id, name, agent_display_name, model, auto_approve, dangerously_skip_permissions, allow_indexing, cli_passthrough, user_modified, plan, created_at, updated_at, deleted_at
|
||||
FROM agent_profiles WHERE id = ? AND deleted_at IS NULL
|
||||
`), id)
|
||||
@@ -296,7 +297,7 @@ func (r *sqliteRepository) GetAgentProfile(ctx context.Context, id string) (*mod
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListAgentProfiles(ctx context.Context, agentID string) ([]*models.AgentProfile, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, agent_id, name, agent_display_name, model, auto_approve, dangerously_skip_permissions, allow_indexing, cli_passthrough, user_modified, plan, created_at, updated_at, deleted_at
|
||||
FROM agent_profiles WHERE agent_id = ? AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
`), agentID)
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"github.com/kandev/kandev/internal/analytics/repository/sqlite"
|
||||
)
|
||||
|
||||
// Provide creates the SQLite analytics repository using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (Repository, func() error, error) {
|
||||
repo, err := sqlite.NewWithDB(db)
|
||||
// Provide creates the SQLite analytics repository using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (Repository, func() error, error) {
|
||||
repo, err := sqlite.NewWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -5,13 +5,14 @@ import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Repository provides SQLite-based analytics operations.
|
||||
type Repository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer (for schema init)
|
||||
ro *sqlx.DB // reader (for all queries — this repo is pure reads)
|
||||
}
|
||||
|
||||
// NewWithDB creates a new analytics repository with an existing database connection.
|
||||
// It automatically creates performance indexes for stats queries.
|
||||
func NewWithDB(dbConn *sqlx.DB) (*Repository, error) {
|
||||
repo := &Repository{db: dbConn}
|
||||
// NewWithDB creates a new analytics repository with existing database connections.
|
||||
// It automatically creates performance indexes for stats queries using the writer.
|
||||
func NewWithDB(writer, reader *sqlx.DB) (*Repository, error) {
|
||||
repo := &Repository{db: writer, ro: reader}
|
||||
if err := repo.ensureStatsIndexes(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ func execOrFatal(t *testing.T, dbConn *sqlx.DB, query string, args ...any) {
|
||||
func TestEnsureStatsIndexes_CreatesIndexes(t *testing.T) {
|
||||
dbConn := createTestDB(t)
|
||||
|
||||
repo, err := NewWithDB(dbConn)
|
||||
repo, err := NewWithDB(dbConn, dbConn)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWithDB failed: %v", err)
|
||||
}
|
||||
@@ -169,7 +169,7 @@ func TestEnsureStatsIndexes_Idempotent(t *testing.T) {
|
||||
|
||||
func TestGetRepositoryStats_ExcludesSoftDeletedRepos(t *testing.T) {
|
||||
dbConn := createTestDB(t)
|
||||
repo, err := NewWithDB(dbConn)
|
||||
repo, err := NewWithDB(dbConn, dbConn)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWithDB failed: %v", err)
|
||||
}
|
||||
@@ -216,7 +216,7 @@ func TestGetRepositoryStats_ExcludesSoftDeletedRepos(t *testing.T) {
|
||||
|
||||
func TestGetGlobalStats_EmptyWorkspace(t *testing.T) {
|
||||
dbConn := createTestDB(t)
|
||||
repo, err := NewWithDB(dbConn)
|
||||
repo, err := NewWithDB(dbConn, dbConn)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWithDB failed: %v", err)
|
||||
}
|
||||
@@ -237,7 +237,7 @@ func TestGetGlobalStats_EmptyWorkspace(t *testing.T) {
|
||||
|
||||
func TestGetGlobalStats_WithTimeFilter(t *testing.T) {
|
||||
dbConn := createTestDB(t)
|
||||
repo, err := NewWithDB(dbConn)
|
||||
repo, err := NewWithDB(dbConn, dbConn)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWithDB failed: %v", err)
|
||||
}
|
||||
@@ -278,7 +278,7 @@ func TestGetGlobalStats_WithTimeFilter(t *testing.T) {
|
||||
|
||||
func TestGetGitStats_EmptyWorkspace(t *testing.T) {
|
||||
dbConn := createTestDB(t)
|
||||
repo, err := NewWithDB(dbConn)
|
||||
repo, err := NewWithDB(dbConn, dbConn)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWithDB failed: %v", err)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (r *Repository) GetTaskStats(ctx context.Context, workspaceID string, start
|
||||
startArg = start.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
dur := dialect.DurationMs(drv, "turn.completed_at", "turn.started_at")
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
@@ -80,7 +80,7 @@ func (r *Repository) GetTaskStats(ctx context.Context, workspaceID string, start
|
||||
ORDER BY t.updated_at DESC
|
||||
`, dur)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query),
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query),
|
||||
startArg, startArg, startArg, startArg,
|
||||
workspaceID, startArg, startArg,
|
||||
)
|
||||
@@ -128,7 +128,7 @@ func (r *Repository) GetGlobalStats(ctx context.Context, workspaceID string, sta
|
||||
startArg = start.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
dur := dialect.DurationMs(drv, "turn.completed_at", "turn.started_at")
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
@@ -168,7 +168,7 @@ func (r *Repository) GetGlobalStats(ctx context.Context, workspaceID string, sta
|
||||
|
||||
var stats models.GlobalStats
|
||||
var totalDurationMs float64
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(query),
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(query),
|
||||
workspaceID, startArg, startArg,
|
||||
workspaceID, startArg, startArg,
|
||||
workspaceID, startArg, startArg,
|
||||
@@ -199,7 +199,7 @@ func (r *Repository) GetGlobalStats(ctx context.Context, workspaceID string, sta
|
||||
|
||||
// GetDailyActivity retrieves daily activity statistics for the last N days
|
||||
func (r *Repository) GetDailyActivity(ctx context.Context, workspaceID string, days int) ([]*models.DailyActivity, error) {
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
dateStart := dialect.DateNowMinusDays(drv, "?")
|
||||
datePlus := dialect.DatePlusOneDay(drv, "date")
|
||||
curDate := dialect.CurrentDate(drv)
|
||||
@@ -235,7 +235,7 @@ func (r *Repository) GetDailyActivity(ctx context.Context, workspaceID string, d
|
||||
ORDER BY d.date ASC
|
||||
`, dateStart, datePlus, curDate, dateOfTurn, dateOfMsg, dateOfTurn, dateOfTurn)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), days-1, workspaceID)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), days-1, workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -255,7 +255,7 @@ func (r *Repository) GetDailyActivity(ctx context.Context, workspaceID string, d
|
||||
|
||||
// GetCompletedTaskActivity retrieves completed task counts for the last N days
|
||||
func (r *Repository) GetCompletedTaskActivity(ctx context.Context, workspaceID string, days int) ([]*models.CompletedTaskActivity, error) {
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
dateStart := dialect.DateNowMinusDays(drv, "?")
|
||||
datePlus := dialect.DatePlusOneDay(drv, "date")
|
||||
curDate := dialect.CurrentDate(drv)
|
||||
@@ -285,7 +285,7 @@ func (r *Repository) GetCompletedTaskActivity(ctx context.Context, workspaceID s
|
||||
ORDER BY d.date ASC
|
||||
`, dateStart, datePlus, curDate, dateOfCompleted, dateOfCompleted)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), days-1, workspaceID)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), days-1, workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -310,8 +310,8 @@ func (r *Repository) GetRepositoryStats(ctx context.Context, workspaceID string,
|
||||
startArg = start.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
query := buildRepositoryStatsQuery(r.db.DriverName())
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query),
|
||||
query := buildRepositoryStatsQuery(r.ro.DriverName())
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query),
|
||||
startArg, startArg, startArg, startArg,
|
||||
startArg, startArg, startArg, startArg,
|
||||
workspaceID,
|
||||
@@ -419,7 +419,7 @@ func (r *Repository) GetAgentUsage(ctx context.Context, workspaceID string, limi
|
||||
startArg = start.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
dur := dialect.DurationMs(drv, "turn.completed_at", "turn.started_at")
|
||||
jeName := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "name")
|
||||
jeDisplay := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "agent_display_name")
|
||||
@@ -444,7 +444,7 @@ func (r *Repository) GetAgentUsage(ctx context.Context, workspaceID string, limi
|
||||
LIMIT ?
|
||||
`, jeName, jeDisplay, jeModel, jeModelName, jeLLM, dur)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), workspaceID, startArg, startArg, limit)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), workspaceID, startArg, startArg, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -488,7 +488,7 @@ func (r *Repository) GetGitStats(ctx context.Context, workspaceID string, start
|
||||
`
|
||||
|
||||
var stats models.GitStats
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(query), workspaceID, startArg, startArg).Scan(
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(query), workspaceID, startArg, startArg).Scan(
|
||||
&stats.TotalCommits, &stats.TotalFilesChanged,
|
||||
&stats.TotalInsertions, &stats.TotalDeletions,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package db
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Pool provides separate read and write database connections.
|
||||
//
|
||||
// For SQLite with WAL mode, this enables concurrent reads while serializing
|
||||
// writes through a single connection. The writer pool uses MaxOpenConns(1) to
|
||||
// avoid SQLITE_BUSY on write contention, while the reader pool allows multiple
|
||||
// concurrent connections for SELECT queries.
|
||||
//
|
||||
// For PostgreSQL, both Writer and Reader return the same *sqlx.DB since pgx
|
||||
// handles connection pooling internally.
|
||||
type Pool struct {
|
||||
writer *sqlx.DB
|
||||
reader *sqlx.DB
|
||||
}
|
||||
|
||||
// NewPool creates a Pool from separate writer and reader connections.
|
||||
func NewPool(writer, reader *sqlx.DB) *Pool {
|
||||
return &Pool{writer: writer, reader: reader}
|
||||
}
|
||||
|
||||
// Writer returns the connection pool used for INSERT, UPDATE, DELETE, and
|
||||
// transactions. For SQLite this is limited to a single connection.
|
||||
func (p *Pool) Writer() *sqlx.DB { return p.writer }
|
||||
|
||||
// Reader returns the connection pool used for SELECT queries. For SQLite
|
||||
// this opens multiple read-only connections that can operate concurrently
|
||||
// with the writer via WAL snapshots.
|
||||
func (p *Pool) Reader() *sqlx.DB { return p.reader }
|
||||
|
||||
// Close closes both the writer and reader pools.
|
||||
func (p *Pool) Close() error {
|
||||
wErr := p.writer.Close()
|
||||
// Avoid double-close when both pools share the same *sqlx.DB (Postgres).
|
||||
if p.reader != p.writer {
|
||||
if rErr := p.reader.Close(); rErr != nil && wErr == nil {
|
||||
return rErr
|
||||
}
|
||||
}
|
||||
return wErr
|
||||
}
|
||||
@@ -12,9 +12,14 @@ import (
|
||||
|
||||
const (
|
||||
defaultBusyTimeout = 5 * time.Second
|
||||
|
||||
// defaultSQLiteReaderConns is the number of concurrent read connections.
|
||||
// SQLite WAL mode allows many readers alongside a single writer; 4 is a
|
||||
// reasonable default for a desktop/server workload.
|
||||
defaultSQLiteReaderConns = 4
|
||||
)
|
||||
|
||||
// OpenSQLite opens a SQLite database with shared best-practice settings.
|
||||
// OpenSQLite opens a SQLite database configured for writes (single connection).
|
||||
func OpenSQLite(dbPath string) (*sql.DB, error) {
|
||||
normalizedPath := normalizeSQLitePath(dbPath)
|
||||
if err := ensureSQLiteDir(normalizedPath); err != nil {
|
||||
@@ -24,7 +29,7 @@ func OpenSQLite(dbPath string) (*sql.DB, error) {
|
||||
return nil, fmt.Errorf("failed to create database file: %w", err)
|
||||
}
|
||||
|
||||
// SQLite DSN settings:
|
||||
// Writer DSN settings:
|
||||
// - foreign_keys=on: enforce FK constraints consistently.
|
||||
// - busy_timeout: wait briefly on locks to reduce transient "database is locked".
|
||||
// - journal_mode=WAL: better read concurrency with a single writer.
|
||||
@@ -40,13 +45,37 @@ func OpenSQLite(dbPath string) (*sql.DB, error) {
|
||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
}
|
||||
|
||||
// SQLite allows a single writer, keep pool small and deterministic.
|
||||
// Single writer connection: serializes writes and avoids SQLITE_BUSY.
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// OpenSQLiteReader opens a read-only SQLite connection pool with multiple
|
||||
// concurrent connections. Combined with WAL mode, this allows readers to
|
||||
// proceed without blocking on (or being blocked by) writes.
|
||||
func OpenSQLiteReader(dbPath string) (*sql.DB, error) {
|
||||
normalizedPath := normalizeSQLitePath(dbPath)
|
||||
|
||||
// Reader DSN: read-only mode, FK enforcement, shared cache.
|
||||
// journal_mode and synchronous are database-level (set by the writer).
|
||||
dsn := fmt.Sprintf(
|
||||
"file:%s?_foreign_keys=on&_mode=ro&_busy_timeout=%d&_cache=shared",
|
||||
normalizedPath,
|
||||
int(defaultBusyTimeout/time.Millisecond),
|
||||
)
|
||||
db, err := sql.Open("sqlite3", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open read-only database: %w", err)
|
||||
}
|
||||
|
||||
db.SetMaxOpenConns(defaultSQLiteReaderConns)
|
||||
db.SetMaxIdleConns(defaultSQLiteReaderConns)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func ensureSQLiteDir(dbPath string) error {
|
||||
dir := filepath.Dir(dbPath)
|
||||
if dir == "." || dir == "" {
|
||||
|
||||
@@ -2,9 +2,9 @@ package store
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Provide creates the SQLite editors store using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(db)
|
||||
// Provide creates the SQLite editor store using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -18,21 +18,22 @@ import (
|
||||
)
|
||||
|
||||
type sqliteRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
var _ Repository = (*sqliteRepository)(nil)
|
||||
|
||||
func newSQLiteRepositoryWithDB(dbConn *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(dbConn, false)
|
||||
func newSQLiteRepositoryWithDB(writer, reader *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: dbConn, ownsDB: ownsDB}
|
||||
func newSQLiteRepository(writer, reader *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -40,7 +41,7 @@ func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error
|
||||
}
|
||||
if err := repo.ensureDefaults(context.Background()); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after defaults error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -120,7 +121,7 @@ func (r *sqliteRepository) ensureDefaults(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListEditors(ctx context.Context) ([]*models.Editor, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, type, name, kind, command, scheme, config, installed, enabled, created_at, updated_at
|
||||
FROM editors
|
||||
ORDER BY name ASC
|
||||
@@ -147,7 +148,7 @@ func (r *sqliteRepository) ListEditors(ctx context.Context) ([]*models.Editor, e
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetEditorByType(ctx context.Context, editorType string) (*models.Editor, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, type, name, kind, command, scheme, config, installed, enabled, created_at, updated_at
|
||||
FROM editors
|
||||
WHERE type = ?
|
||||
@@ -156,7 +157,7 @@ func (r *sqliteRepository) GetEditorByType(ctx context.Context, editorType strin
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetEditorByID(ctx context.Context, editorID string) (*models.Editor, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, type, name, kind, command, scheme, config, installed, enabled, created_at, updated_at
|
||||
FROM editors
|
||||
WHERE id = ?
|
||||
|
||||
@@ -67,7 +67,7 @@ func NewTestServer(t *testing.T) *TestServer {
|
||||
dbConn, err := db.OpenSQLite(filepath.Join(tmpDir, "test.db"))
|
||||
require.NoError(t, err)
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
taskRepoImpl, cleanup, err := repository.Provide(sqlxDB)
|
||||
taskRepoImpl, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
require.NoError(t, err)
|
||||
taskRepo := taskRepoImpl
|
||||
t.Cleanup(func() {
|
||||
@@ -80,12 +80,12 @@ func NewTestServer(t *testing.T) *TestServer {
|
||||
}
|
||||
}
|
||||
})
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB); err != nil {
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB, sqlxDB); err != nil {
|
||||
t.Fatalf("failed to init worktree store: %v", err)
|
||||
}
|
||||
|
||||
// Initialize workflow service
|
||||
_, workflowSvc, _, err := workflow.Provide(sqlxDB, log)
|
||||
_, workflowSvc, _, err := workflow.Provide(sqlxDB, sqlxDB, log)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Initialize task service and wire workflow step creator
|
||||
|
||||
@@ -237,7 +237,7 @@ func NewOrchestratorTestServer(t *testing.T) *OrchestratorTestServer {
|
||||
dbConn, err := db.OpenSQLite(filepath.Join(tmpDir, "test.db"))
|
||||
require.NoError(t, err)
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
taskRepoImpl, cleanup, err := repository.Provide(sqlxDB)
|
||||
taskRepoImpl, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
require.NoError(t, err)
|
||||
taskRepo := taskRepoImpl
|
||||
t.Cleanup(func() {
|
||||
@@ -250,12 +250,12 @@ func NewOrchestratorTestServer(t *testing.T) *OrchestratorTestServer {
|
||||
}
|
||||
}
|
||||
})
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB); err != nil {
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB, sqlxDB); err != nil {
|
||||
t.Fatalf("failed to init worktree store: %v", err)
|
||||
}
|
||||
|
||||
// Initialize workflow service
|
||||
_, workflowSvc, _, err := workflow.Provide(sqlxDB, log)
|
||||
_, workflowSvc, _, err := workflow.Provide(sqlxDB, sqlxDB, log)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Initialize task service and wire workflow step creator
|
||||
|
||||
@@ -2,9 +2,9 @@ package store
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Provide creates the SQLite notifications store using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(db)
|
||||
// Provide creates the SQLite notification store using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -14,21 +14,22 @@ import (
|
||||
)
|
||||
|
||||
type sqliteRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
var _ Repository = (*sqliteRepository)(nil)
|
||||
|
||||
func newSQLiteRepositoryWithDB(dbConn *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(dbConn, false)
|
||||
func newSQLiteRepositoryWithDB(writer, reader *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: dbConn, ownsDB: ownsDB}
|
||||
func newSQLiteRepository(writer, reader *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -126,7 +127,7 @@ func (r *sqliteRepository) UpdateProvider(ctx context.Context, provider *models.
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetProvider(ctx context.Context, id string) (*models.Provider, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, user_id, name, type, config, enabled, created_at, updated_at
|
||||
FROM notification_providers
|
||||
WHERE id = ?
|
||||
@@ -135,7 +136,7 @@ func (r *sqliteRepository) GetProvider(ctx context.Context, id string) (*models.
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListProvidersByUser(ctx context.Context, userID string) ([]*models.Provider, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, user_id, name, type, config, enabled, created_at, updated_at
|
||||
FROM notification_providers
|
||||
WHERE user_id = ?
|
||||
@@ -168,7 +169,7 @@ func (r *sqliteRepository) DeleteProvider(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListSubscriptionsByProvider(ctx context.Context, providerID string) ([]*models.Subscription, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, user_id, provider_id, event_type, enabled, created_at, updated_at
|
||||
FROM notification_subscriptions
|
||||
WHERE provider_id = ?
|
||||
|
||||
@@ -136,7 +136,7 @@ func setupTestRepo(t *testing.T) repository.Repository {
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
t.Cleanup(func() { _ = sqlxDB.Close() })
|
||||
|
||||
repo, cleanup, err := repository.Provide(sqlxDB)
|
||||
repo, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test repository: %v", err)
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ func createTestExecutor(t *testing.T, agentMgr *mockAgentManager, log *logger.Lo
|
||||
t.Fatalf("failed to open test database: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB)
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test repository: %v", err)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,11 @@ import (
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
)
|
||||
|
||||
// Provide creates the database connection used by repositories.
|
||||
func Provide(cfg *config.Config, log *logger.Logger) (*sqlx.DB, func() error, error) {
|
||||
// Provide creates the database connection pool used by repositories.
|
||||
// For SQLite it returns a Pool with a single-writer connection and a
|
||||
// multi-reader connection pool (leveraging WAL for concurrent reads).
|
||||
// For PostgreSQL both Writer and Reader point to the same *sqlx.DB.
|
||||
func Provide(cfg *config.Config, log *logger.Logger) (*db.Pool, func() error, error) {
|
||||
driver := cfg.Database.Driver
|
||||
if driver == "" {
|
||||
driver = "sqlite"
|
||||
@@ -20,48 +23,69 @@ func Provide(cfg *config.Config, log *logger.Logger) (*sqlx.DB, func() error, er
|
||||
|
||||
switch driver {
|
||||
case "sqlite":
|
||||
dbPath := cfg.Database.Path
|
||||
if dbPath == "" {
|
||||
dbPath = "./kandev.db"
|
||||
}
|
||||
|
||||
dbConn, err := db.OpenSQLite(dbPath)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open sqlite database: %w", err)
|
||||
}
|
||||
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
|
||||
if log != nil {
|
||||
log.Info("Database initialized", zap.String("db_path", dbPath), zap.String("db_driver", driver))
|
||||
}
|
||||
cleanup := func() error {
|
||||
// Run PRAGMA optimize before closing to update query planner
|
||||
// statistics for tables that need it. This is the SQLite-recommended
|
||||
// way to maintain stats — lightweight and safe to call on every close.
|
||||
_, _ = sqlxDB.Exec("PRAGMA optimize")
|
||||
return sqlxDB.Close()
|
||||
}
|
||||
return sqlxDB, cleanup, nil
|
||||
|
||||
return provideSQLite(cfg, log)
|
||||
case "postgres":
|
||||
dsn := cfg.Database.DSN()
|
||||
dbConn, err := db.OpenPostgres(dsn, cfg.Database.MaxConns, cfg.Database.MinConns)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open postgres database: %w", err)
|
||||
}
|
||||
|
||||
sqlxDB := sqlx.NewDb(dbConn, "pgx")
|
||||
|
||||
if log != nil {
|
||||
log.Info("Database initialized", zap.String("db_driver", driver))
|
||||
}
|
||||
cleanup := func() error {
|
||||
return sqlxDB.Close()
|
||||
}
|
||||
return sqlxDB, cleanup, nil
|
||||
|
||||
return providePostgres(cfg, log)
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unsupported database driver: %s", driver)
|
||||
}
|
||||
}
|
||||
|
||||
func provideSQLite(cfg *config.Config, log *logger.Logger) (*db.Pool, func() error, error) {
|
||||
dbPath := cfg.Database.Path
|
||||
if dbPath == "" {
|
||||
dbPath = "./kandev.db"
|
||||
}
|
||||
|
||||
// Writer: single connection, owns WAL/journal_mode setup.
|
||||
writerConn, err := db.OpenSQLite(dbPath)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open sqlite writer: %w", err)
|
||||
}
|
||||
writer := sqlx.NewDb(writerConn, "sqlite3")
|
||||
|
||||
// Reader: multiple read-only connections for concurrent SELECTs.
|
||||
readerConn, err := db.OpenSQLiteReader(dbPath)
|
||||
if err != nil {
|
||||
_ = writer.Close()
|
||||
return nil, nil, fmt.Errorf("failed to open sqlite reader: %w", err)
|
||||
}
|
||||
reader := sqlx.NewDb(readerConn, "sqlite3")
|
||||
|
||||
pool := db.NewPool(writer, reader)
|
||||
|
||||
if log != nil {
|
||||
log.Info("Database initialized (single-writer pool)",
|
||||
zap.String("db_path", dbPath),
|
||||
zap.String("db_driver", "sqlite"),
|
||||
)
|
||||
}
|
||||
|
||||
cleanup := func() error {
|
||||
// Run PRAGMA optimize before closing to update query planner
|
||||
// statistics for tables that need it.
|
||||
_, _ = writer.Exec("PRAGMA optimize")
|
||||
return pool.Close()
|
||||
}
|
||||
return pool, cleanup, nil
|
||||
}
|
||||
|
||||
func providePostgres(cfg *config.Config, log *logger.Logger) (*db.Pool, func() error, error) {
|
||||
dsn := cfg.Database.DSN()
|
||||
dbConn, err := db.OpenPostgres(dsn, cfg.Database.MaxConns, cfg.Database.MinConns)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open postgres database: %w", err)
|
||||
}
|
||||
|
||||
pgDB := sqlx.NewDb(dbConn, "pgx")
|
||||
// For Postgres, writer and reader share the same pool.
|
||||
pool := db.NewPool(pgDB, pgDB)
|
||||
|
||||
if log != nil {
|
||||
log.Info("Database initialized", zap.String("db_driver", "postgres"))
|
||||
}
|
||||
cleanup := func() error {
|
||||
return pool.Close()
|
||||
}
|
||||
return pool, cleanup, nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func createService(t *testing.T) (*Service, func()) {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repoImpl, repoCleanup, err := promptstore.Provide(sqlxDB)
|
||||
repoImpl, repoCleanup, err := promptstore.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("create repo: %v", err)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package store
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Provide creates the SQLite prompts store using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(db)
|
||||
// Provide creates the SQLite prompts store using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -15,19 +15,20 @@ import (
|
||||
)
|
||||
|
||||
type sqliteRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
func newSQLiteRepositoryWithDB(dbConn *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(dbConn, false)
|
||||
func newSQLiteRepositoryWithDB(writer, reader *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: dbConn, ownsDB: ownsDB}
|
||||
func newSQLiteRepository(writer, reader *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -67,7 +68,7 @@ func (r *sqliteRepository) Close() error {
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) ListPrompts(ctx context.Context) ([]*models.Prompt, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, content, builtin, created_at, updated_at
|
||||
FROM custom_prompts
|
||||
ORDER BY builtin DESC, name ASC
|
||||
@@ -96,7 +97,7 @@ func (r *sqliteRepository) ListPrompts(ctx context.Context) ([]*models.Prompt, e
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetPromptByID(ctx context.Context, id string) (*models.Prompt, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, content, builtin, created_at, updated_at
|
||||
FROM custom_prompts
|
||||
WHERE id = ?
|
||||
@@ -111,7 +112,7 @@ func (r *sqliteRepository) GetPromptByID(ctx context.Context, id string) (*model
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetPromptByName(ctx context.Context, name string) (*models.Prompt, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, content, builtin, created_at, updated_at
|
||||
FROM custom_prompts
|
||||
WHERE name = ?
|
||||
|
||||
@@ -18,7 +18,7 @@ func createTestRepo(t *testing.T) (*sqliteRepository, func()) {
|
||||
t.Fatalf("failed to open sqlite db: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repo, err := newSQLiteRepositoryWithDB(sqlxDB)
|
||||
repo, err := newSQLiteRepositoryWithDB(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create repo: %v", err)
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
)
|
||||
|
||||
// Provide creates the SQLite repository using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (Repository, func() error, error) {
|
||||
repo, err := sqlite.NewWithDB(db)
|
||||
// Provide creates the SQLite repository using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (Repository, func() error, error) {
|
||||
repo, err := sqlite.NewWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ func createTestSQLiteRepo(t *testing.T) (*sqlite.Repository, func()) {
|
||||
t.Fatalf("failed to open SQLite database: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repo, err := sqlite.NewWithDB(sqlxDB)
|
||||
repo, err := sqlite.NewWithDB(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create SQLite repository: %v", err)
|
||||
}
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB); err != nil {
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB, sqlxDB); err != nil {
|
||||
t.Fatalf("failed to init worktree store: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,20 +10,21 @@ import (
|
||||
|
||||
// Repository provides SQLite-based task storage operations.
|
||||
type Repository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader (read-only pool)
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
// NewWithDB creates a new SQLite repository with an existing database connection (shared ownership).
|
||||
func NewWithDB(dbConn *sqlx.DB) (*Repository, error) {
|
||||
return newRepository(dbConn, false)
|
||||
func NewWithDB(writer, reader *sqlx.DB) (*Repository, error) {
|
||||
return newRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newRepository(dbConn *sqlx.DB, ownsDB bool) (*Repository, error) {
|
||||
repo := &Repository{db: dbConn, ownsDB: ownsDB}
|
||||
func newRepository(writer, reader *sqlx.DB, ownsDB bool) (*Repository, error) {
|
||||
repo := &Repository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (r *Repository) GetEnvironment(ctx context.Context, id string) (*models.Env
|
||||
var buildConfigJSON string
|
||||
var isSystem int
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, kind, is_system, worktree_root, image_tag, dockerfile, build_config, created_at, updated_at, deleted_at
|
||||
FROM environments WHERE id = ? AND deleted_at IS NULL
|
||||
`), id).Scan(
|
||||
@@ -104,7 +104,7 @@ func (r *Repository) DeleteEnvironment(ctx context.Context, id string) error {
|
||||
|
||||
// ListEnvironments returns all non-deleted environments
|
||||
func (r *Repository) ListEnvironments(ctx context.Context) ([]*models.Environment, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, kind, is_system, worktree_root, image_tag, dockerfile, build_config, created_at, updated_at, deleted_at
|
||||
FROM environments WHERE deleted_at IS NULL ORDER BY created_at ASC
|
||||
`)
|
||||
|
||||
@@ -41,7 +41,7 @@ func (r *Repository) GetExecutor(ctx context.Context, id string) (*models.Execut
|
||||
var isSystem int
|
||||
var resumable int
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, type, status, is_system, resumable, config, created_at, updated_at, deleted_at
|
||||
FROM executors WHERE id = ? AND deleted_at IS NULL
|
||||
`), id).Scan(
|
||||
@@ -103,7 +103,7 @@ func (r *Repository) DeleteExecutor(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func (r *Repository) ListExecutors(ctx context.Context) ([]*models.Executor, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, type, status, is_system, resumable, config, created_at, updated_at, deleted_at
|
||||
FROM executors WHERE deleted_at IS NULL ORDER BY created_at ASC
|
||||
`)
|
||||
@@ -204,7 +204,7 @@ func (r *Repository) UpsertExecutorRunning(ctx context.Context, running *models.
|
||||
}
|
||||
|
||||
func (r *Repository) ListExecutorsRunning(ctx context.Context) ([]*models.ExecutorRunning, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, session_id, task_id, executor_id, runtime, status, resumable, resume_token,
|
||||
agent_execution_id, container_id, agentctl_url, agentctl_port,
|
||||
worktree_id, worktree_path, worktree_branch, created_at, updated_at
|
||||
@@ -256,7 +256,7 @@ func (r *Repository) GetExecutorRunningBySessionID(ctx context.Context, sessionI
|
||||
var resumable int
|
||||
var lastSeen sql.NullTime
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, session_id, task_id, executor_id, runtime, status, resumable, resume_token,
|
||||
agent_execution_id, container_id, agentctl_url, agentctl_port, pid,
|
||||
worktree_id, worktree_path, worktree_branch, last_seen_at, error_message,
|
||||
@@ -315,7 +315,7 @@ func (r *Repository) DeleteExecutorRunningBySessionID(ctx context.Context, sessi
|
||||
|
||||
func (r *Repository) HasActiveTaskSessionsByExecutor(ctx context.Context, executorID string) (bool, error) {
|
||||
var exists int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT 1 FROM task_sessions
|
||||
WHERE executor_id = ? AND state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT')
|
||||
LIMIT 1
|
||||
|
||||
@@ -64,7 +64,7 @@ func (r *Repository) getGitSnapshotByOrder(ctx context.Context, sessionID, order
|
||||
WHERE session_id = ?
|
||||
ORDER BY created_at %s LIMIT 1
|
||||
`, orderDir)
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(query), sessionID).Scan(
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(query), sessionID).Scan(
|
||||
&snapshot.ID, &snapshot.SessionID, &snapshotType, &snapshot.Branch,
|
||||
&snapshot.RemoteBranch, &snapshot.HeadCommit, &snapshot.BaseCommit,
|
||||
&snapshot.Ahead, &snapshot.Behind, &filesJSON, &snapshot.TriggeredBy,
|
||||
@@ -116,7 +116,7 @@ func (r *Repository) GetGitSnapshotsBySession(ctx context.Context, sessionID str
|
||||
query += fmt.Sprintf(" LIMIT %d", limit)
|
||||
}
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), sessionID)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func (r *Repository) CreateSessionCommit(ctx context.Context, commit *models.Ses
|
||||
// GetSessionCommits retrieves all commits for a session, ordered by committed_at descending.
|
||||
// Returns an empty slice if no commits are found.
|
||||
func (r *Repository) GetSessionCommits(ctx context.Context, sessionID string) ([]*models.SessionCommit, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, session_id, commit_sha, parent_sha, author_name, author_email,
|
||||
commit_message, committed_at, pre_commit_snapshot_id, post_commit_snapshot_id,
|
||||
files_changed, insertions, deletions, created_at
|
||||
@@ -225,7 +225,7 @@ func (r *Repository) GetSessionCommits(ctx context.Context, sessionID string) ([
|
||||
func (r *Repository) GetLatestSessionCommit(ctx context.Context, sessionID string) (*models.SessionCommit, error) {
|
||||
commit := &models.SessionCommit{}
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, session_id, commit_sha, parent_sha, author_name, author_email,
|
||||
commit_message, committed_at, pre_commit_snapshot_id, post_commit_snapshot_id,
|
||||
files_changed, insertions, deletions, created_at
|
||||
|
||||
@@ -60,7 +60,7 @@ func (r *Repository) GetMessage(ctx context.Context, id string) (*models.Message
|
||||
var requestsInput int
|
||||
var messageType string
|
||||
var metadataJSON string
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at
|
||||
FROM task_session_messages WHERE id = ?
|
||||
`), id).Scan(&message.ID, &message.TaskSessionID, &message.TaskID, &message.TurnID, &message.AuthorType, &message.AuthorID, &message.Content, &requestsInput, &messageType, &metadataJSON, &message.CreatedAt)
|
||||
@@ -81,7 +81,7 @@ func (r *Repository) GetMessage(ctx context.Context, id string) (*models.Message
|
||||
|
||||
// ListMessages returns all messages for a session ordered by creation time.
|
||||
func (r *Repository) ListMessages(ctx context.Context, sessionID string) ([]*models.Message, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at
|
||||
FROM task_session_messages WHERE task_session_id = ? ORDER BY created_at ASC
|
||||
`), sessionID)
|
||||
@@ -132,7 +132,7 @@ func (r *Repository) ListMessagesPaginated(ctx context.Context, sessionID string
|
||||
return nil, false, err
|
||||
}
|
||||
query, args := buildListMessagesQuery(sessionID, opts, cursor, sortDir, limit)
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), args...)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
@@ -236,13 +236,13 @@ func (r *Repository) getMessageByMetadataField(ctx context.Context, sessionID, f
|
||||
var requestsInput int
|
||||
var messageType string
|
||||
var metadataJSON string
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at
|
||||
FROM task_session_messages WHERE task_session_id = ? AND %s = ?
|
||||
%s
|
||||
`, dialect.JSONExtract(drv, "metadata", fieldName), orderSuffix)
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(query), sessionID, fieldValue).Scan(&message.ID, &message.TaskSessionID, &message.TaskID, &message.TurnID, &message.AuthorType, &message.AuthorID,
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(query), sessionID, fieldValue).Scan(&message.ID, &message.TaskSessionID, &message.TaskID, &message.TurnID, &message.AuthorType, &message.AuthorID,
|
||||
&message.Content, &requestsInput, &messageType, &metadataJSON, &message.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -274,13 +274,13 @@ func (r *Repository) FindMessageByPendingID(ctx context.Context, pendingID strin
|
||||
var requestsInput int
|
||||
var messageType string
|
||||
var metadataJSON string
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at
|
||||
FROM task_session_messages WHERE %s = ?
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
`, dialect.JSONExtract(drv, "metadata", "pending_id"))
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(query), pendingID).Scan(&message.ID, &message.TaskSessionID, &message.TaskID, &message.TurnID, &message.AuthorType, &message.AuthorID,
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(query), pendingID).Scan(&message.ID, &message.TaskSessionID, &message.TaskID, &message.TurnID, &message.AuthorType, &message.AuthorID,
|
||||
&message.Content, &requestsInput, &messageType, &metadataJSON, &message.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -36,7 +36,7 @@ func (r *Repository) CreateTaskPlan(ctx context.Context, plan *models.TaskPlan)
|
||||
// GetTaskPlan retrieves a task plan by task ID.
|
||||
func (r *Repository) GetTaskPlan(ctx context.Context, taskID string) (*models.TaskPlan, error) {
|
||||
plan := &models.TaskPlan{}
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, title, content, created_by, created_at, updated_at
|
||||
FROM task_plans WHERE task_id = ?
|
||||
`), taskID).Scan(&plan.ID, &plan.TaskID, &plan.Title, &plan.Content, &plan.CreatedBy, &plan.CreatedAt, &plan.UpdatedAt)
|
||||
|
||||
@@ -37,7 +37,7 @@ func (r *Repository) CreateRepository(ctx context.Context, repository *models.Re
|
||||
func (r *Repository) GetRepository(ctx context.Context, id string) (*models.Repository, error) {
|
||||
repository := &models.Repository{}
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, name, source_type, local_path, provider, provider_repo_id, provider_owner,
|
||||
provider_name, default_branch, worktree_branch_prefix, pull_before_worktree, setup_script, cleanup_script, dev_script, created_at, updated_at, deleted_at
|
||||
FROM repositories WHERE id = ? AND deleted_at IS NULL
|
||||
@@ -94,7 +94,7 @@ func (r *Repository) DeleteRepository(ctx context.Context, id string) error {
|
||||
|
||||
// ListRepositories returns all repositories for a workspace
|
||||
func (r *Repository) ListRepositories(ctx context.Context, workspaceID string) ([]*models.Repository, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, name, source_type, local_path, provider, provider_repo_id, provider_owner,
|
||||
provider_name, default_branch, worktree_branch_prefix, pull_before_worktree, setup_script, cleanup_script, dev_script, created_at, updated_at, deleted_at
|
||||
FROM repositories WHERE workspace_id = ? AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
@@ -140,7 +140,7 @@ func (r *Repository) CreateRepositoryScript(ctx context.Context, script *models.
|
||||
// GetRepositoryScript retrieves a repository script by ID
|
||||
func (r *Repository) GetRepositoryScript(ctx context.Context, id string) (*models.RepositoryScript, error) {
|
||||
script := &models.RepositoryScript{}
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, repository_id, name, command, position, created_at, updated_at
|
||||
FROM repository_scripts WHERE id = ?
|
||||
`), id).Scan(&script.ID, &script.RepositoryID, &script.Name, &script.Command, &script.Position, &script.CreatedAt, &script.UpdatedAt)
|
||||
@@ -181,7 +181,7 @@ func (r *Repository) DeleteRepositoryScript(ctx context.Context, id string) erro
|
||||
|
||||
// ListRepositoryScripts returns all scripts for a repository
|
||||
func (r *Repository) ListRepositoryScripts(ctx context.Context, repositoryID string) ([]*models.RepositoryScript, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, repository_id, name, command, position, created_at, updated_at
|
||||
FROM repository_scripts WHERE repository_id = ? ORDER BY position
|
||||
`), repositoryID)
|
||||
|
||||
@@ -37,7 +37,7 @@ func (r *Repository) UpsertSessionFileReview(ctx context.Context, review *models
|
||||
|
||||
// GetSessionFileReviews retrieves all file reviews for a session.
|
||||
func (r *Repository) GetSessionFileReviews(ctx context.Context, sessionID string) ([]*models.SessionFileReview, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, session_id, file_path, reviewed, diff_hash, reviewed_at, created_at, updated_at
|
||||
FROM session_file_reviews
|
||||
WHERE session_id = ?
|
||||
|
||||
@@ -69,7 +69,7 @@ func scanTurnRow(row *sql.Row) (*models.Turn, error) {
|
||||
|
||||
// GetTurn retrieves a turn by ID
|
||||
func (r *Repository) GetTurn(ctx context.Context, id string) (*models.Turn, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_session_id, task_id, started_at, completed_at, metadata, created_at, updated_at
|
||||
FROM task_session_turns WHERE id = ?
|
||||
`), id)
|
||||
@@ -78,7 +78,7 @@ func (r *Repository) GetTurn(ctx context.Context, id string) (*models.Turn, erro
|
||||
|
||||
// GetActiveTurnBySessionID gets the currently active (non-completed) turn for a session
|
||||
func (r *Repository) GetActiveTurnBySessionID(ctx context.Context, sessionID string) (*models.Turn, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_session_id, task_id, started_at, completed_at, metadata, created_at, updated_at
|
||||
FROM task_session_turns
|
||||
WHERE task_session_id = ? AND completed_at IS NULL
|
||||
@@ -122,7 +122,7 @@ func (r *Repository) CompleteTurn(ctx context.Context, id string) error {
|
||||
|
||||
// ListTurnsBySession returns all turns for a session ordered by start time
|
||||
func (r *Repository) ListTurnsBySession(ctx context.Context, sessionID string) ([]*models.Turn, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_session_id, task_id, started_at, completed_at, metadata, created_at, updated_at
|
||||
FROM task_session_turns WHERE task_session_id = ? ORDER BY started_at ASC
|
||||
`), sessionID)
|
||||
@@ -289,7 +289,7 @@ func (r *Repository) scanTaskSession(ctx context.Context, row *sql.Row, noRowsEr
|
||||
|
||||
// GetTaskSession retrieves an agent session by ID
|
||||
func (r *Repository) GetTaskSession(ctx context.Context, id string) (*models.TaskSession, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -302,7 +302,7 @@ func (r *Repository) GetTaskSession(ctx context.Context, id string) (*models.Tas
|
||||
|
||||
// GetTaskSessionByTaskID retrieves the most recent agent session for a task
|
||||
func (r *Repository) GetTaskSessionByTaskID(ctx context.Context, taskID string) (*models.TaskSession, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -315,7 +315,7 @@ func (r *Repository) GetTaskSessionByTaskID(ctx context.Context, taskID string)
|
||||
|
||||
// GetActiveTaskSessionByTaskID retrieves the active (running/waiting) agent session for a task
|
||||
func (r *Repository) GetActiveTaskSessionByTaskID(ctx context.Context, taskID string) (*models.TaskSession, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -421,7 +421,7 @@ func (r *Repository) ClearSessionExecutionID(ctx context.Context, id string) err
|
||||
|
||||
// ListTaskSessions returns all agent sessions for a task
|
||||
func (r *Repository) ListTaskSessions(ctx context.Context, taskID string) ([]*models.TaskSession, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -450,7 +450,7 @@ func (r *Repository) ListTaskSessions(ctx context.Context, taskID string) ([]*mo
|
||||
|
||||
// ListActiveTaskSessions returns all active agent sessions across all tasks
|
||||
func (r *Repository) ListActiveTaskSessions(ctx context.Context) ([]*models.TaskSession, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -479,7 +479,7 @@ func (r *Repository) ListActiveTaskSessions(ctx context.Context) ([]*models.Task
|
||||
|
||||
// ListActiveTaskSessionsByTaskID returns all active agent sessions for a specific task
|
||||
func (r *Repository) ListActiveTaskSessionsByTaskID(ctx context.Context, taskID string) ([]*models.TaskSession, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -508,7 +508,7 @@ func (r *Repository) ListActiveTaskSessionsByTaskID(ctx context.Context, taskID
|
||||
|
||||
func (r *Repository) HasActiveTaskSessionsByAgentProfile(ctx context.Context, agentProfileID string) (bool, error) {
|
||||
var exists int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT 1 FROM task_sessions
|
||||
WHERE agent_profile_id = ? AND state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT')
|
||||
LIMIT 1
|
||||
@@ -521,7 +521,7 @@ func (r *Repository) HasActiveTaskSessionsByAgentProfile(ctx context.Context, ag
|
||||
|
||||
func (r *Repository) HasActiveTaskSessionsByEnvironment(ctx context.Context, environmentID string) (bool, error) {
|
||||
var exists int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT 1 FROM task_sessions
|
||||
WHERE environment_id = ? AND state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT')
|
||||
LIMIT 1
|
||||
@@ -534,7 +534,7 @@ func (r *Repository) HasActiveTaskSessionsByEnvironment(ctx context.Context, env
|
||||
|
||||
func (r *Repository) HasActiveTaskSessionsByRepository(ctx context.Context, repositoryID string) (bool, error) {
|
||||
var exists int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT 1
|
||||
FROM task_sessions s
|
||||
INNER JOIN task_repositories tr ON tr.task_id = s.task_id
|
||||
@@ -679,7 +679,7 @@ func (r *Repository) CreateTaskSessionWorktree(ctx context.Context, sessionWorkt
|
||||
}
|
||||
|
||||
func (r *Repository) ListTaskSessionWorktrees(ctx context.Context, sessionID string) ([]*models.TaskSessionWorktree, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.id, tsw.session_id, tsw.worktree_id, tsw.repository_id, tsw.position,
|
||||
tsw.worktree_path, tsw.worktree_branch, tsw.created_at
|
||||
@@ -733,7 +733,7 @@ func (r *Repository) DeleteTaskSessionWorktreesBySession(ctx context.Context, se
|
||||
|
||||
// GetPrimarySessionByTaskID retrieves the primary session for a task
|
||||
func (r *Repository) GetPrimarySessionByTaskID(ctx context.Context, taskID string) (*models.TaskSession, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
@@ -764,7 +764,7 @@ func (r *Repository) GetPrimarySessionIDsByTaskIDs(ctx context.Context, taskIDs
|
||||
WHERE task_id IN (%s) AND is_primary = 1
|
||||
`, strings.Join(placeholders, ","))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), args...)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -801,7 +801,7 @@ func (r *Repository) GetSessionCountsByTaskIDs(ctx context.Context, taskIDs []st
|
||||
GROUP BY task_id
|
||||
`, strings.Join(placeholders, ","))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), args...)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -839,7 +839,7 @@ func (r *Repository) GetPrimarySessionInfoByTaskIDs(ctx context.Context, taskIDs
|
||||
WHERE task_id IN (%s) AND is_primary = 1
|
||||
`, strings.Join(placeholders, ","))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), args...)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func (r *Repository) GetTask(ctx context.Context, id string) (*models.Task, erro
|
||||
task := &models.Task{}
|
||||
var metadata string
|
||||
var archivedAt sql.NullTime
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, workflow_id, workflow_step_id, title, description, state, priority, position, metadata, archived_at, created_at, updated_at
|
||||
FROM tasks WHERE id = ?
|
||||
`), id).Scan(&task.ID, &task.WorkspaceID, &task.WorkflowID, &task.WorkflowStepID, &task.Title, &task.Description, &task.State, &task.Priority, &task.Position, &metadata, &archivedAt, &task.CreatedAt, &task.UpdatedAt)
|
||||
@@ -111,7 +111,7 @@ func (r *Repository) DeleteTask(ctx context.Context, id string) error {
|
||||
|
||||
// ListTasks returns all non-archived tasks for a workflow
|
||||
func (r *Repository) ListTasks(ctx context.Context, workflowID string) ([]*models.Task, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, workflow_id, workflow_step_id, title, description, state, priority, position, metadata, archived_at, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE workflow_id = ? AND archived_at IS NULL
|
||||
@@ -128,7 +128,7 @@ func (r *Repository) ListTasks(ctx context.Context, workflowID string) ([]*model
|
||||
// CountTasksByWorkflow returns the number of non-archived tasks in a workflow
|
||||
func (r *Repository) CountTasksByWorkflow(ctx context.Context, workflowID string) (int, error) {
|
||||
var count int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`SELECT COUNT(*) FROM tasks WHERE workflow_id = ? AND archived_at IS NULL`), workflowID).Scan(&count)
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`SELECT COUNT(*) FROM tasks WHERE workflow_id = ? AND archived_at IS NULL`), workflowID).Scan(&count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (r *Repository) CountTasksByWorkflow(ctx context.Context, workflowID string
|
||||
// CountTasksByWorkflowStep returns the number of non-archived tasks in a workflow step
|
||||
func (r *Repository) CountTasksByWorkflowStep(ctx context.Context, stepID string) (int, error) {
|
||||
var count int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`SELECT COUNT(*) FROM tasks WHERE workflow_step_id = ? AND archived_at IS NULL`), stepID).Scan(&count)
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`SELECT COUNT(*) FROM tasks WHERE workflow_step_id = ? AND archived_at IS NULL`), stepID).Scan(&count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func (r *Repository) CountTasksByWorkflowStep(ctx context.Context, stepID string
|
||||
|
||||
// ListTasksByWorkflowStep returns all non-archived tasks in a workflow step
|
||||
func (r *Repository) ListTasksByWorkflowStep(ctx context.Context, workflowStepID string) ([]*models.Task, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, workflow_id, workflow_step_id, title, description, state, priority, position, metadata, archived_at, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE workflow_step_id = ? AND archived_at IS NULL ORDER BY position
|
||||
@@ -201,11 +201,11 @@ func (r *Repository) ListTasksByWorkspace(ctx context.Context, workspaceID strin
|
||||
// queryAllTasks fetches all tasks (no search) for a workspace with pagination.
|
||||
func (r *Repository) queryAllTasks(ctx context.Context, workspaceID, archiveFilter string, pageSize, offset int) (*sql.Rows, int, error) {
|
||||
var total int
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`SELECT COUNT(*) FROM tasks WHERE workspace_id = ?`+archiveFilter), workspaceID).Scan(&total)
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`SELECT COUNT(*) FROM tasks WHERE workspace_id = ?`+archiveFilter), workspaceID).Scan(&total)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, workflow_id, workflow_step_id, title, description, state, priority, position, metadata, archived_at, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE workspace_id = ?`+archiveFilter+`
|
||||
@@ -218,7 +218,7 @@ func (r *Repository) queryAllTasks(ctx context.Context, workspaceID, archiveFilt
|
||||
// searchTasks fetches tasks matching a search query for a workspace with pagination.
|
||||
func (r *Repository) searchTasks(ctx context.Context, workspaceID, query, archiveFilter string, pageSize, offset int, includeArchived bool) (*sql.Rows, int, error) {
|
||||
searchPattern := "%" + query + "%"
|
||||
like := dialect.Like(r.db.DriverName())
|
||||
like := dialect.Like(r.ro.DriverName())
|
||||
|
||||
tFilter := ""
|
||||
if !includeArchived {
|
||||
@@ -238,7 +238,7 @@ func (r *Repository) searchTasks(ctx context.Context, workspaceID, query, archiv
|
||||
)
|
||||
`, tFilter, like, like, like, like)
|
||||
var total int
|
||||
if err := r.db.QueryRowContext(ctx, r.db.Rebind(countQuery), workspaceID, searchPattern, searchPattern, searchPattern, searchPattern).Scan(&total); err != nil {
|
||||
if err := r.ro.QueryRowContext(ctx, r.ro.Rebind(countQuery), workspaceID, searchPattern, searchPattern, searchPattern, searchPattern).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ func (r *Repository) searchTasks(ctx context.Context, workspaceID, query, archiv
|
||||
ORDER BY t.updated_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`, tFilter, like, like, like, like)
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(selectQuery), workspaceID, searchPattern, searchPattern, searchPattern, searchPattern, pageSize, offset)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(selectQuery), workspaceID, searchPattern, searchPattern, searchPattern, searchPattern, pageSize, offset)
|
||||
return rows, total, err
|
||||
}
|
||||
|
||||
@@ -311,7 +311,7 @@ func (r *Repository) ArchiveTask(ctx context.Context, id string) error {
|
||||
|
||||
// ListTasksForAutoArchive returns tasks eligible for auto-archiving based on workflow step settings
|
||||
func (r *Repository) ListTasksForAutoArchive(ctx context.Context) ([]*models.Task, error) {
|
||||
drv := r.db.DriverName()
|
||||
drv := r.ro.DriverName()
|
||||
query := fmt.Sprintf(`
|
||||
SELECT t.id, t.workspace_id, t.workflow_id, t.workflow_step_id, t.title, t.description, t.state, t.priority, t.position, t.metadata, t.archived_at, t.created_at, t.updated_at
|
||||
FROM tasks t
|
||||
@@ -320,7 +320,7 @@ func (r *Repository) ListTasksForAutoArchive(ctx context.Context) ([]*models.Tas
|
||||
AND t.archived_at IS NULL
|
||||
AND t.updated_at <= %s
|
||||
`, dialect.NowMinusHours(drv, "ws.auto_archive_after_hours"))
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query))
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func (r *Repository) GetTaskRepository(ctx context.Context, id string) (*models.
|
||||
taskRepo := &models.TaskRepository{}
|
||||
var metadataJSON string
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, repository_id, base_branch, position, metadata, created_at, updated_at
|
||||
FROM task_repositories WHERE id = ?
|
||||
`), id).Scan(
|
||||
@@ -68,7 +68,7 @@ func (r *Repository) GetTaskRepository(ctx context.Context, id string) (*models.
|
||||
|
||||
// ListTaskRepositories returns all repository links for a task
|
||||
func (r *Repository) ListTaskRepositories(ctx context.Context, taskID string) ([]*models.TaskRepository, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, task_id, repository_id, base_branch, position, metadata, created_at, updated_at
|
||||
FROM task_repositories
|
||||
WHERE task_id = ?
|
||||
|
||||
@@ -51,7 +51,7 @@ func (r *Repository) GetWorkflow(ctx context.Context, id string) (*models.Workfl
|
||||
workflow := &models.Workflow{}
|
||||
var workflowTemplateID sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, workspace_id, name, description, workflow_template_id, created_at, updated_at
|
||||
FROM workflows WHERE id = ?
|
||||
`), id).Scan(&workflow.ID, &workflow.WorkspaceID, &workflow.Name, &workflow.Description, &workflowTemplateID, &workflow.CreatedAt, &workflow.UpdatedAt)
|
||||
@@ -113,7 +113,7 @@ func (r *Repository) ListWorkflows(ctx context.Context, workspaceID string) ([]*
|
||||
}
|
||||
query += " ORDER BY created_at DESC"
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(query), args...)
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func (r *Repository) GetWorkspace(ctx context.Context, id string) (*models.Works
|
||||
var defaultEnvironmentID sql.NullString
|
||||
var defaultAgentProfileID sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, description, owner_id, default_executor_id, default_environment_id, default_agent_profile_id, created_at, updated_at
|
||||
FROM workspaces WHERE id = ?
|
||||
`), id).Scan(
|
||||
@@ -116,7 +116,7 @@ func (r *Repository) DeleteWorkspace(ctx context.Context, id string) error {
|
||||
|
||||
// ListWorkspaces returns all workspaces
|
||||
func (r *Repository) ListWorkspaces(ctx context.Context) ([]*models.Workspace, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, description, owner_id, default_executor_id, default_environment_id, default_agent_profile_id, created_at, updated_at
|
||||
FROM workspaces ORDER BY created_at DESC
|
||||
`)
|
||||
|
||||
@@ -315,7 +315,7 @@ func TestSQLiteRepository_Persistence(t *testing.T) {
|
||||
t.Fatalf("failed to open SQLite database: %v", err)
|
||||
}
|
||||
sqlxDB1 := sqlx.NewDb(dbConn1, "sqlite3")
|
||||
repo1, err := sqlite.NewWithDB(sqlxDB1)
|
||||
repo1, err := sqlite.NewWithDB(sqlxDB1, sqlxDB1)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create first repository: %v", err)
|
||||
}
|
||||
@@ -336,7 +336,7 @@ func TestSQLiteRepository_Persistence(t *testing.T) {
|
||||
t.Fatalf("failed to open SQLite database: %v", err)
|
||||
}
|
||||
sqlxDB2 := sqlx.NewDb(dbConn2, "sqlite3")
|
||||
repo2, err := sqlite.NewWithDB(sqlxDB2)
|
||||
repo2, err := sqlite.NewWithDB(sqlxDB2, sqlxDB2)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create second repository: %v", err)
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ func newDiscoveryService(t *testing.T, root string) *Service {
|
||||
t.Fatalf("failed to open test database: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB)
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test repository: %v", err)
|
||||
}
|
||||
|
||||
@@ -79,12 +79,12 @@ func createTestService(t *testing.T) (*Service, *MockEventBus, repository.Reposi
|
||||
t.Fatalf("failed to open test database: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB)
|
||||
repoImpl, cleanup, err := repository.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test repository: %v", err)
|
||||
}
|
||||
repo := repoImpl
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB); err != nil {
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB, sqlxDB); err != nil {
|
||||
t.Fatalf("failed to init worktree store: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
|
||||
@@ -2,9 +2,9 @@ package store
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// Provide creates the SQLite user store using the shared database connection.
|
||||
func Provide(db *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(db)
|
||||
// Provide creates the SQLite user store using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB) (*sqliteRepository, func() error, error) {
|
||||
repo, err := newSQLiteRepositoryWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -17,21 +17,22 @@ const (
|
||||
)
|
||||
|
||||
type sqliteRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
ownsDB bool
|
||||
}
|
||||
|
||||
var _ Repository = (*sqliteRepository)(nil)
|
||||
|
||||
func newSQLiteRepositoryWithDB(dbConn *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(dbConn, false)
|
||||
func newSQLiteRepositoryWithDB(writer, reader *sqlx.DB) (*sqliteRepository, error) {
|
||||
return newSQLiteRepository(writer, reader, false)
|
||||
}
|
||||
|
||||
func newSQLiteRepository(dbConn *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: dbConn, ownsDB: ownsDB}
|
||||
func newSQLiteRepository(writer, reader *sqlx.DB, ownsDB bool) (*sqliteRepository, error) {
|
||||
repo := &sqliteRepository{db: writer, ro: reader, ownsDB: ownsDB}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := dbConn.Close(); closeErr != nil {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
}
|
||||
}
|
||||
@@ -84,7 +85,7 @@ func (r *sqliteRepository) Close() error {
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetUser(ctx context.Context, id string) (*models.User, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, email, created_at, updated_at
|
||||
FROM users WHERE id = ?
|
||||
`), id)
|
||||
@@ -96,7 +97,7 @@ func (r *sqliteRepository) GetDefaultUser(ctx context.Context) (*models.User, er
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) GetUserSettings(ctx context.Context, userID string) (*models.UserSettings, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT settings, updated_at
|
||||
FROM users WHERE id = ?
|
||||
`), userID)
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"github.com/kandev/kandev/internal/workflow/service"
|
||||
)
|
||||
|
||||
// Provide creates the workflow repository and service using the shared database connection.
|
||||
func Provide(db *sqlx.DB, log *logger.Logger) (*repository.Repository, *service.Service, func() error, error) {
|
||||
repo, err := repository.NewWithDB(db)
|
||||
// Provide creates the workflow repository and service using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB, log *logger.Logger) (*repository.Repository, *service.Service, func() error, error) {
|
||||
repo, err := repository.NewWithDB(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
@@ -17,12 +17,13 @@ import (
|
||||
|
||||
// Repository provides SQLite-based workflow storage operations.
|
||||
type Repository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
}
|
||||
|
||||
// NewWithDB creates a new SQLite repository with an existing database connection.
|
||||
func NewWithDB(db *sqlx.DB) (*Repository, error) {
|
||||
repo := &Repository{db: db}
|
||||
// NewWithDB creates a new SQLite repository with existing database connections.
|
||||
func NewWithDB(writer, reader *sqlx.DB) (*Repository, error) {
|
||||
repo := &Repository{db: writer, ro: reader}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize workflow schema: %w", err)
|
||||
}
|
||||
@@ -493,7 +494,7 @@ func (r *Repository) GetTemplate(ctx context.Context, id string) (*models.Workfl
|
||||
var stepsJSON string
|
||||
var isSystem int
|
||||
|
||||
err := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
err := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, name, description, is_system, steps, created_at, updated_at
|
||||
FROM workflow_templates WHERE id = ?
|
||||
`), id).Scan(&template.ID, &template.Name, &template.Description, &isSystem, &stepsJSON, &template.CreatedAt, &template.UpdatedAt)
|
||||
@@ -575,7 +576,7 @@ func scanTemplateRows(rows *sql.Rows) ([]*models.WorkflowTemplate, error) {
|
||||
|
||||
// ListTemplates returns all workflow templates.
|
||||
func (r *Repository) ListTemplates(ctx context.Context) ([]*models.WorkflowTemplate, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, description, is_system, steps, created_at, updated_at
|
||||
FROM workflow_templates
|
||||
ORDER BY is_system DESC,
|
||||
@@ -596,7 +597,7 @@ func (r *Repository) ListTemplates(ctx context.Context) ([]*models.WorkflowTempl
|
||||
|
||||
// GetSystemTemplates returns only system workflow templates.
|
||||
func (r *Repository) GetSystemTemplates(ctx context.Context) ([]*models.WorkflowTemplate, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
rows, err := r.ro.QueryContext(ctx, `
|
||||
SELECT id, name, description, is_system, steps, created_at, updated_at
|
||||
FROM workflow_templates WHERE is_system = 1 ORDER BY name ASC
|
||||
`)
|
||||
@@ -677,7 +678,7 @@ const stepSelectColumns = `id, workflow_id, name, position, color, prompt, event
|
||||
|
||||
// GetStep retrieves a workflow step by ID.
|
||||
func (r *Repository) GetStep(ctx context.Context, id string) (*models.WorkflowStep, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT `+stepSelectColumns+`
|
||||
FROM workflow_steps WHERE id = ?
|
||||
`), id)
|
||||
@@ -733,7 +734,7 @@ func (r *Repository) ClearStartStepFlag(ctx context.Context, workflowID, exceptS
|
||||
// GetStartStep returns the step marked as is_start_step for a workflow.
|
||||
// Returns nil if no step is marked.
|
||||
func (r *Repository) GetStartStep(ctx context.Context, workflowID string) (*models.WorkflowStep, error) {
|
||||
row := r.db.QueryRowContext(ctx, r.db.Rebind(`
|
||||
row := r.ro.QueryRowContext(ctx, r.ro.Rebind(`
|
||||
SELECT `+stepSelectColumns+`
|
||||
FROM workflow_steps WHERE workflow_id = ? AND is_start_step = 1
|
||||
LIMIT 1
|
||||
@@ -796,7 +797,7 @@ func (r *Repository) DeleteStep(ctx context.Context, id string) error {
|
||||
|
||||
// ListStepsByWorkflow returns all workflow steps for a workflow.
|
||||
func (r *Repository) ListStepsByWorkflow(ctx context.Context, workflowID string) ([]*models.WorkflowStep, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT `+stepSelectColumns+`
|
||||
FROM workflow_steps WHERE workflow_id = ? ORDER BY position
|
||||
`), workflowID)
|
||||
@@ -854,7 +855,7 @@ func (r *Repository) CreateHistory(ctx context.Context, history *models.SessionS
|
||||
|
||||
// ListHistoryBySession returns all step history entries for a session.
|
||||
func (r *Repository) ListHistoryBySession(ctx context.Context, sessionID string) ([]*models.SessionStepHistory, error) {
|
||||
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`
|
||||
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(`
|
||||
SELECT id, session_id, from_step_id, to_step_id, trigger, actor_id, metadata, created_at
|
||||
FROM session_step_history WHERE session_id = ? ORDER BY created_at
|
||||
`), sessionID)
|
||||
|
||||
@@ -32,7 +32,7 @@ func setupTestService(t *testing.T) (*Service, *sqlx.DB) {
|
||||
)`)
|
||||
require.NoError(t, err)
|
||||
|
||||
repo, err := repository.NewWithDB(sqlxDB)
|
||||
repo, err := repository.NewWithDB(sqlxDB, sqlxDB)
|
||||
require.NoError(t, err)
|
||||
|
||||
log, _ := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "console"})
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
)
|
||||
|
||||
// Provide creates the worktree manager using the shared database connection.
|
||||
func Provide(db *sqlx.DB, cfg *config.Config, log *logger.Logger) (*Manager, func() error, error) {
|
||||
store, err := NewSQLiteStore(db)
|
||||
// Provide creates the worktree manager using separate writer and reader pools.
|
||||
func Provide(writer, reader *sqlx.DB, cfg *config.Config, log *logger.Logger) (*Manager, func() error, error) {
|
||||
store, err := NewSQLiteStore(writer, reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -12,13 +12,14 @@ import (
|
||||
|
||||
// SQLiteStore implements Store interface using SQLite.
|
||||
type SQLiteStore struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
}
|
||||
|
||||
// NewSQLiteStore creates a new SQLite-backed worktree store.
|
||||
// It uses the provided sqlx.DB connection and ensures the task_session_worktrees table exists.
|
||||
func NewSQLiteStore(db *sqlx.DB) (*SQLiteStore, error) {
|
||||
store := &SQLiteStore{db: db}
|
||||
// It uses the provided writer and reader connections and ensures the task_session_worktrees table exists.
|
||||
func NewSQLiteStore(writer, reader *sqlx.DB) (*SQLiteStore, error) {
|
||||
store := &SQLiteStore{db: writer, ro: reader}
|
||||
if err := store.initSchema(); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize worktree schema: %w", err)
|
||||
}
|
||||
@@ -101,7 +102,7 @@ func (s *SQLiteStore) GetWorktreeByID(ctx context.Context, id string) (*Worktree
|
||||
var mergedAt, deletedAt sql.NullTime
|
||||
var repositoryPath, baseBranch sql.NullString
|
||||
|
||||
err := s.db.QueryRowContext(ctx, s.db.Rebind(`
|
||||
err := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
@@ -204,7 +205,7 @@ func scanWorktreeRow(row *sql.Row) (*Worktree, error) {
|
||||
|
||||
// GetWorktreeBySessionID retrieves the worktree by session ID.
|
||||
func (s *SQLiteStore) GetWorktreeBySessionID(ctx context.Context, sessionID string) (*Worktree, error) {
|
||||
row := s.db.QueryRowContext(ctx, s.db.Rebind(`
|
||||
row := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
@@ -230,7 +231,7 @@ func (s *SQLiteStore) GetWorktreeBySessionID(ctx context.Context, sessionID stri
|
||||
// GetWorktreeByTaskID retrieves the most recent active worktree by task ID.
|
||||
// Since multiple worktrees can exist per task, this returns the most recently created active one.
|
||||
func (s *SQLiteStore) GetWorktreeByTaskID(ctx context.Context, taskID string) (*Worktree, error) {
|
||||
row := s.db.QueryRowContext(ctx, s.db.Rebind(`
|
||||
row := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
@@ -256,7 +257,7 @@ func (s *SQLiteStore) GetWorktreeByTaskID(ctx context.Context, taskID string) (*
|
||||
|
||||
// GetWorktreesByTaskID retrieves all worktrees for a task.
|
||||
func (s *SQLiteStore) GetWorktreesByTaskID(ctx context.Context, taskID string) ([]*Worktree, error) {
|
||||
rows, err := s.db.QueryContext(ctx, s.db.Rebind(`
|
||||
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
@@ -286,7 +287,7 @@ func (s *SQLiteStore) GetWorktreesByTaskID(ctx context.Context, taskID string) (
|
||||
|
||||
// GetWorktreesByRepositoryID retrieves all worktrees for a repository.
|
||||
func (s *SQLiteStore) GetWorktreesByRepositoryID(ctx context.Context, repoID string) ([]*Worktree, error) {
|
||||
rows, err := s.db.QueryContext(ctx, s.db.Rebind(`
|
||||
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
@@ -367,7 +368,7 @@ func (s *SQLiteStore) DeleteWorktree(ctx context.Context, id string) error {
|
||||
|
||||
// ListActiveWorktrees returns all worktrees with status 'active'.
|
||||
func (s *SQLiteStore) ListActiveWorktrees(ctx context.Context) ([]*Worktree, error) {
|
||||
rows, err := s.db.QueryContext(ctx, s.db.Rebind(`
|
||||
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
||||
SELECT
|
||||
tsw.worktree_id,
|
||||
tsw.session_id,
|
||||
|
||||
Reference in New Issue
Block a user