fix: keep large database startup alive (#3624)
This commit is contained in:
@@ -556,7 +556,7 @@ func (r *Repository) deleteClaimedMessageAttachments(
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
queueTablePresent, err := r.tableExists("queued_messages")
|
||||
queueTablePresent, err := r.tableExistsContext(ctx, "queued_messages")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("probe queue attachment references: %w", err)
|
||||
}
|
||||
|
||||
@@ -108,6 +108,13 @@ func (r *Repository) nowUTC() time.Time {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
|
||||
func (r *Repository) migrationContext() context.Context {
|
||||
if r.migrate == nil {
|
||||
return context.Background()
|
||||
}
|
||||
return r.migrate.Context()
|
||||
}
|
||||
|
||||
// SetTaskQueuePurger registers the orchestrator-owned queue cleanup for
|
||||
// in-memory queues. SQLite queues are also purged in the task mutation
|
||||
// transaction; this callback keeps the explicitly ephemeral queue equivalent.
|
||||
@@ -178,18 +185,37 @@ func (r *Repository) notifyTaskSessionQueuePurged(ctx context.Context, taskID, s
|
||||
|
||||
// NewWithDB creates a new SQLite repository with an existing database connection (shared ownership).
|
||||
func NewWithDB(writer, reader *sqlx.DB, log *logger.Logger) (*Repository, error) {
|
||||
return newRepository(writer, reader, log, false)
|
||||
return NewWithDBContext(context.Background(), writer, reader, log)
|
||||
}
|
||||
|
||||
// NewWithDBContext creates a repository with an existing database connection
|
||||
// while observing cancellation at schema-step boundaries and in
|
||||
// context-aware migration statements. A statement that has already entered
|
||||
// the database driver can finish before that driver reports cancellation; the
|
||||
// caller retains pool ownership until this constructor returns.
|
||||
func NewWithDBContext(ctx context.Context, writer, reader *sqlx.DB, log *logger.Logger) (*Repository, error) {
|
||||
return newRepositoryContext(ctx, writer, reader, log, false)
|
||||
}
|
||||
|
||||
func newRepository(writer, reader *sqlx.DB, log *logger.Logger, ownsDB bool) (*Repository, error) {
|
||||
return newRepositoryContext(context.Background(), writer, reader, log, ownsDB)
|
||||
}
|
||||
|
||||
func newRepositoryContext(ctx context.Context, writer, reader *sqlx.DB, log *logger.Logger, ownsDB bool) (*Repository, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repo := &Repository{
|
||||
db: writer,
|
||||
ro: reader,
|
||||
ownsDB: ownsDB,
|
||||
log: log,
|
||||
migrate: db.NewRequiredMigrateLogger(writer, log),
|
||||
migrate: db.NewRequiredMigrateLoggerContext(writer, log, ctx),
|
||||
}
|
||||
if err := repo.initSchema(); err != nil {
|
||||
if err := repo.initSchemaContext(ctx); err != nil {
|
||||
if ownsDB {
|
||||
if closeErr := writer.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to close database after schema error: %w", closeErr)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -391,7 +392,8 @@ func (r *Repository) runMigrations() error {
|
||||
return nil
|
||||
}
|
||||
func (r *Repository) backfillTaskSessionQueueIncarnations() error {
|
||||
rows, err := r.db.Queryx(`SELECT id FROM task_sessions WHERE queue_incarnation_id = ''`)
|
||||
ctx := r.migrationContext()
|
||||
rows, err := r.db.QueryxContext(ctx, `SELECT id FROM task_sessions WHERE queue_incarnation_id = ''`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list sessions missing queue incarnation: %w", err)
|
||||
}
|
||||
@@ -411,7 +413,7 @@ func (r *Repository) backfillTaskSessionQueueIncarnations() error {
|
||||
return fmt.Errorf("iterate sessions missing queue incarnation: %w", err)
|
||||
}
|
||||
for _, sessionID := range sessionIDs {
|
||||
if _, err := r.db.Exec(r.db.Rebind(`
|
||||
if _, err := r.db.ExecContext(ctx, r.db.Rebind(`
|
||||
UPDATE task_sessions
|
||||
SET queue_incarnation_id = ?
|
||||
WHERE id = ? AND queue_incarnation_id = ''
|
||||
@@ -440,7 +442,8 @@ func (r *Repository) backfillPromptSeq() error {
|
||||
AND (%s < %s OR (%s = %s AND u.id <= task_session_messages.id))
|
||||
)
|
||||
WHERE author_type = 'user' AND prompt_seq = 0`, nmU, nmM, nmU, nmM)
|
||||
if _, err := r.db.Exec(update); err != nil {
|
||||
ctx := r.migrationContext()
|
||||
if _, err := r.db.ExecContext(ctx, update); err != nil {
|
||||
return fmt.Errorf("backfill prompt_seq: %w", err)
|
||||
}
|
||||
seed := `
|
||||
@@ -449,7 +452,7 @@ func (r *Repository) backfillPromptSeq() error {
|
||||
WHERE author_type = 'user' AND prompt_seq > 0
|
||||
GROUP BY task_session_id
|
||||
ON CONFLICT(task_session_id) DO NOTHING`
|
||||
if _, err := r.db.Exec(seed); err != nil {
|
||||
if _, err := r.db.ExecContext(ctx, seed); err != nil {
|
||||
return fmt.Errorf("seed prompt sequence counters: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -602,14 +605,14 @@ func jsonBoolToInt(postgres bool, column, parent, key string) string {
|
||||
// the initial implementation, reconciles legacy bootstrap duplicates, then
|
||||
// enforces uniqueness only for the two hidden workflows created by this flow.
|
||||
func (r *Repository) ensureImproveKandevWorkflowTemplateUniqueness() error {
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin improve kandev workflow migration: %w", err)
|
||||
}
|
||||
// Keep this template list synchronized with every hidden Improve Kandev
|
||||
// workflow bootstrapped by internal/improvekandev. Add new IDs to both this
|
||||
// reconciliation query and the partial-index predicate below.
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `
|
||||
UPDATE workflows
|
||||
SET workflow_template_id = ''
|
||||
WHERE id IN (
|
||||
@@ -631,11 +634,11 @@ func (r *Repository) ensureImproveKandevWorkflowTemplateUniqueness() error {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("reconcile improve kandev workflow duplicates: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`DROP INDEX IF EXISTS uniq_workflows_workspace_template_hidden`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `DROP INDEX IF EXISTS uniq_workflows_workspace_template_hidden`); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("drop broad workflow template index: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS uniq_improve_kandev_workflows
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `CREATE UNIQUE INDEX IF NOT EXISTS uniq_improve_kandev_workflows
|
||||
ON workflows(workspace_id, workflow_template_id, hidden)
|
||||
WHERE workflow_template_id IN ('improve-kandev', 'report-kandev-issue')`); err != nil {
|
||||
_ = tx.Rollback()
|
||||
@@ -651,7 +654,7 @@ func (r *Repository) ensureImproveKandevWorkflowTemplateUniqueness() error {
|
||||
// folder attachments existed. CREATE TABLE/INDEX IF NOT EXISTS is replay-safe
|
||||
// on SQLite and Postgres.
|
||||
func (r *Repository) ensureTaskWorkspaceFoldersSchema() error {
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_workspace_folders (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL,
|
||||
@@ -676,7 +679,7 @@ func (r *Repository) ensureTaskWorkspaceFoldersSchema() error {
|
||||
// existed. It replays the same DDL as the schema-init step; CREATE TABLE/INDEX
|
||||
// IF NOT EXISTS is replay-safe on SQLite and Postgres.
|
||||
func (r *Repository) ensureRepositorySetsSchema() error {
|
||||
if _, err := r.db.Exec(repositorySetsSchemaDDL); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), repositorySetsSchemaDDL); err != nil {
|
||||
return fmt.Errorf("create repository sets schema: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -685,7 +688,7 @@ func (r *Repository) ensureRepositorySetsSchema() error {
|
||||
// ensureRepositoryBranchPoliciesSchema replays the policy DDL for databases
|
||||
// created before repository branch policies existed.
|
||||
func (r *Repository) ensureRepositoryBranchPoliciesSchema() error {
|
||||
if _, err := r.db.Exec(repositoryBranchPoliciesSchemaDDL); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), repositoryBranchPoliciesSchemaDDL); err != nil {
|
||||
return fmt.Errorf("create repository branch policies schema: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -705,7 +708,7 @@ func (r *Repository) recreateTable(tableName, triggerPhrase string, statements [
|
||||
}
|
||||
|
||||
var tableSql string
|
||||
err := r.db.QueryRow(`SELECT sql FROM sqlite_master WHERE type='table' AND name=?`, tableName).Scan(&tableSql)
|
||||
err := r.db.QueryRowContext(r.migrationContext(), `SELECT sql FROM sqlite_master WHERE type='table' AND name=?`, tableName).Scan(&tableSql)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil // Table doesn't exist yet; migration not applicable
|
||||
}
|
||||
@@ -716,19 +719,21 @@ func (r *Repository) recreateTable(tableName, triggerPhrase string, statements [
|
||||
return false, nil // Trigger phrase absent; migration already applied or not needed
|
||||
}
|
||||
|
||||
if _, err := r.db.Exec(`PRAGMA foreign_keys=OFF`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `PRAGMA foreign_keys=OFF`); err != nil {
|
||||
return false, fmt.Errorf("disable foreign keys: %w", err)
|
||||
}
|
||||
defer func() { _, _ = r.db.Exec(`PRAGMA foreign_keys=ON`) }()
|
||||
// This restoration is cleanup, so it must not inherit a canceled startup
|
||||
// context and leave the shared SQLite writer with foreign-key checks off.
|
||||
defer func() { _, _ = r.db.ExecContext(context.Background(), `PRAGMA foreign_keys=ON`) }()
|
||||
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("begin migration transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
for _, stmt := range statements {
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), stmt); err != nil {
|
||||
return false, fmt.Errorf("migration %s failed: %w", tableName, err)
|
||||
}
|
||||
}
|
||||
@@ -806,7 +811,7 @@ func (r *Repository) backfillExecutorsRunningFromTaskSessions() error {
|
||||
// Check whether task_sessions still has the column. If migration already ran,
|
||||
// the column is gone and there's nothing to backfill.
|
||||
var tableSql string
|
||||
if err := r.db.QueryRow(`SELECT sql FROM sqlite_master WHERE type='table' AND name='task_sessions'`).Scan(&tableSql); err != nil {
|
||||
if err := r.db.QueryRowContext(r.migrationContext(), `SELECT sql FROM sqlite_master WHERE type='table' AND name='task_sessions'`).Scan(&tableSql); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
@@ -820,7 +825,7 @@ func (r *Repository) backfillExecutorsRunningFromTaskSessions() error {
|
||||
// SELECT … LEFT JOIN to find sessions with execution data but no executors_running row.
|
||||
// Insert with the minimum field set; runtime/status are best-effort defaults
|
||||
// (subsequent Launch / Resume will overwrite via the lifecycle manager's persistence).
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
INSERT INTO executors_running (
|
||||
id, session_id, task_id, executor_id, runtime, status, resumable,
|
||||
resume_token, last_message_uuid, agent_execution_id, container_id,
|
||||
@@ -887,7 +892,7 @@ const commitCaptureActivatedAtMetaKey = "commit_capture_activated_at"
|
||||
// exist, so a failure here must abort boot (propagated below) rather than
|
||||
// leave every future commit insert failing silently forever.
|
||||
func (r *Repository) migrateSessionCommitsDedupeAndActivation() error {
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
DELETE FROM task_session_commits
|
||||
WHERE id NOT IN (
|
||||
SELECT id FROM (
|
||||
@@ -903,7 +908,7 @@ func (r *Repository) migrateSessionCommitsDedupeAndActivation() error {
|
||||
`); err != nil {
|
||||
return fmt.Errorf("dedupe task_session_commits: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(
|
||||
if _, err := r.db.ExecContext(r.migrationContext(),
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS uniq_session_commits_session_sha ON task_session_commits(session_id, commit_sha)`,
|
||||
); err != nil {
|
||||
return fmt.Errorf("create uniq_session_commits_session_sha: %w", err)
|
||||
@@ -916,7 +921,7 @@ func (r *Repository) migrateSessionCommitsDedupeAndActivation() error {
|
||||
// production (persistence.Provide creates it before opening any
|
||||
// repository), but repo-level tests build a bare DB via NewWithDB where
|
||||
// it does not, so recreate it defensively.
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS kandev_meta (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL DEFAULT ''
|
||||
@@ -924,7 +929,7 @@ func (r *Repository) migrateSessionCommitsDedupeAndActivation() error {
|
||||
return fmt.Errorf("ensure kandev_meta: %w", err)
|
||||
}
|
||||
now := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if _, err := r.db.Exec(r.db.Rebind(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), r.db.Rebind(`
|
||||
INSERT INTO kandev_meta (key, value) VALUES (?, ?)
|
||||
ON CONFLICT(key) DO NOTHING
|
||||
`), commitCaptureActivatedAtMetaKey, now); err != nil {
|
||||
@@ -1081,10 +1086,10 @@ func (r *Repository) migrateTaskEnvironmentReposAllowMultiBranch() error {
|
||||
}
|
||||
|
||||
func (r *Repository) migrateTaskEnvironmentReposAllowMultiBranchPostgres() error {
|
||||
if _, err := r.db.Exec(`ALTER TABLE task_environment_repos ADD COLUMN IF NOT EXISTS branch_slug TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `ALTER TABLE task_environment_repos ADD COLUMN IF NOT EXISTS branch_slug TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
return fmt.Errorf("add task_environment_repos.branch_slug: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
DO $$
|
||||
DECLARE
|
||||
old_constraint_name text;
|
||||
@@ -1254,13 +1259,13 @@ func (r *Repository) migrateSessionsRemoveWorkflowStepID() error {
|
||||
// Runs before ensureTaskEnvironmentTaskUniqueIndex so the unique constraint
|
||||
// can be added cleanly. Idempotent — a no-op once the data is healed.
|
||||
func (r *Repository) healDuplicateTaskEnvironments() error {
|
||||
tx, err := r.db.Begin()
|
||||
tx, err := r.db.BeginTx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("heal duplicate envs: begin tx: %w", err)
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck
|
||||
|
||||
rows, err := tx.Query(`
|
||||
rows, err := tx.QueryContext(r.migrationContext(), `
|
||||
SELECT task_id
|
||||
FROM task_environments
|
||||
GROUP BY task_id
|
||||
@@ -1285,7 +1290,7 @@ func (r *Repository) healDuplicateTaskEnvironments() error {
|
||||
_ = rows.Close()
|
||||
|
||||
for _, taskID := range taskIDs {
|
||||
if err := healDuplicateTaskEnvForTask(tx, taskID); err != nil {
|
||||
if err := healDuplicateTaskEnvForTask(r.migrationContext(), tx, taskID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1294,9 +1299,9 @@ func (r *Repository) healDuplicateTaskEnvironments() error {
|
||||
|
||||
// healDuplicateTaskEnvForTask keeps the most recently updated env for a task,
|
||||
// re-points sessions on the loser rows to the winner, then deletes losers.
|
||||
func healDuplicateTaskEnvForTask(tx *sql.Tx, taskID string) error {
|
||||
func healDuplicateTaskEnvForTask(ctx context.Context, tx *sql.Tx, taskID string) error {
|
||||
var winnerID string
|
||||
if err := tx.QueryRow(`
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT id FROM task_environments
|
||||
WHERE task_id = ?
|
||||
ORDER BY updated_at DESC, created_at DESC
|
||||
@@ -1305,7 +1310,7 @@ func healDuplicateTaskEnvForTask(tx *sql.Tx, taskID string) error {
|
||||
return fmt.Errorf("heal duplicate envs: find winner for task %s: %w", taskID, err)
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE task_sessions
|
||||
SET task_environment_id = ?
|
||||
WHERE task_id = ?
|
||||
@@ -1314,7 +1319,7 @@ func healDuplicateTaskEnvForTask(tx *sql.Tx, taskID string) error {
|
||||
return fmt.Errorf("heal duplicate envs: relink sessions for task %s: %w", taskID, err)
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM task_environments
|
||||
WHERE task_id = ?
|
||||
AND id != ?
|
||||
@@ -1329,7 +1334,7 @@ func healDuplicateTaskEnvForTask(tx *sql.Tx, taskID string) error {
|
||||
// instead of silently producing two rows for the same task. Must run AFTER
|
||||
// healDuplicateTaskEnvironments, which collapses any pre-existing duplicates.
|
||||
func (r *Repository) ensureTaskEnvironmentTaskUniqueIndex() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uniq_task_environments_task_id
|
||||
ON task_environments(task_id)
|
||||
`)
|
||||
@@ -1351,7 +1356,7 @@ func (r *Repository) healSessionTaskEnvironmentIDs() error {
|
||||
// ensureTaskEnvironmentTaskUniqueIndex guarantees ≤1 row per task at
|
||||
// runtime, but the SQL reads as non-deterministic in isolation. Belt
|
||||
// and suspenders.
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
UPDATE task_sessions
|
||||
SET task_environment_id = (
|
||||
SELECT te.id FROM task_environments te WHERE te.task_id = task_sessions.task_id LIMIT 1
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -14,6 +15,13 @@ import (
|
||||
// table so the call list stays readable as new schema/migration steps are
|
||||
// added without growing the function's cyclomatic complexity.
|
||||
func (r *Repository) initSchema() error {
|
||||
return r.initSchemaContext(context.Background())
|
||||
}
|
||||
|
||||
func (r *Repository) initSchemaContext(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
steps := []func() error{
|
||||
r.initDesktopDiscoverySchema,
|
||||
r.initCoreSchema,
|
||||
@@ -51,10 +59,22 @@ func (r *Repository) initSchema() error {
|
||||
r.ensureMessageMetadataIndexes,
|
||||
r.ensurePromptOrderIndex,
|
||||
}
|
||||
for _, step := range steps {
|
||||
// Every boundary is checked before and after its step. The task repository
|
||||
// passes the same context to startup SQL through migrationContext, so a
|
||||
// cancellation stops later statements and later repository admission. A
|
||||
// statement already executing is bounded by the database driver's
|
||||
// cancellation behavior; the constructor never closes the shared pool
|
||||
// until this function returns, so that in-flight work cannot race cleanup.
|
||||
for index, step := range steps {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return fmt.Errorf("startup schema canceled before step %d: %w", index, err)
|
||||
}
|
||||
if err := step(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return fmt.Errorf("startup schema canceled after step %d: %w", index, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -89,7 +109,7 @@ func (r *Repository) ensureTaskEnvironmentRecoveryClaimsSchema() error {
|
||||
}
|
||||
|
||||
func (r *Repository) initDynamicRoutingSchema() error {
|
||||
_, err := r.db.Exec(fmt.Sprintf(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS dynamic_route_states (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
logical_profile_id TEXT NOT NULL,
|
||||
@@ -161,19 +181,19 @@ const taskResourceCleanupSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initTaskResourceCleanupSchema() error {
|
||||
_, err := r.db.Exec(taskResourceCleanupSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), taskResourceCleanupSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
// ensureWorkspaceIndexes creates workspace-related indexes
|
||||
func (r *Repository) ensureWorkspaceIndexes() error {
|
||||
if _, err := r.db.Exec(`CREATE INDEX IF NOT EXISTS idx_tasks_workspace_id ON tasks(workspace_id)`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `CREATE INDEX IF NOT EXISTS idx_tasks_workspace_id ON tasks(workspace_id)`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.db.Exec(`CREATE INDEX IF NOT EXISTS idx_workflows_workspace_id ON workflows(workspace_id)`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `CREATE INDEX IF NOT EXISTS idx_workflows_workspace_id ON workflows(workspace_id)`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.db.Exec(`CREATE INDEX IF NOT EXISTS idx_tasks_workspace_archived ON tasks(workspace_id, archived_at)`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `CREATE INDEX IF NOT EXISTS idx_tasks_workspace_archived ON tasks(workspace_id, archived_at)`); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -186,14 +206,14 @@ func (r *Repository) ensureMessageMetadataIndexes() error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_messages_metadata_tool_call_id ON task_session_messages(task_session_id, (%s))`,
|
||||
dialect.JSONExtract(driver, "metadata", "tool_call_id"),
|
||||
)
|
||||
if _, err := r.db.Exec(toolCallIndex); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), toolCallIndex); err != nil {
|
||||
return err
|
||||
}
|
||||
pendingIndex := fmt.Sprintf(
|
||||
`CREATE INDEX IF NOT EXISTS idx_messages_metadata_pending_id ON task_session_messages(task_session_id, (%s))`,
|
||||
dialect.JSONExtract(driver, "metadata", "pending_id"),
|
||||
)
|
||||
if _, err := r.db.Exec(pendingIndex); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), pendingIndex); err != nil {
|
||||
return err
|
||||
}
|
||||
// idx_messages_metadata_pending_id leads with task_session_id, so it
|
||||
@@ -204,7 +224,7 @@ func (r *Repository) ensureMessageMetadataIndexes() error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_messages_metadata_pending_id_lookup ON task_session_messages((%s))`,
|
||||
dialect.JSONExtract(driver, "metadata", "pending_id"),
|
||||
)
|
||||
if _, err := r.db.Exec(pendingIndexLookup); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), pendingIndexLookup); err != nil {
|
||||
return err
|
||||
}
|
||||
lookupIndex := dialect.PendingIDLookupIndexDDL(
|
||||
@@ -212,7 +232,7 @@ func (r *Repository) ensureMessageMetadataIndexes() error {
|
||||
"idx_messages_metadata_pending_id_lookup_ordered",
|
||||
"task_session_messages",
|
||||
)
|
||||
if _, err := r.db.Exec(lookupIndex); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), lookupIndex); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -223,13 +243,13 @@ func (r *Repository) ensureMessageMetadataIndexes() error {
|
||||
// filtered prompt pages skip interleaved agent and tool rows.
|
||||
func (r *Repository) ensurePromptOrderIndex() error {
|
||||
ddl := dialect.PromptOrderIndexDDL(r.db.DriverName(), "idx_messages_prompt_order", "task_session_messages")
|
||||
if _, err := r.db.Exec(ddl); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), ddl); err != nil {
|
||||
return fmt.Errorf("create prompt-order index: %w", err)
|
||||
}
|
||||
userDDL := dialect.PromptUserOrderIndexDDL(
|
||||
r.db.DriverName(), "idx_messages_prompt_user_order", "task_session_messages",
|
||||
)
|
||||
if _, err := r.db.Exec(userDDL); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), userDDL); err != nil {
|
||||
return fmt.Errorf("create prompt-user-order index: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -249,7 +269,7 @@ func (r *Repository) ensureRunnerProjectionTables() error {
|
||||
// repo so workflow.NewWithDB's later ALTER ADD COLUMNs become no-ops
|
||||
// (only column-already-exists errors are tolerated). Mirrors
|
||||
// internal/workflow/repository/sqlite.go (the canonical owner).
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS workflow_steps (
|
||||
id TEXT PRIMARY KEY,
|
||||
workflow_id TEXT NOT NULL DEFAULT '',
|
||||
@@ -275,7 +295,7 @@ func (r *Repository) ensureRunnerProjectionTables() error {
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create workflow_steps projection table: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS workflow_step_participants (
|
||||
id TEXT PRIMARY KEY,
|
||||
step_id TEXT NOT NULL DEFAULT '',
|
||||
@@ -319,7 +339,7 @@ const taskStatusSummarySchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initTaskStatusSummarySchema() error {
|
||||
_, err := r.db.Exec(taskStatusSummarySchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), taskStatusSummarySchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -441,12 +461,12 @@ const infraSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initInfraSchema() error {
|
||||
_, err := r.db.Exec(infraSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), infraSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) initTaskSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id TEXT PRIMARY KEY,
|
||||
workspace_id TEXT NOT NULL DEFAULT '',
|
||||
@@ -601,7 +621,7 @@ const repositorySetsSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initRepositorySetsSchema() error {
|
||||
_, err := r.db.Exec(repositorySetsSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), repositorySetsSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -625,12 +645,12 @@ const repositoryBranchPoliciesSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initRepositoryBranchPoliciesSchema() error {
|
||||
_, err := r.db.Exec(repositoryBranchPoliciesSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), repositoryBranchPoliciesSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) initCoreIndexes() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_workflow_id ON tasks(workflow_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_workflow_step_id ON tasks(workflow_step_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_archived_at ON tasks(archived_at);
|
||||
@@ -645,7 +665,7 @@ func (r *Repository) initCoreIndexes() error {
|
||||
}
|
||||
|
||||
func (r *Repository) initPlansSchema() error {
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_plans (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL UNIQUE,
|
||||
@@ -692,7 +712,7 @@ func (r *Repository) initPlansSchema() error {
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_plan_revisions (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL,
|
||||
@@ -729,7 +749,7 @@ func (r *Repository) initPlansSchema() error {
|
||||
// as a JSON array in a single column (read/written whole — there is no
|
||||
// per-step query path), keeping the artifact one row per task like task_plans.
|
||||
func (r *Repository) initWalkthroughsSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_walkthroughs (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL UNIQUE,
|
||||
@@ -748,7 +768,7 @@ func (r *Repository) initWalkthroughsSchema() error {
|
||||
// backfillInitialPlanRevisions ensures every existing task_plans row has at least
|
||||
// one corresponding revision. Runs once at startup and is idempotent.
|
||||
func (r *Repository) backfillInitialPlanRevisions() error {
|
||||
rows, err := r.db.Query(`
|
||||
rows, err := r.db.QueryContext(r.migrationContext(), `
|
||||
SELECT p.id, p.task_id, p.title, p.content, p.created_by, p.created_at, p.updated_at
|
||||
FROM task_plans p
|
||||
WHERE NOT EXISTS (
|
||||
@@ -781,7 +801,7 @@ func (r *Repository) backfillInitialPlanRevisions() error {
|
||||
if authorKind != "user" && authorKind != authorKindAgent {
|
||||
authorKind = authorKindAgent
|
||||
}
|
||||
_, err := r.db.Exec(r.db.Rebind(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), r.db.Rebind(`
|
||||
INSERT INTO task_plan_revisions
|
||||
(id, task_id, revision_number, title, content, author_kind, author_name, revert_of_revision_id, created_at, updated_at)
|
||||
VALUES (?, ?, 1, ?, ?, ?, 'legacy', NULL, ?, ?)
|
||||
@@ -796,7 +816,7 @@ func (r *Repository) backfillInitialPlanRevisions() error {
|
||||
// initDocumentsSchema creates the task_documents and task_document_revisions tables.
|
||||
// These tables generalize task_plans: documents have a key (e.g., "plan", "spec") and type.
|
||||
func (r *Repository) initDocumentsSchema() error {
|
||||
if _, err := r.db.Exec(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_documents (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL,
|
||||
@@ -844,7 +864,7 @@ func (r *Repository) initDocumentsSchema() error {
|
||||
// attachments. The storage key is intentionally opaque and never exposed in
|
||||
// API responses; bytes are owned by the attachment service.
|
||||
func (r *Repository) initAttachmentsSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_message_attachments (
|
||||
id TEXT PRIMARY KEY,
|
||||
owner_id TEXT NOT NULL DEFAULT '',
|
||||
@@ -901,7 +921,7 @@ func (r *Repository) initSessionSchema() error {
|
||||
// row instead of clobbering a later execution's (AC-32). Historical message
|
||||
// rows are handled separately by the one-time backfill migration.
|
||||
func (r *Repository) initSubagentContextSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_session_subagents (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_session_id TEXT NOT NULL,
|
||||
@@ -947,9 +967,9 @@ func (r *Repository) initSubagentContextSchema() error {
|
||||
// now-deleted step must survive that deletion.
|
||||
func (r *Repository) initStepTransitionsSchema() error {
|
||||
idCol := dialect.AutoIncrementIDColumn(r.db.DriverName())
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_step_transitions (
|
||||
` + idCol + `,
|
||||
`+idCol+`,
|
||||
task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
|
||||
session_id TEXT REFERENCES task_sessions(id) ON DELETE SET NULL,
|
||||
from_workflow_id TEXT,
|
||||
@@ -974,7 +994,7 @@ func (r *Repository) initStepTransitionsSchema() error {
|
||||
}
|
||||
|
||||
func (r *Repository) initMessageTurnSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_session_turns (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_session_id TEXT NOT NULL,
|
||||
@@ -1126,12 +1146,12 @@ const sessionWorktreeSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initSessionWorktreeSchema() error {
|
||||
_, err := r.db.Exec(sessionWorktreeSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), sessionWorktreeSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) initGitSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_session_git_snapshots (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_environment_id TEXT NOT NULL,
|
||||
@@ -1179,7 +1199,7 @@ func (r *Repository) initGitSchema() error {
|
||||
}
|
||||
|
||||
func (r *Repository) initReviewSchema() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS session_file_reviews (
|
||||
id TEXT PRIMARY KEY,
|
||||
session_id TEXT NOT NULL,
|
||||
@@ -1253,6 +1273,6 @@ const taskReviewSchemaDDL = `
|
||||
`
|
||||
|
||||
func (r *Repository) initTaskReviewSchema() error {
|
||||
_, err := r.db.Exec(taskReviewSchemaDDL)
|
||||
_, err := r.db.ExecContext(r.migrationContext(), taskReviewSchemaDDL)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ func (r *Repository) tryHealWorkflowStepEvents(
|
||||
stepID, operation string,
|
||||
mutate func(*wfmodels.StepEvents) bool,
|
||||
) (applied, retry bool, err error) {
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return false, false, fmt.Errorf("begin %s reconciliation tx: %w", operation, err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var rawEvents sql.NullString
|
||||
if err := tx.QueryRow(tx.Rebind(`SELECT events FROM workflow_steps WHERE id = ?`), stepID).Scan(&rawEvents); err != nil {
|
||||
if err := tx.QueryRowContext(r.migrationContext(), tx.Rebind(`SELECT events FROM workflow_steps WHERE id = ?`), stepID).Scan(&rawEvents); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return true, false, nil
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (r *Repository) tryHealWorkflowStepEvents(
|
||||
if guardArg != nil {
|
||||
args = append(args, guardArg)
|
||||
}
|
||||
res, err := tx.Exec(tx.Rebind(`
|
||||
res, err := tx.ExecContext(r.migrationContext(), tx.Rebind(`
|
||||
UPDATE workflow_steps SET events = ?, updated_at = ?
|
||||
WHERE id = ? AND `+guardClause), args...)
|
||||
if err != nil {
|
||||
|
||||
@@ -15,7 +15,7 @@ type systemWorkflowStepRow struct{ stepID, workflowID string }
|
||||
// transactional per-row retry loops never run with an open read cursor over
|
||||
// the same table.
|
||||
func (r *Repository) findSystemOwnedWorkflowSteps(templateID, stepName string) ([]systemWorkflowStepRow, error) {
|
||||
rows, err := r.db.Query(r.db.Rebind(`
|
||||
rows, err := r.db.QueryContext(r.migrationContext(), r.db.Rebind(`
|
||||
SELECT ws.id, ws.workflow_id FROM workflow_steps ws
|
||||
JOIN workflows w ON w.id = ws.workflow_id
|
||||
WHERE w.is_system = 1 AND w.workflow_template_id = ? AND ws.name = ?
|
||||
|
||||
@@ -58,7 +58,7 @@ func (r *Repository) hideBuiltinWorkflows() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("build hidden workflow reconciliation: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(r.db.Rebind(query), args...); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), r.db.Rebind(query), args...); err != nil {
|
||||
return fmt.Errorf("hide builtin workflows: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -79,7 +79,7 @@ func (r *Repository) healBuiltinWorkflowStepFlags() error {
|
||||
for _, tmpl := range templates {
|
||||
for _, step := range tmpl.Steps {
|
||||
want := dialect.BoolToInt(step.AutoAdvanceRequiresSignal)
|
||||
if _, err := r.db.Exec(r.db.Rebind(`
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), r.db.Rebind(`
|
||||
UPDATE workflow_steps SET auto_advance_requires_signal = ?, updated_at = ?
|
||||
WHERE name = ? AND auto_advance_requires_signal != ?
|
||||
AND workflow_id IN (SELECT id FROM workflows WHERE is_system = 1 AND workflow_template_id = ?)
|
||||
@@ -93,7 +93,7 @@ func (r *Repository) healBuiltinWorkflowStepFlags() error {
|
||||
|
||||
// ensureDefaultWorkspace creates a default workspace if none exists
|
||||
func (r *Repository) ensureDefaultWorkspace() error {
|
||||
ctx := context.Background()
|
||||
ctx := r.migrationContext()
|
||||
|
||||
var count int
|
||||
if err := r.db.QueryRowContext(ctx, "SELECT COUNT(1) FROM workspaces").Scan(&count); err != nil {
|
||||
@@ -215,7 +215,7 @@ func (r *Repository) stampWorkspaceOfficeWorkflow(ctx context.Context, workspace
|
||||
|
||||
// ensureDefaultExecutorsAndEnvironments creates default executors and environments if none exist
|
||||
func (r *Repository) ensureDefaultExecutorsAndEnvironments() error {
|
||||
ctx := context.Background()
|
||||
ctx := r.migrationContext()
|
||||
if err := r.ensureDefaultExecutors(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ func (r *Repository) initDesktopDiscoverySchema() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.db.Exec(desktopDiscoverySchemaDDL); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), desktopDiscoverySchemaDDL); err != nil {
|
||||
return err
|
||||
}
|
||||
required := 0
|
||||
if legacyInstallation {
|
||||
required = 1
|
||||
}
|
||||
_, err = r.db.Exec(r.db.Rebind(`
|
||||
_, err = r.db.ExecContext(r.migrationContext(), r.db.Rebind(`
|
||||
INSERT INTO desktop_discovery_migration (id, home_confirmation_required, updated_at)
|
||||
VALUES (1, ?, ?)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
|
||||
+15
-13
@@ -1,6 +1,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -54,14 +55,15 @@ const dynamicRouteLegacyActiveBackfillLockID int64 = 0x4B44_4452_4142 // "KDDRAB
|
||||
// is exactly the orphan the startup sweep exists to catch. Re-running a
|
||||
// value-based backfill on every boot would silently swallow it instead.
|
||||
func (r *Repository) backfillLegacyActiveDynamicRoutes() error {
|
||||
exists, err := db.ColumnExists(r.db, "dynamic_route_states", dynamicRouteLegacyActiveBackfillColumn)
|
||||
ctx := r.migrationContext()
|
||||
exists, err := db.ColumnExistsContext(ctx, r.db, "dynamic_route_states", dynamicRouteLegacyActiveBackfillColumn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: probe marker column: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: begin tx: %w", err)
|
||||
}
|
||||
@@ -74,20 +76,20 @@ func (r *Repository) backfillLegacyActiveDynamicRoutes() error {
|
||||
// Re-probe inside the lock: a concurrent PostgreSQL initializer may have
|
||||
// already run and committed the backfill between the cheap probe above
|
||||
// and this transaction acquiring the advisory lock.
|
||||
exists, err = r.columnExists(tx, "dynamic_route_states", dynamicRouteLegacyActiveBackfillColumn)
|
||||
exists, err = db.ColumnExistsContext(ctx, tx, "dynamic_route_states", dynamicRouteLegacyActiveBackfillColumn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: re-probe marker column: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
eligible, err := legacyActiveBackfillVersionAllowed(tx)
|
||||
eligible, err := legacyActiveBackfillVersionAllowed(ctx, tx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: read stored version: %w", err)
|
||||
}
|
||||
|
||||
if eligible {
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `
|
||||
UPDATE dynamic_route_states
|
||||
SET state = 'active'
|
||||
WHERE state = 'starting'
|
||||
@@ -105,7 +107,7 @@ func (r *Repository) backfillLegacyActiveDynamicRoutes() error {
|
||||
// Scoped to sessions whose dynamic_route_states row is now 'active' and
|
||||
// whose generation still matches. A session whose authoritative route row
|
||||
// or generation differs must keep its projection available for recovery.
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `
|
||||
UPDATE task_sessions
|
||||
SET route_state = 'active'
|
||||
WHERE state = 'IDLE' AND route_state = 'starting'
|
||||
@@ -120,8 +122,8 @@ func (r *Repository) backfillLegacyActiveDynamicRoutes() error {
|
||||
return fmt.Errorf("dynamic route legacy backfill: backfill task_sessions: %w", err)
|
||||
}
|
||||
}
|
||||
if _, err := tx.Exec(`ALTER TABLE dynamic_route_states ADD COLUMN ` +
|
||||
dynamicRouteLegacyActiveBackfillColumn + ` INTEGER NOT NULL DEFAULT 1`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `ALTER TABLE dynamic_route_states ADD COLUMN `+
|
||||
dynamicRouteLegacyActiveBackfillColumn+` INTEGER NOT NULL DEFAULT 1`); err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: add marker column: %w", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -130,8 +132,8 @@ func (r *Repository) backfillLegacyActiveDynamicRoutes() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func legacyActiveBackfillVersionAllowed(conn db.SchemaQuerier) (bool, error) {
|
||||
exists, err := db.TableExists(conn, "kandev_meta")
|
||||
func legacyActiveBackfillVersionAllowed(ctx context.Context, conn db.ContextSchemaQuerier) (bool, error) {
|
||||
exists, err := db.TableExistsContext(ctx, conn, "kandev_meta")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -139,7 +141,7 @@ func legacyActiveBackfillVersionAllowed(conn db.SchemaQuerier) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
var storedVersion string
|
||||
err = conn.QueryRow(conn.Rebind(
|
||||
err = conn.QueryRowContext(ctx, conn.Rebind(
|
||||
`SELECT value FROM kandev_meta WHERE key = 'kandev_version'`,
|
||||
)).Scan(&storedVersion)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -174,10 +176,10 @@ func (r *Repository) acquireDynamicRouteLegacyActiveBackfillLock(tx *sqlx.Tx) er
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(`SET LOCAL lock_timeout = '30s'`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `SET LOCAL lock_timeout = '30s'`); err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: set lock timeout: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`SELECT pg_advisory_xact_lock($1)`, dynamicRouteLegacyActiveBackfillLockID); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `SELECT pg_advisory_xact_lock($1)`, dynamicRouteLegacyActiveBackfillLockID); err != nil {
|
||||
return fmt.Errorf("dynamic route legacy backfill: acquire migration advisory lock: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
+10
-10
@@ -28,7 +28,7 @@ func (r *Repository) migrateGitSnapshotOwnership() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: begin tx: %w", err)
|
||||
}
|
||||
@@ -70,10 +70,10 @@ func (r *Repository) replayGitSnapshotFinalShape(tx *sqlx.Tx) error {
|
||||
}
|
||||
|
||||
func (r *Repository) prepareGitSnapshotCutover(tx *sqlx.Tx) error {
|
||||
if _, err := tx.Exec("DROP TABLE IF EXISTS " + gitSnapshotCutoverShadowTable); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "DROP TABLE IF EXISTS "+gitSnapshotCutoverShadowTable); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: remove stale shadow: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(finalGitSnapshotDDL(gitSnapshotCutoverShadowTable)); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), finalGitSnapshotDDL(gitSnapshotCutoverShadowTable)); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: create shadow: %w", err)
|
||||
}
|
||||
if err := r.maybeFailGitSnapshotCutover("create_shadow"); err != nil {
|
||||
@@ -99,10 +99,10 @@ func (r *Repository) prepareGitSnapshotCutover(tx *sqlx.Tx) error {
|
||||
}
|
||||
|
||||
func (r *Repository) swapGitSnapshotCutover(tx *sqlx.Tx) error {
|
||||
if _, err := tx.Exec("DROP TABLE task_session_git_snapshots"); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "DROP TABLE task_session_git_snapshots"); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: drop legacy table: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec("ALTER TABLE " + gitSnapshotCutoverShadowTable + " RENAME TO task_session_git_snapshots"); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "ALTER TABLE "+gitSnapshotCutoverShadowTable+" RENAME TO task_session_git_snapshots"); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: rename shadow: %w", err)
|
||||
}
|
||||
if err := r.maybeFailGitSnapshotCutover("swap"); err != nil {
|
||||
@@ -143,13 +143,13 @@ func (r *Repository) acquireGitSnapshotCutoverLocks(tx *sqlx.Tx) error {
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec("SET LOCAL lock_timeout = '30s'"); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "SET LOCAL lock_timeout = '30s'"); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: set lock timeout: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec("SELECT pg_advisory_xact_lock($1)", gitSnapshotCutoverLockID); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "SELECT pg_advisory_xact_lock($1)", gitSnapshotCutoverLockID); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: acquire migration advisory lock: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `
|
||||
LOCK TABLE task_session_git_snapshots, task_sessions, task_environments
|
||||
IN ACCESS EXCLUSIVE MODE`); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: lock ownership tables: %w", err)
|
||||
@@ -195,7 +195,7 @@ func (r *Repository) copyGitSnapshotWinners(tx *sqlx.Tx) error {
|
||||
) ranked
|
||||
WHERE row_number = 1
|
||||
`
|
||||
if _, err := tx.Exec(r.db.Rebind(query)); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), r.db.Rebind(query)); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: copy current winners: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -221,7 +221,7 @@ func (r *Repository) ensureGitSnapshotIndexes(tx *sqlx.Tx) error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_git_snapshots_session ON task_session_git_snapshots(session_id, created_at DESC)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_git_snapshots_type ON task_session_git_snapshots(session_id, snapshot_type)`,
|
||||
} {
|
||||
if _, err := tx.Exec(query); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), query); err != nil {
|
||||
return fmt.Errorf("git snapshot cutover: create index: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,434 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
dbpkg "github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/persistence"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// TestStartupMigrationCosts records fresh-schema and replay costs on an
|
||||
// isolated SQLite file. The output is evidence for startup budgeting; it does
|
||||
// not impose a machine-specific duration threshold.
|
||||
func TestStartupMigrationCosts(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "startup-costs.db")
|
||||
|
||||
freshDB := openStartupCostDB(t, dbPath)
|
||||
t.Cleanup(func() { _ = freshDB.Close() })
|
||||
freshStarted := time.Now()
|
||||
if _, err := NewWithDB(freshDB, freshDB, nil); err != nil {
|
||||
t.Fatalf("initialize fresh schema: %v", err)
|
||||
}
|
||||
freshDuration := time.Since(freshStarted)
|
||||
if err := freshDB.Close(); err != nil {
|
||||
t.Fatalf("close fresh database: %v", err)
|
||||
}
|
||||
|
||||
replayDB := openStartupCostDB(t, dbPath)
|
||||
t.Cleanup(func() { _ = replayDB.Close() })
|
||||
replayStarted := time.Now()
|
||||
if _, err := NewWithDB(replayDB, replayDB, nil); err != nil {
|
||||
t.Fatalf("replay schema: %v", err)
|
||||
}
|
||||
replayDuration := time.Since(replayStarted)
|
||||
if err := replayDB.Close(); err != nil {
|
||||
t.Fatalf("close replay database: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("startup migration cost: fresh=%s replay=%s", freshDuration, replayDuration)
|
||||
}
|
||||
|
||||
// TestStartupMigrationCostsPopulated measures startup against a repeatable
|
||||
// legacy-shaped fixture with enough messages for the two recurring backfills
|
||||
// to do real work. The fixture is intentionally local and synthetic: its
|
||||
// purpose is to expose the relative costs and the backup/liveness boundary,
|
||||
// not to predict a particular production machine's total startup time.
|
||||
func TestStartupMigrationCostsPopulated(t *testing.T) {
|
||||
const (
|
||||
sessionCount = 24
|
||||
messagesPerSession = 250
|
||||
)
|
||||
dbPath := filepath.Join(t.TempDir(), "startup-costs-populated.db")
|
||||
|
||||
freshDB := openStartupCostDB(t, dbPath)
|
||||
freshStarted := time.Now()
|
||||
if _, err := NewWithDB(freshDB, freshDB, nil); err != nil {
|
||||
t.Fatalf("initialize fresh schema: %v", err)
|
||||
}
|
||||
freshDuration := time.Since(freshStarted)
|
||||
if err := freshDB.Close(); err != nil {
|
||||
t.Fatalf("close fresh database: %v", err)
|
||||
}
|
||||
|
||||
seedDB := openStartupCostDB(t, dbPath)
|
||||
if err := makePopulatedLegacyMessageFixture(seedDB, sessionCount, messagesPerSession); err != nil {
|
||||
_ = seedDB.Close()
|
||||
t.Fatalf("prepare populated legacy fixture: %v", err)
|
||||
}
|
||||
messageCount := sessionCount * messagesPerSession
|
||||
var measuredMessageCount int
|
||||
if err := seedDB.Get(&measuredMessageCount, `SELECT COUNT(*) FROM task_session_messages`); err != nil {
|
||||
_ = seedDB.Close()
|
||||
t.Fatalf("count fixture messages: %v", err)
|
||||
}
|
||||
if measuredMessageCount != messageCount {
|
||||
_ = seedDB.Close()
|
||||
t.Fatalf("fixture message count = %d, want %d", measuredMessageCount, messageCount)
|
||||
}
|
||||
|
||||
dbInfo, err := os.Stat(dbPath)
|
||||
if err != nil {
|
||||
_ = seedDB.Close()
|
||||
t.Fatalf("stat populated fixture: %v", err)
|
||||
}
|
||||
backupPath := filepath.Join(t.TempDir(), "startup-costs-backup.db")
|
||||
backupStarted := time.Now()
|
||||
backupBytes, err := persistence.SnapshotSQLite(seedDB, backupPath)
|
||||
backupDuration := time.Since(backupStarted)
|
||||
if err != nil {
|
||||
_ = seedDB.Close()
|
||||
t.Fatalf("snapshot populated fixture: %v", err)
|
||||
}
|
||||
if err := seedDB.Close(); err != nil {
|
||||
t.Fatalf("close seeded database: %v", err)
|
||||
}
|
||||
|
||||
replayDB := openStartupCostDB(t, dbPath)
|
||||
replayStarted := time.Now()
|
||||
replayRepo, err := NewWithDB(replayDB, replayDB, nil)
|
||||
if err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("replay populated schema: %v", err)
|
||||
}
|
||||
replayDuration := time.Since(replayStarted)
|
||||
|
||||
// Measure the two recurring data migrations separately on the same
|
||||
// populated database. Reset statements are excluded from the timings.
|
||||
if _, err := replayDB.Exec(`UPDATE task_session_messages SET updated_at = NULL`); err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("reset updated_at fixture: %v", err)
|
||||
}
|
||||
updatedAtStarted := time.Now()
|
||||
if err := dbpkg.NewRequiredMigrateLogger(replayDB, nil).Apply(
|
||||
"evidence.task_session_messages.updated_at.backfill",
|
||||
`UPDATE task_session_messages SET updated_at = created_at WHERE updated_at IS NULL`,
|
||||
); err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("measure updated_at backfill: %v", err)
|
||||
}
|
||||
updatedAtDuration := time.Since(updatedAtStarted)
|
||||
|
||||
if _, err := replayDB.Exec(`UPDATE task_session_messages SET prompt_seq = 0`); err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("reset prompt sequence fixture: %v", err)
|
||||
}
|
||||
if _, err := replayDB.Exec(`DELETE FROM task_session_prompt_seq`); err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("reset prompt sequence counters: %v", err)
|
||||
}
|
||||
promptSeqStarted := time.Now()
|
||||
if err := replayRepo.backfillPromptSeq(); err != nil {
|
||||
_ = replayDB.Close()
|
||||
t.Fatalf("measure prompt sequence backfill: %v", err)
|
||||
}
|
||||
promptSeqDuration := time.Since(promptSeqStarted)
|
||||
if err := replayDB.Close(); err != nil {
|
||||
t.Fatalf("close replay database: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("populated startup evidence: sessions=%d messages=%d db_bytes=%d backup_bytes=%d fresh_schema=%s backup=%s replay=%s updated_at_backfill=%s prompt_seq_backfill=%s environment=%s/%s go=%s cpus=%d",
|
||||
sessionCount, messageCount, dbInfo.Size(), backupBytes, freshDuration, backupDuration,
|
||||
replayDuration, updatedAtDuration, promptSeqDuration, runtime.GOOS, runtime.GOARCH,
|
||||
runtime.Version(), runtime.NumCPU())
|
||||
}
|
||||
|
||||
// makePopulatedLegacyMessageFixture turns the current empty schema into the
|
||||
// shape that exercises the recurring startup migrations: updated_at is present
|
||||
// but nullable, and prompt_seq is absent. The rebuild is isolated to this
|
||||
// measurement test and does not alter production migration behavior.
|
||||
func makePopulatedLegacyMessageFixture(db *sqlx.DB, sessionCount, messagesPerSession int) error {
|
||||
for _, index := range []string{
|
||||
"idx_messages_session_id",
|
||||
"idx_messages_created_at",
|
||||
"idx_messages_session_created",
|
||||
"idx_messages_turn_id",
|
||||
"idx_messages_task_author_created",
|
||||
"idx_messages_session_updated",
|
||||
"idx_messages_metadata_tool_call_id",
|
||||
"idx_messages_metadata_pending_id",
|
||||
"idx_messages_metadata_pending_id_lookup",
|
||||
"idx_messages_metadata_pending_id_lookup_ordered",
|
||||
} {
|
||||
if _, err := db.Exec("DROP INDEX IF EXISTS " + index); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := db.Exec(`PRAGMA foreign_keys=OFF`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`ALTER TABLE task_session_messages RENAME TO startup_messages_current`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE task_session_messages (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_session_id TEXT NOT NULL,
|
||||
task_id TEXT DEFAULT '',
|
||||
turn_id TEXT NOT NULL,
|
||||
author_type TEXT NOT NULL DEFAULT 'user',
|
||||
author_id TEXT DEFAULT '',
|
||||
content TEXT NOT NULL,
|
||||
requests_input INTEGER DEFAULT 0,
|
||||
type TEXT NOT NULL DEFAULT 'message',
|
||||
metadata TEXT DEFAULT '{}',
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP,
|
||||
FOREIGN KEY (task_session_id) REFERENCES task_sessions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (turn_id) REFERENCES task_session_turns(id) ON DELETE CASCADE
|
||||
)`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO task_session_messages
|
||||
(id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at, updated_at)
|
||||
SELECT id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at, updated_at
|
||||
FROM startup_messages_current`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`DROP TABLE startup_messages_current`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`PRAGMA foreign_keys=ON`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var workspaceID, workflowID string
|
||||
if err := db.QueryRow(`SELECT id FROM workspaces ORDER BY created_at LIMIT 1`).Scan(&workspaceID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.QueryRow(`SELECT id FROM workflows ORDER BY created_at LIMIT 1`).Scan(&workflowID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
baseTime := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
for sessionIndex := 0; sessionIndex < sessionCount; sessionIndex++ {
|
||||
taskID := "startup-evidence-task-" + strconv.Itoa(sessionIndex)
|
||||
sessionID := "startup-evidence-session-" + strconv.Itoa(sessionIndex)
|
||||
turnID := "startup-evidence-turn-" + strconv.Itoa(sessionIndex)
|
||||
now := baseTime.Add(time.Duration(sessionIndex) * time.Hour)
|
||||
if _, err := tx.Exec(`INSERT INTO tasks (id, workspace_id, workflow_id, title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)`, taskID, workspaceID, workflowID, taskID, now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO task_sessions (id, task_id, started_at, updated_at) VALUES (?, ?, ?, ?)`, sessionID, taskID, now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO task_session_turns (id, task_session_id, task_id, started_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)`, turnID, sessionID, taskID, now, now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
for messageIndex := 0; messageIndex < messagesPerSession; messageIndex++ {
|
||||
authorType := "agent"
|
||||
if messageIndex%3 == 0 {
|
||||
authorType = "user"
|
||||
}
|
||||
createdAt := now.Add(time.Duration(messageIndex) * time.Microsecond)
|
||||
messageID := "startup-evidence-message-" + strconv.Itoa(sessionIndex) + "-" + strconv.Itoa(messageIndex)
|
||||
if _, err := tx.Exec(`
|
||||
INSERT INTO task_session_messages
|
||||
(id, task_session_id, task_id, turn_id, author_type, author_id, content, requests_input, type, metadata, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, '', 'startup evidence', 0, 'message', '{}', ?, NULL)
|
||||
`, messageID, sessionID, taskID, turnID, authorType, createdAt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func openStartupCostDB(t *testing.T, path string) *sqlx.DB {
|
||||
t.Helper()
|
||||
conn, err := dbpkg.OpenSQLite(path)
|
||||
if err != nil {
|
||||
t.Fatalf("open SQLite database: %v", err)
|
||||
}
|
||||
return sqlx.NewDb(conn, "sqlite3")
|
||||
}
|
||||
|
||||
// TestBackfillPromptSeqStopsOnCanceledContext exercises a real recurring
|
||||
// migration while its UPDATE is executing. The trigger blocks the statement
|
||||
// at the SQLite driver boundary so cancellation is deterministic and does not
|
||||
// depend on a timer racing a particular database size.
|
||||
func TestBackfillPromptSeqStopsOnCanceledContext(t *testing.T) {
|
||||
db := openStartupCostDB(t, filepath.Join(t.TempDir(), "prompt-cancel.db"))
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE task_session_messages (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_session_id TEXT NOT NULL,
|
||||
author_type TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
prompt_seq INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE task_session_prompt_seq (
|
||||
task_session_id TEXT PRIMARY KEY,
|
||||
last_seq INTEGER NOT NULL
|
||||
);
|
||||
INSERT INTO task_session_messages(id, task_session_id, author_type, created_at, prompt_seq)
|
||||
VALUES ('message-1', 'session-1', 'user', '2026-01-01T00:00:00Z', 0);
|
||||
`); err != nil {
|
||||
t.Fatalf("seed prompt sequence fixture: %v", err)
|
||||
}
|
||||
started := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
var startedOnce sync.Once
|
||||
conn, err := db.Conn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("reserve SQLite connection: %v", err)
|
||||
}
|
||||
if err := conn.Raw(func(driverConn any) error {
|
||||
sqliteConn, ok := driverConn.(*sqlite3.SQLiteConn)
|
||||
if !ok {
|
||||
t.Fatalf("driver connection = %T, want *sqlite3.SQLiteConn", driverConn)
|
||||
}
|
||||
return sqliteConn.RegisterFunc("wait_for_prompt_release", func() int64 {
|
||||
startedOnce.Do(func() { close(started) })
|
||||
<-release
|
||||
return 1
|
||||
}, true)
|
||||
}); err != nil {
|
||||
_ = conn.Close()
|
||||
t.Fatalf("register SQLite function: %v", err)
|
||||
}
|
||||
if err := conn.Close(); err != nil {
|
||||
t.Fatalf("release SQLite connection: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TRIGGER block_prompt_sequence_update
|
||||
BEFORE UPDATE OF prompt_seq ON task_session_messages
|
||||
BEGIN
|
||||
SELECT wait_for_prompt_release();
|
||||
END;
|
||||
`); err != nil {
|
||||
t.Fatalf("create blocking trigger: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
repo := &Repository{
|
||||
db: db,
|
||||
migrate: dbpkg.NewRequiredMigrateLoggerContext(db, nil, ctx),
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- repo.backfillPromptSeq() }()
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("prompt sequence migration did not enter the active statement")
|
||||
}
|
||||
cancel()
|
||||
close(release)
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("prompt sequence migration error = %v, want context.Canceled", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("prompt sequence migration did not stop after cancellation")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewWithDBContextStopsActiveMigration proves that cancellation reaches
|
||||
// the real repository constructor at the shared startup boundary. The
|
||||
// trigger blocks the production prompt-sequence UPDATE, then cancellation
|
||||
// must abort that constructor before it can admit a later schema step.
|
||||
func TestNewWithDBContextStopsActiveMigration(t *testing.T) {
|
||||
db := openStartupCostDB(t, filepath.Join(t.TempDir(), "constructor-cancel.db"))
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
db.SetMaxOpenConns(1)
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("initialize schema: %v", err)
|
||||
}
|
||||
seedForMsgTest(t, repo, "constructor-task", "constructor-session", "constructor-turn")
|
||||
var workspaceID, workflowID string
|
||||
if err := db.QueryRow(`SELECT id FROM workspaces ORDER BY created_at LIMIT 1`).Scan(&workspaceID); err != nil {
|
||||
t.Fatalf("read workspace: %v", err)
|
||||
}
|
||||
if err := db.QueryRow(`SELECT id FROM workflows ORDER BY created_at LIMIT 1`).Scan(&workflowID); err != nil {
|
||||
t.Fatalf("read workflow: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`UPDATE tasks SET workspace_id = ?, workflow_id = ? WHERE id = ?`, workspaceID, workflowID, "constructor-task"); err != nil {
|
||||
t.Fatalf("make constructor fixture replayable: %v", err)
|
||||
}
|
||||
insertPromptRow(t, repo, "constructor-message", "constructor-session", "constructor-turn", "user", "startup cancellation", time.Now().UTC())
|
||||
|
||||
started := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
var startedOnce sync.Once
|
||||
conn, err := db.Conn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("reserve SQLite connection: %v", err)
|
||||
}
|
||||
if err := conn.Raw(func(driverConn any) error {
|
||||
sqliteConn, ok := driverConn.(*sqlite3.SQLiteConn)
|
||||
if !ok {
|
||||
t.Fatalf("driver connection = %T, want *sqlite3.SQLiteConn", driverConn)
|
||||
}
|
||||
return sqliteConn.RegisterFunc("wait_for_constructor_release", func() int64 {
|
||||
startedOnce.Do(func() { close(started) })
|
||||
<-release
|
||||
return 1
|
||||
}, true)
|
||||
}); err != nil {
|
||||
_ = conn.Close()
|
||||
t.Fatalf("register SQLite function: %v", err)
|
||||
}
|
||||
if err := conn.Close(); err != nil {
|
||||
t.Fatalf("release SQLite connection: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TRIGGER block_constructor_prompt_sequence
|
||||
BEFORE UPDATE OF prompt_seq ON task_session_messages
|
||||
BEGIN
|
||||
SELECT wait_for_constructor_release();
|
||||
END;
|
||||
`); err != nil {
|
||||
t.Fatalf("create blocking trigger: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := NewWithDBContext(ctx, db, db, nil)
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("repository constructor did not enter the active migration")
|
||||
}
|
||||
cancel()
|
||||
close(release)
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("constructor error = %v, want context.Canceled", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("repository constructor did not stop after cancellation")
|
||||
}
|
||||
}
|
||||
@@ -71,9 +71,9 @@ func isStepEntryMarkerUniqueViolation(err error) bool {
|
||||
// initStepTransitionsSchema's comment for why that rule doesn't apply here.
|
||||
func (r *Repository) initStepEntriesSchema() error {
|
||||
idCol := dialect.AutoIncrementIDColumn(r.db.DriverName())
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS workflow_step_entries (
|
||||
` + idCol + `,
|
||||
`+idCol+`,
|
||||
task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
|
||||
step_id TEXT NOT NULL,
|
||||
entry_seq INTEGER NOT NULL,
|
||||
@@ -85,7 +85,7 @@ func (r *Repository) initStepEntriesSchema() error {
|
||||
ON workflow_step_entries(task_id, step_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS workflow_step_entry_markers (
|
||||
` + idCol + `,
|
||||
`+idCol+`,
|
||||
entry_id INTEGER NOT NULL REFERENCES workflow_step_entries(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
|
||||
@@ -24,7 +24,7 @@ func subagentContextActivationKeyInsertSQL(key, value string) string {
|
||||
// empty string as this key's legitimate sentinel value, not "absent".
|
||||
func (r *Repository) subagentContextActivationKeyPresent(key string) (bool, error) {
|
||||
var value string
|
||||
err := r.db.QueryRow(r.db.Rebind(`SELECT value FROM kandev_meta WHERE key = ?`), key).Scan(&value)
|
||||
err := r.db.QueryRowContext(r.migrationContext(), r.db.Rebind(`SELECT value FROM kandev_meta WHERE key = ?`), key).Scan(&value)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -89,16 +90,17 @@ func (r *Repository) migrateSubagentContextBackfill() {
|
||||
// trigger, or from a pre-amendment partial pair — is preserved verbatim
|
||||
// (AC-24 point 5, AC-24d revision 4).
|
||||
func (r *Repository) migrateSubagentContextBackfillUnsafe() error {
|
||||
ctx := r.migrationContext()
|
||||
postgres := dialect.IsPostgres(r.db.DriverName())
|
||||
predicate, selectCols := subagentContextBackfillPredicateAndColumns(postgres)
|
||||
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin subagent context backfill transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
backfillThrough, err := subagentContextBackfillHighWaterMark(tx, postgres, predicate)
|
||||
backfillThrough, err := subagentContextBackfillHighWaterMark(ctx, tx, postgres, predicate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -148,15 +150,15 @@ func (r *Repository) migrateSubagentContextBackfillUnsafe() error {
|
||||
WHERE m.rn = 1
|
||||
ON CONFLICT (task_session_id, agent_execution_id, tool_call_id) DO NOTHING
|
||||
`
|
||||
if _, err := tx.Exec(insertSQL); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, insertSQL); err != nil {
|
||||
return fmt.Errorf("subagent context backfill insert: %w", err)
|
||||
}
|
||||
|
||||
captureSince := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if _, err := tx.Exec(subagentContextActivationKeyInsertSQL(subagentContextCaptureSinceKey, captureSince)); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, subagentContextActivationKeyInsertSQL(subagentContextCaptureSinceKey, captureSince)); err != nil {
|
||||
return fmt.Errorf("write subagent_context_capture_since: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(subagentContextActivationKeyInsertSQL(subagentContextBackfillThroughKey, backfillThrough)); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, subagentContextActivationKeyInsertSQL(subagentContextBackfillThroughKey, backfillThrough)); err != nil {
|
||||
return fmt.Errorf("write subagent_context_backfill_through: %w", err)
|
||||
}
|
||||
|
||||
@@ -176,11 +178,11 @@ func (r *Repository) migrateSubagentContextBackfillUnsafe() error {
|
||||
// see the declared column type; wrapping it in MAX() loses that, so the scan
|
||||
// target must be a string on that dialect (see parseLegacyTimestamp's own
|
||||
// comment for the same fact). PostgreSQL's driver has no such gap.
|
||||
func subagentContextBackfillHighWaterMark(tx *sqlx.Tx, postgres bool, predicate string) (string, error) {
|
||||
func subagentContextBackfillHighWaterMark(ctx context.Context, tx *sqlx.Tx, postgres bool, predicate string) (string, error) {
|
||||
query := `SELECT MAX(m.created_at) FROM task_session_messages m WHERE ` + predicate
|
||||
if postgres {
|
||||
var maxCreatedAt sql.NullTime
|
||||
if err := tx.QueryRow(query).Scan(&maxCreatedAt); err != nil {
|
||||
if err := tx.QueryRowContext(ctx, query).Scan(&maxCreatedAt); err != nil {
|
||||
return "", fmt.Errorf("compute subagent context backfill high-water mark: %w", err)
|
||||
}
|
||||
if !maxCreatedAt.Valid {
|
||||
@@ -189,7 +191,7 @@ func subagentContextBackfillHighWaterMark(tx *sqlx.Tx, postgres bool, predicate
|
||||
return maxCreatedAt.Time.UTC().Format(time.RFC3339Nano), nil
|
||||
}
|
||||
var maxCreatedAt sql.NullString
|
||||
if err := tx.QueryRow(query).Scan(&maxCreatedAt); err != nil {
|
||||
if err := tx.QueryRowContext(ctx, query).Scan(&maxCreatedAt); err != nil {
|
||||
return "", fmt.Errorf("compute subagent context backfill high-water mark: %w", err)
|
||||
}
|
||||
if !maxCreatedAt.Valid || maxCreatedAt.String == "" {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -25,7 +24,7 @@ func (r *Repository) migrateTaskPriorityToTextPostgres() error {
|
||||
}
|
||||
|
||||
var dataType string
|
||||
err := r.db.Get(&dataType, `
|
||||
err := r.db.GetContext(r.migrationContext(), &dataType, `
|
||||
SELECT data_type
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = current_schema()
|
||||
@@ -42,7 +41,7 @@ func (r *Repository) migrateTaskPriorityToTextPostgres() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := r.db.BeginTxx(context.Background(), nil)
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tasks.priority migration: %w", err)
|
||||
}
|
||||
@@ -59,7 +58,7 @@ func (r *Repository) migrateTaskPriorityToTextPostgres() error {
|
||||
{"add constraint", `ALTER TABLE tasks ADD CONSTRAINT tasks_priority_check CHECK (priority IN ('critical','high','medium','low'))`},
|
||||
}
|
||||
for _, statement := range statements {
|
||||
if _, err := tx.Exec(statement.sql); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), statement.sql); err != nil {
|
||||
return fmt.Errorf("tasks.priority migration (%s): %w", statement.name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func (r *Repository) ensureTeamAccessSchema() error {
|
||||
// visible to everyone until the setup wizard claims them, and inventing an
|
||||
// owner here would lock the single user out of their own data.
|
||||
func (r *Repository) backfillWorkspaceOwnerMembers() error {
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
INSERT INTO workspace_members (workspace_id, user_id, role, added_by, created_at)
|
||||
SELECT w.id, w.owner_id, 'owner', '', w.created_at
|
||||
FROM workspaces w
|
||||
|
||||
@@ -34,9 +34,9 @@ func (r *Repository) initTaskUsageEventsSchema() error {
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
idCol = "id BIGSERIAL PRIMARY KEY"
|
||||
}
|
||||
_, err := r.db.Exec(`
|
||||
_, err := r.db.ExecContext(r.migrationContext(), `
|
||||
CREATE TABLE IF NOT EXISTS task_usage_events (
|
||||
` + idCol + `,
|
||||
`+idCol+`,
|
||||
usage_event_id TEXT NOT NULL,
|
||||
task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
|
||||
session_id TEXT REFERENCES task_sessions(id) ON DELETE SET NULL,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -17,10 +18,13 @@ func (r *Repository) prepareTaskWorktreeCutoverForeignKeys() (func(), error) {
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
return func() {}, nil
|
||||
}
|
||||
if _, err := r.db.Exec(`PRAGMA foreign_keys=OFF`); err != nil {
|
||||
if _, err := r.db.ExecContext(r.migrationContext(), `PRAGMA foreign_keys=OFF`); err != nil {
|
||||
return nil, fmt.Errorf("cutover: disable sqlite foreign keys: %w", err)
|
||||
}
|
||||
return func() { _, _ = r.db.Exec(`PRAGMA foreign_keys=ON`) }, nil
|
||||
// Cleanup must restore the connection setting even after startup context
|
||||
// cancellation; otherwise the shared writer would retain foreign-key checks
|
||||
// disabled for the next operation.
|
||||
return func() { _, _ = r.db.ExecContext(context.Background(), `PRAGMA foreign_keys=ON`) }, nil
|
||||
}
|
||||
|
||||
// rebindGitSnapshotEnvironmentForeignKey preserves an already environment-
|
||||
@@ -41,7 +45,7 @@ func (r *Repository) rebindGitSnapshotEnvironmentForeignKey(c *worktreeCutover,
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(postgresGitSnapshotShadowForeignKeyDDL); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), postgresGitSnapshotShadowForeignKeyDDL); err != nil {
|
||||
return fmt.Errorf("cutover: rebind git snapshot environment foreign key: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -53,7 +57,7 @@ func (r *Repository) rehomeGitSnapshotsForCutover(c *worktreeCutover, tx *sqlx.T
|
||||
if survivingID == "" || survivingID == environmentID {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(tx.Rebind(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), tx.Rebind(`
|
||||
UPDATE task_session_git_snapshots
|
||||
SET task_environment_id = ?
|
||||
WHERE task_environment_id = ?
|
||||
@@ -81,7 +85,7 @@ func (r *Repository) rebindTaskEnvironmentRecoveryClaimForeignKey(c *worktreeCut
|
||||
if survivingID == "" || survivingID == environmentID {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(tx.Rebind(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), tx.Rebind(`
|
||||
UPDATE task_environment_recovery_claims
|
||||
SET task_environment_id = ?
|
||||
WHERE task_environment_id = ?
|
||||
@@ -92,7 +96,7 @@ func (r *Repository) rebindTaskEnvironmentRecoveryClaimForeignKey(c *worktreeCut
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(postgresRecoveryClaimShadowForeignKeyDDL); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), postgresRecoveryClaimShadowForeignKeyDDL); err != nil {
|
||||
return fmt.Errorf("cutover: rebind recovery claim environment foreign key: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/db/dialect"
|
||||
)
|
||||
|
||||
@@ -107,7 +109,7 @@ func (r *Repository) normalizeTaskWorktreeOwnership() error {
|
||||
}
|
||||
defer restoreForeignKeys()
|
||||
|
||||
tx, err := r.db.Beginx()
|
||||
tx, err := r.db.BeginTxx(r.migrationContext(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cutover: begin tx: %w", err)
|
||||
}
|
||||
@@ -129,6 +131,7 @@ func (r *Repository) normalizeTaskWorktreeOwnership() error {
|
||||
return err
|
||||
}
|
||||
cut := &worktreeCutover{
|
||||
ctx: r.migrationContext(),
|
||||
envs: make(map[string]*legacyEnv),
|
||||
taskEnvs: make(map[string][]*legacyEnv),
|
||||
sessions: make(map[string]*legacySession),
|
||||
@@ -211,10 +214,10 @@ func (r *Repository) cutoverAcquireLocks(tx *sqlx.Tx) error {
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(`SET LOCAL lock_timeout = '30s'`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `SET LOCAL lock_timeout = '30s'`); err != nil {
|
||||
return fmt.Errorf("cutover: set lock timeout: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`SELECT pg_advisory_xact_lock($1)`, taskWorktreeCutoverLockID); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `SELECT pg_advisory_xact_lock($1)`, taskWorktreeCutoverLockID); err != nil {
|
||||
return fmt.Errorf("cutover: acquire migration advisory lock: %w", err)
|
||||
}
|
||||
claimTable, err := r.columnExists(tx, "task_environment_recovery_claims", "task_environment_id")
|
||||
@@ -227,7 +230,7 @@ func (r *Repository) cutoverAcquireLocks(tx *sqlx.Tx) error {
|
||||
if claimTable {
|
||||
lockTables += `, task_environment_recovery_claims`
|
||||
}
|
||||
if _, err := tx.Exec("LOCK TABLE " + lockTables + " IN ACCESS EXCLUSIVE MODE"); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), "LOCK TABLE "+lockTables+" IN ACCESS EXCLUSIVE MODE"); err != nil {
|
||||
return fmt.Errorf("cutover: lock ownership tables: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -235,19 +238,11 @@ func (r *Repository) cutoverAcquireLocks(tx *sqlx.Tx) error {
|
||||
|
||||
// tableExists reports whether a table exists on either database engine.
|
||||
func (r *Repository) tableExists(name string) (bool, error) {
|
||||
var exists bool
|
||||
var err error
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
err = r.db.QueryRow(`
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_name = $1 AND table_schema = current_schema())`,
|
||||
name).Scan(&exists)
|
||||
} else {
|
||||
err = r.db.QueryRow(
|
||||
`SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?)`,
|
||||
name).Scan(&exists)
|
||||
}
|
||||
return db.TableExists(r.db, name)
|
||||
}
|
||||
|
||||
func (r *Repository) tableExistsContext(ctx context.Context, name string) (bool, error) {
|
||||
exists, err := db.TableExistsContext(ctx, r.db, name)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("probe table %s: %w", name, err)
|
||||
}
|
||||
@@ -266,7 +261,7 @@ func (r *Repository) cutoverEnsureLegacyBranchSlug(tx *sqlx.Tx) error {
|
||||
if has {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(`ALTER TABLE task_session_worktrees ADD COLUMN branch_slug TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `ALTER TABLE task_session_worktrees ADD COLUMN branch_slug TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
return fmt.Errorf("cutover: add legacy branch_slug: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -274,19 +269,7 @@ func (r *Repository) cutoverEnsureLegacyBranchSlug(tx *sqlx.Tx) error {
|
||||
|
||||
// columnExists reports whether a column exists on either database engine.
|
||||
func (r *Repository) columnExists(tx *sqlx.Tx, table, column string) (bool, error) {
|
||||
var exists bool
|
||||
var err error
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
err = tx.QueryRow(`
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = $1 AND column_name = $2 AND table_schema = current_schema())`,
|
||||
table, column).Scan(&exists)
|
||||
} else {
|
||||
err = tx.QueryRow(`SELECT EXISTS (
|
||||
SELECT 1 FROM pragma_table_info(?) WHERE name = ?)`,
|
||||
table, column).Scan(&exists)
|
||||
}
|
||||
exists, err := db.ColumnExistsContext(r.migrationContext(), tx, table, column)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("probe column %s.%s: %w", table, column, err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -20,6 +21,7 @@ const (
|
||||
|
||||
// worktreeCutover holds the legacy inventory and the normalized result.
|
||||
type worktreeCutover struct {
|
||||
ctx context.Context
|
||||
envs map[string]*legacyEnv
|
||||
taskEnvs map[string][]*legacyEnv
|
||||
sessions map[string]*legacySession
|
||||
@@ -93,7 +95,7 @@ func (c *worktreeCutover) loadLegacy(tx *sqlx.Tx, legacyEnvColumns, legacyRepoCo
|
||||
func (c *worktreeCutover) loadLegacyEnvs(tx *sqlx.Tx, columns map[string]bool) error {
|
||||
// Every interpolated expression comes from the fixed deprecated-column allowlist.
|
||||
//nolint:gosec
|
||||
rows, err := tx.Query(fmt.Sprintf(`
|
||||
rows, err := tx.QueryContext(c.ctx, fmt.Sprintf(`
|
||||
SELECT id, task_id, %s, executor_type, executor_id,
|
||||
executor_profile_id, control_port, status,
|
||||
%s, %s, %s, COALESCE(workspace_path, ''),
|
||||
@@ -137,7 +139,7 @@ func (c *worktreeCutover) loadLegacySessions(tx *sqlx.Tx) error {
|
||||
// query (SQLite is lenient about functional dependency). started_at is
|
||||
// scanned as a string because MIN() makes the driver return the raw
|
||||
// TEXT value instead of a time.
|
||||
rows, err := tx.Query(`
|
||||
rows, err := tx.QueryContext(c.ctx, `
|
||||
SELECT ts.id, ts.task_id, COALESCE(ts.task_environment_id, ''), ts.state,
|
||||
COALESCE(ts.executor_id, ''), COALESCE(ts.executor_profile_id, ''),
|
||||
COALESCE(ts.repository_id, ''), COALESCE(er.container_id, ''),
|
||||
@@ -186,7 +188,7 @@ func parseLegacyTimestamp(raw string) time.Time {
|
||||
func (c *worktreeCutover) loadLegacyEnvRepos(tx *sqlx.Tx, columns map[string]bool) error {
|
||||
// Every interpolated expression comes from the fixed lifecycle-column allowlist.
|
||||
//nolint:gosec
|
||||
rows, err := tx.Query(fmt.Sprintf(`
|
||||
rows, err := tx.QueryContext(c.ctx, fmt.Sprintf(`
|
||||
SELECT id, task_environment_id, repository_id, COALESCE(branch_slug, ''),
|
||||
COALESCE(worktree_id, ''), COALESCE(worktree_path, ''),
|
||||
COALESCE(worktree_branch, ''), position, COALESCE(error_message, ''),
|
||||
@@ -228,7 +230,7 @@ func legacyRepoColumnExpr(columns map[string]bool, column, fallback string) stri
|
||||
}
|
||||
|
||||
func (c *worktreeCutover) loadLegacySessionWorktrees(tx *sqlx.Tx) error {
|
||||
rows, err := tx.Query(`
|
||||
rows, err := tx.QueryContext(c.ctx, `
|
||||
SELECT session_id, worktree_id, repository_id, COALESCE(branch_slug, ''),
|
||||
position, COALESCE(worktree_path, ''), COALESCE(worktree_branch, ''),
|
||||
status, created_at, updated_at, merged_at, deleted_at
|
||||
@@ -710,7 +712,7 @@ func (c *worktreeCutover) resolveExecutorType(tx *sqlx.Tx, executorID string) (s
|
||||
return cached, nil
|
||||
}
|
||||
var executorType string
|
||||
err := tx.QueryRow(tx.Rebind(`SELECT type FROM executors WHERE id = ?`), executorID).Scan(&executorType)
|
||||
err := tx.QueryRowContext(c.ctx, tx.Rebind(`SELECT type FROM executors WHERE id = ?`), executorID).Scan(&executorType)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.executorTypes[executorID] = executorTypeLocalPC
|
||||
return executorTypeLocalPC, nil
|
||||
|
||||
@@ -220,10 +220,10 @@ func (t *envRepoTarget) verifyWorktree(worktreeID, path, branch string) error {
|
||||
|
||||
// cutoverBuildShadows creates the final-shape shadow tables.
|
||||
func (r *Repository) cutoverBuildShadows(tx *sqlx.Tx) error {
|
||||
if _, err := tx.Exec(finalTaskEnvironmentsDDL("task_environments_shadow")); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), finalTaskEnvironmentsDDL("task_environments_shadow")); err != nil {
|
||||
return fmt.Errorf("cutover: create task_environments_shadow: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(finalTaskEnvironmentReposDDL("task_environment_repos_shadow", "task_environments_shadow")); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), finalTaskEnvironmentReposDDL("task_environment_repos_shadow", "task_environments_shadow")); err != nil {
|
||||
return fmt.Errorf("cutover: create task_environment_repos_shadow: %w", err)
|
||||
}
|
||||
return r.maybeFailCutover("create_shadow")
|
||||
@@ -237,7 +237,7 @@ func (r *Repository) insertNormalized(c *worktreeCutover, tx *sqlx.Tx) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("cutover: internal: environment %s for task %s missing", envID, taskID)
|
||||
}
|
||||
if _, err := tx.Exec(tx.Rebind(`
|
||||
if _, err := tx.ExecContext(r.migrationContext(), tx.Rebind(`
|
||||
INSERT INTO task_environments_shadow (
|
||||
id, task_id, ownership_generation, executor_type, executor_id, executor_profile_id,
|
||||
control_port, status, materialization_session_id, workspace_path, container_id,
|
||||
@@ -270,7 +270,7 @@ func (r *Repository) insertNormalized(c *worktreeCutover, tx *sqlx.Tx) error {
|
||||
if envID == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(tx.Rebind(
|
||||
if _, err := tx.ExecContext(r.migrationContext(), tx.Rebind(
|
||||
`UPDATE task_sessions SET task_environment_id = ? WHERE id = ?`),
|
||||
envID, sessionID); err != nil {
|
||||
return fmt.Errorf("cutover: link session %s to environment %s: %w", sessionID, envID, err)
|
||||
@@ -293,7 +293,7 @@ func (c *worktreeCutover) insertRepoRow(tx *sqlx.Tx, taskID, envID string, targe
|
||||
if target.deletedAt != nil {
|
||||
deletedAt = *target.deletedAt
|
||||
}
|
||||
if _, err := tx.Exec(tx.Rebind(`
|
||||
if _, err := tx.ExecContext(c.ctx, tx.Rebind(`
|
||||
INSERT INTO task_environment_repos_shadow (
|
||||
id, task_environment_id, repository_id, branch_slug,
|
||||
worktree_id, worktree_path, worktree_branch, position,
|
||||
@@ -462,11 +462,11 @@ func (r *Repository) tableColumns(tx *sqlx.Tx, table string) (map[string]bool, e
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
rows, err = tx.Query(`
|
||||
rows, err = tx.QueryContext(r.migrationContext(), `
|
||||
SELECT column_name FROM information_schema.columns
|
||||
WHERE table_name = $1 AND table_schema = current_schema()`, table)
|
||||
} else {
|
||||
rows, err = tx.Query(`SELECT name FROM pragma_table_info(?)`, table)
|
||||
rows, err = tx.QueryContext(r.migrationContext(), `SELECT name FROM pragma_table_info(?)`, table)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cutover: read columns of %s: %w", table, err)
|
||||
@@ -490,18 +490,18 @@ func (r *Repository) tableColumns(tx *sqlx.Tx, table string) (map[string]bool, e
|
||||
// the swap. Postgres constraint names are restored to their canonical form
|
||||
// afterwards.
|
||||
func (r *Repository) cutoverSwap(c *worktreeCutover, tx *sqlx.Tx) error {
|
||||
if _, err := tx.Exec(`DROP TABLE task_session_worktrees`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `DROP TABLE task_session_worktrees`); err != nil {
|
||||
return fmt.Errorf("cutover: drop task_session_worktrees: %w", err)
|
||||
}
|
||||
// Preview-build session-delete cleanup jobs are invalid under the
|
||||
// task-owned model; purge them before any cleanup worker can claim them.
|
||||
if _, err := tx.Exec(tx.Rebind(`DELETE FROM task_resource_cleanup_jobs WHERE trigger = ?`), "session_delete"); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), tx.Rebind(`DELETE FROM task_resource_cleanup_jobs WHERE trigger = ?`), "session_delete"); err != nil {
|
||||
return fmt.Errorf("cutover: purge preview session_delete cleanup jobs: %w", err)
|
||||
}
|
||||
if err := r.maybeFailCutover("drop_legacy"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`DROP TABLE task_environment_repos`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `DROP TABLE task_environment_repos`); err != nil {
|
||||
return fmt.Errorf("cutover: drop legacy task_environment_repos: %w", err)
|
||||
}
|
||||
if err := r.rebindGitSnapshotEnvironmentForeignKey(c, tx); err != nil {
|
||||
@@ -510,13 +510,13 @@ func (r *Repository) cutoverSwap(c *worktreeCutover, tx *sqlx.Tx) error {
|
||||
if err := r.rebindTaskEnvironmentRecoveryClaimForeignKey(c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`DROP TABLE task_environments`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `DROP TABLE task_environments`); err != nil {
|
||||
return fmt.Errorf("cutover: drop legacy task_environments: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`ALTER TABLE task_environments_shadow RENAME TO task_environments`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `ALTER TABLE task_environments_shadow RENAME TO task_environments`); err != nil {
|
||||
return fmt.Errorf("cutover: rename task_environments_shadow: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`ALTER TABLE task_environment_repos_shadow RENAME TO task_environment_repos`); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), `ALTER TABLE task_environment_repos_shadow RENAME TO task_environment_repos`); err != nil {
|
||||
return fmt.Errorf("cutover: rename task_environment_repos_shadow: %w", err)
|
||||
}
|
||||
if err := r.maybeFailCutover("swap"); err != nil {
|
||||
@@ -533,7 +533,7 @@ func (r *Repository) cutoverSwap(c *worktreeCutover, tx *sqlx.Tx) error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_task_environment_repos_env_id ON task_environment_repos(task_environment_id)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_task_environment_repos_repository_id ON task_environment_repos(repository_id)`,
|
||||
} {
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
if _, err := tx.ExecContext(r.migrationContext(), stmt); err != nil {
|
||||
return fmt.Errorf("cutover: recreate index: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -556,7 +556,7 @@ func (r *Repository) cutoverRenamePostgresConstraints(tx *sqlx.Tx) error {
|
||||
}
|
||||
for _, rename := range renames {
|
||||
var constraintName string
|
||||
err := tx.QueryRow(tx.Rebind(`
|
||||
err := tx.QueryRowContext(r.migrationContext(), tx.Rebind(`
|
||||
SELECT conname FROM pg_constraint
|
||||
WHERE conrelid = (SELECT oid FROM pg_class WHERE relname = ? AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()))
|
||||
AND conname LIKE ?
|
||||
@@ -564,7 +564,7 @@ func (r *Repository) cutoverRenamePostgresConstraints(tx *sqlx.Tx) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cutover: find postgres constraint on %s: %w", rename.table, err)
|
||||
}
|
||||
if _, err := tx.Exec(fmt.Sprintf(
|
||||
if _, err := tx.ExecContext(r.migrationContext(), fmt.Sprintf(
|
||||
`ALTER TABLE %s RENAME CONSTRAINT %s TO %s`, rename.table, constraintName, rename.canonical)); err != nil {
|
||||
return fmt.Errorf("cutover: rename postgres constraint %s: %w", constraintName, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user