Native SQLite page copy
apps/backend/internal/db/sqlite_backup.go ↗The helper copies a consistent database image without rebuilding tables and indexes.
Page copy entry
func BackupSQLite(ctx context.Context, source *sql.DB, path string) (err error) {
if err = ctx.Err(); err != nil {
return err
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0o600)
if err != nil {
return err
}
defer func() {
if err != nil {
_ = os.Remove(path)
_ = os.Remove(path + "-journal")
}
}()
if err = file.Close(); err != nil {
return err
}
absolute, err := filepath.Abs(path)
if err != nil {
return err
}
Bounded step copy
func copySQLitePages(ctx context.Context, source, destination *sqlite3.SQLiteConn, pages int) (err error) {
backup, err := destination.Backup("main", source, "main")
if err != nil {
return err
}
defer func() { err = errors.Join(err, backup.Finish()) }()
lastProgress := time.Now()
for {
if err := ctx.Err(); err != nil {
return err
}
remaining, total := backup.Remaining(), backup.PageCount()
done, err := backup.Step(pages)
if err != nil {
return err
}
if done {
return ctx.Err()
}
}
Boot backup uses page copy
apps/backend/internal/persistence/provider.go ↗Startup takes the pre-migration snapshot with the page copy, then validates and installs it.
Staged copy and install
func createPreMigrationBackup(ctx context.Context, writer *sqlx.DB, backupDir, storedVersion string) (string, int64, error) {
path := snapshotPath(backupDir, storedVersion)
stagingDir, err := os.MkdirTemp(backupDir, ".kandev-backup-*")
if err != nil {
return "", 0, fmt.Errorf("create private backup staging directory: %w", err)
}
defer func() { _ = os.RemoveAll(stagingDir) }()
stagedPath := filepath.Join(stagingDir, filepath.Base(path))
stopTracking := trackBackupProgress(ctx, stagedPath)
size, err := CopySQLiteSnapshotContext(ctx, writer, stagedPath)
stopTracking()
if err != nil {
return "", 0, fmt.Errorf("pre-migration backup failed: %w", err)
}
if err := os.Chmod(stagedPath, 0o600); err != nil {
return "", 0, fmt.Errorf("protect staged pre-migration backup: %w", err)
}
if _, err := inspectSQLiteCandidate(stagedPath); err != nil {
return "", 0, fmt.Errorf("validate staged pre-migration backup: %w", err)
}
if err := installStagedSQLite(stagedPath, path); err != nil {
return "", 0, fmt.Errorf("install pre-migration backup: %w", err)
}
return path, size, nil
}
Light selection and legacy adopt
apps/backend/internal/persistence/sqlite_selection.go ↗Startup checks the current database with a light read, and adopts a legacy database with a page copy.
Lightweight current check
current, err := inspectSQLiteCandidateLightweight(currentPath)
if err != nil {
return sqliteSelection{}, err
}
if current.exists && current.hasTaskHistory {
selection := sqliteSelection{
path: currentPath,
existedBeforeOpen: true,
outcome: sqliteSelectionExisting,
}
logSQLiteSelection(log, cfg.SourceFor("database.path"), selection)
return selection, nil
}
Legacy adopt with copy
source, err := openSQLiteReadOnly(legacyPath)
if err != nil {
return fmt.Errorf("open legacy sqlite database: %w", err)
}
defer func() { _ = source.Close() }()
if _, err := CopySQLiteSnapshot(source, stagedPath); err != nil {
return fmt.Errorf("snapshot legacy sqlite database: %w", err)
}
Retention drains without sleep
apps/backend/internal/task/deliveryretention/worker.go ↗The worker repeats productive phases at once and returns to idle delay only when no work remains.
Continuous pass loop
func (w *Worker) runPass(ctx context.Context) Snapshot {
started := w.opts.Now()
deadline := started.Add(passBudget)
snapshot := Snapshot{OldestEligibleAgeSeconds: -1}
phases := []string{"retention", "orphans", "retirements"}
for len(phases) > 0 {
if ctx.Err() != nil {
break
}
remaining := deadline.Sub(w.opts.Now())
if remaining < batchBudget {
snapshot.ContinuationPending = committedRetentionWork(snapshot) > 0
break
}
phase := phases[0]
phases = phases[1:]
before := committedRetentionWork(snapshot)
if !w.runPassBatch(ctx, phase, remaining, &snapshot) {
break
}
if committedRetentionWork(snapshot) > before {
phases = append(phases, phase)
}
}
Immediate continuation
default:
w.retryAttempt = 0
if snapshot.ContinuationPending {
continue
}
}