feat: Add dry-run mode to ingest, sync, and stats commands
Enables preview of operations without making changes, useful for understanding what would happen before committing to a full sync. Ingest dry-run (--dry-run flag): - Shows resource type, sync mode (full vs incremental), project list - Per-project info: existing count, has_cursor, last_synced timestamp - No GitLab API calls, no database writes Sync dry-run (--dry-run flag): - Preview all four stages: issues ingest, MRs ingest, docs, embed - Shows which stages would run vs be skipped (--no-docs, --no-embed) - Per-project breakdown for both entity types Stats repair dry-run (--dry-run flag): - Shows what would be repaired without executing repairs - "would fix" vs "fixed" indicator in terminal output - dry_run: true field in JSON response Implementation details: - DryRunPreview struct captures project-level sync state - SyncDryRunResult aggregates previews for all sync stages - Terminal output uses yellow styling for "would" actions - JSON output includes dry_run: true at top level Flag handling: - --dry-run and --no-dry-run pair for explicit control - Defaults to false (normal operation) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -69,9 +69,10 @@ pub struct RepairResult {
|
||||
pub fts_rebuilt: bool,
|
||||
pub orphans_deleted: i64,
|
||||
pub stale_cleared: i64,
|
||||
pub dry_run: bool,
|
||||
}
|
||||
|
||||
pub fn run_stats(config: &Config, check: bool, repair: bool) -> Result<StatsResult> {
|
||||
pub fn run_stats(config: &Config, check: bool, repair: bool, dry_run: bool) -> Result<StatsResult> {
|
||||
let db_path = get_db_path(config.storage.db_path.as_deref());
|
||||
let conn = create_connection(&db_path)?;
|
||||
|
||||
@@ -220,43 +221,54 @@ pub fn run_stats(config: &Config, check: bool, repair: bool) -> Result<StatsResu
|
||||
|
||||
if repair {
|
||||
let mut repair_result = RepairResult::default();
|
||||
repair_result.dry_run = dry_run;
|
||||
|
||||
if integrity.fts_doc_mismatch {
|
||||
conn.execute(
|
||||
"INSERT INTO documents_fts(documents_fts) VALUES('rebuild')",
|
||||
[],
|
||||
)?;
|
||||
if !dry_run {
|
||||
conn.execute(
|
||||
"INSERT INTO documents_fts(documents_fts) VALUES('rebuild')",
|
||||
[],
|
||||
)?;
|
||||
}
|
||||
repair_result.fts_rebuilt = true;
|
||||
}
|
||||
|
||||
if integrity.orphan_embeddings > 0 && table_exists(&conn, "embedding_metadata") {
|
||||
let deleted = conn.execute(
|
||||
"DELETE FROM embedding_metadata
|
||||
WHERE NOT EXISTS (SELECT 1 FROM documents d WHERE d.id = embedding_metadata.document_id)",
|
||||
[],
|
||||
)?;
|
||||
repair_result.orphans_deleted = deleted as i64;
|
||||
|
||||
if table_exists(&conn, "embeddings") {
|
||||
let _ = conn.execute(
|
||||
"DELETE FROM embeddings
|
||||
WHERE rowid / 1000 NOT IN (SELECT id FROM documents)",
|
||||
if !dry_run {
|
||||
let deleted = conn.execute(
|
||||
"DELETE FROM embedding_metadata
|
||||
WHERE NOT EXISTS (SELECT 1 FROM documents d WHERE d.id = embedding_metadata.document_id)",
|
||||
[],
|
||||
);
|
||||
)?;
|
||||
repair_result.orphans_deleted = deleted as i64;
|
||||
|
||||
if table_exists(&conn, "embeddings") {
|
||||
let _ = conn.execute(
|
||||
"DELETE FROM embeddings
|
||||
WHERE rowid / 1000 NOT IN (SELECT id FROM documents)",
|
||||
[],
|
||||
);
|
||||
}
|
||||
} else {
|
||||
repair_result.orphans_deleted = integrity.orphan_embeddings;
|
||||
}
|
||||
}
|
||||
|
||||
if integrity.stale_metadata > 0 && table_exists(&conn, "embedding_metadata") {
|
||||
let cleared = conn.execute(
|
||||
"DELETE FROM embedding_metadata
|
||||
WHERE document_id IN (
|
||||
SELECT em.document_id FROM embedding_metadata em
|
||||
JOIN documents d ON d.id = em.document_id
|
||||
WHERE em.chunk_index = 0 AND em.document_hash != d.content_hash
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
repair_result.stale_cleared = cleared as i64;
|
||||
if !dry_run {
|
||||
let cleared = conn.execute(
|
||||
"DELETE FROM embedding_metadata
|
||||
WHERE document_id IN (
|
||||
SELECT em.document_id FROM embedding_metadata em
|
||||
JOIN documents d ON d.id = em.document_id
|
||||
WHERE em.chunk_index = 0 AND em.document_hash != d.content_hash
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
repair_result.stale_cleared = cleared as i64;
|
||||
} else {
|
||||
repair_result.stale_cleared = integrity.stale_metadata;
|
||||
}
|
||||
}
|
||||
|
||||
integrity.repair = Some(repair_result);
|
||||
@@ -387,22 +399,35 @@ pub fn print_stats(result: &StatsResult) {
|
||||
|
||||
if let Some(ref repair) = integrity.repair {
|
||||
println!();
|
||||
println!("{}", style("Repair").cyan().bold());
|
||||
if repair.dry_run {
|
||||
println!(
|
||||
"{} {}",
|
||||
style("Repair").cyan().bold(),
|
||||
style("(dry run - no changes made)").yellow()
|
||||
);
|
||||
} else {
|
||||
println!("{}", style("Repair").cyan().bold());
|
||||
}
|
||||
|
||||
let action = if repair.dry_run {
|
||||
style("would fix").yellow()
|
||||
} else {
|
||||
style("fixed").green()
|
||||
};
|
||||
|
||||
if repair.fts_rebuilt {
|
||||
println!(" {} FTS index rebuilt", style("fixed").green());
|
||||
println!(" {} FTS index rebuilt", action);
|
||||
}
|
||||
if repair.orphans_deleted > 0 {
|
||||
println!(
|
||||
" {} {} orphan embeddings deleted",
|
||||
style("fixed").green(),
|
||||
repair.orphans_deleted
|
||||
action, repair.orphans_deleted
|
||||
);
|
||||
}
|
||||
if repair.stale_cleared > 0 {
|
||||
println!(
|
||||
" {} {} stale metadata entries cleared",
|
||||
style("fixed").green(),
|
||||
repair.stale_cleared
|
||||
action, repair.stale_cleared
|
||||
);
|
||||
}
|
||||
if !repair.fts_rebuilt && repair.orphans_deleted == 0 && repair.stale_cleared == 0 {
|
||||
@@ -442,6 +467,7 @@ pub fn print_stats_json(result: &StatsResult) {
|
||||
fts_rebuilt: r.fts_rebuilt,
|
||||
orphans_deleted: r.orphans_deleted,
|
||||
stale_cleared: r.stale_cleared,
|
||||
dry_run: r.dry_run,
|
||||
}),
|
||||
}),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user