Skip to content

feat: Enable optimization by default, add logs - BED-9170 - #3105

Merged
StephenHinck merged 2 commits into
mainfrom
BED-9170
Aug 6, 2026
Merged

feat: Enable optimization by default, add logs - BED-9170#3105
StephenHinck merged 2 commits into
mainfrom
BED-9170

Conversation

@StephenHinck

@StephenHinck StephenHinck commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Description

Default-enables optimization and adds logging to optimize calls.

Motivation and Context

Resolves BED-9170

Why is this change required? What problem does it solve?

How Has This Been Tested?

Validated in local environment.

Screenshots (optional):

Types of changes

  • New feature (non-breaking change which adds functionality)
  • Database Migrations

Checklist:

Summary by CodeRabbit

  • New Features

    • Graph storage optimization is now enabled by default with configurable processing stages and scheduling intervals.
    • Added tracking for the most recently completed optimization.
  • Improvements

    • Optimization stages now provide structured measurement details, improving visibility into processing progress and performance.
    • Optimization behavior and error handling remain unchanged while reporting becomes more consistent.

@StephenHinck StephenHinck self-assigned this Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds structured logging for graph storage optimization and moves its default configuration and status column into a separate database migration.

Changes

Graph storage optimization

Layer / File(s) Summary
Separate graph storage optimization defaults migration
cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql, cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql
The original migration delegates its up operation. The new migration inserts default optimization settings, adds the nullable last_complete_optimize_at column, and removes both objects during rollback.
Optimization duration and scope logging
cmd/api/src/daemons/datapipe/pipeline.go
Optimize and OptimizeOnBoot record structured duration and function metadata before running optimization.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested labels: enhancement, dbmigration, go

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main changes: enabling optimization by default and adding logs.
Description check ✅ Passed The description covers the change, ticket, migration type, checklist, and testing, but the testing details are brief.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch BED-9170

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot added dbmigration enhancement New feature or request go Pull requests that update go code labels Aug 5, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql`:
- Line 18: Restore the original Up migration in
cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql
and keep its prior seed unchanged; add its Down migration to remove the
parameter and column. In
cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql,
update only the existing prior seed to the enabled value when it still matches,
preserving externally changed values and updated_at, and add a Down migration
that restores the prior seed.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 0a69a2c5-be66-4d09-814b-ae845845cd5e

📥 Commits

Reviewing files that changed from the base of the PR and between c9e4e36 and 676a09e.

📒 Files selected for processing (3)
  • cmd/api/src/daemons/datapipe/pipeline.go
  • cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql
  • cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql


ALTER TABLE datapipe_status
ADD COLUMN IF NOT EXISTS last_complete_optimize_at timestamp with time zone;
-- Intentionally empty - see 20260805120000_v9_default_enable_graph_storage_optimization.sql for the up migration

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- migration files ---'
for f in \
  cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql \
  cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql
do
  echo "--- $f"
  cat -n "$f"
done

printf '%s\n' '--- migration status/history ---'
git log --oneline --all -- \
  cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql \
  cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql | head -30

printf '%s\n' '--- migration references and parameter schema/usages ---'
rg -n -C 3 \
  'analysis\.graph_storage_optimization|20260722120000_v9_add_graph_storage_optimization_parameter|20260805120000_v9_default_enable_graph_storage_optimization|CREATE TABLE[[:space:]]+parameters|ON CONFLICT' \
  cmd/api/src/database cmd/api/src --glob '*.sql' --glob '*.go' --glob '*.yaml' --glob '*.yml' | head -300

Repository: SpecterOps/BloodHound

Length of output: 31849


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- relevant migration directory listing ---'
git ls-files cmd/api/src/database/migration/migrations | tail -40

printf '%s\n' '--- current branch and tracked-file status ---'
git status --short
git branch --show-current

printf '%s\n' '--- migration annotations and license headers ---'
for f in \
  cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql \
  cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql
do
  echo "--- $f"
  sed -n '1,40p' "$f"
done

Repository: SpecterOps/BloodHound

Length of output: 5711


🏁 Script executed:

#!/bin/bash
set -e
for f in \
  cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql \
  cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql
do
  echo "--- $f ---"
  cat -n "$f"
done

echo '--- commits touching these paths ---'
git log --oneline --all --follow -- cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql | head -20
git log --oneline --all --follow -- cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql | head -20

echo '--- all references ---'
rg -n -C 4 'analysis\.graph_storage_optimization|20260722120000_v9_add_graph_storage_optimization_parameter|20260805120000_v9_default_enable_graph_storage_optimization|CREATE TABLE[[:space:]]+parameters|INSERT INTO parameters' . --glob '*.sql' --glob '*.go' --glob '*.yaml' --glob '*.yml' | head -400

Repository: SpecterOps/BloodHound

Length of output: 33576


🏁 Script executed:

#!/bin/bash
set -e
python3 - <<'PY'
from pathlib import Path
files = [
    Path("cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql"),
    Path("cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql"),
]
for p in files:
    print(f"--- {p} ---")
    text = p.read_text()
    print(text)
    print("annotations:", [line for line in text.splitlines() if "goose" in line.lower()])
    print("license first line:", text.splitlines()[0] if text.splitlines() else "")
PY

echo '--- migration filenames around v9 ---'
git ls-files 'cmd/api/src/database/migration/migrations/*.sql' | grep -E 'v9|20260722120000|20260805120000' | tail -40

echo '--- branch/ref evidence ---'
git branch -a --contains HEAD
git log --oneline --all --decorate -- cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql | head -20

Repository: SpecterOps/BloodHound

Length of output: 5256


🏁 Script executed:

#!/bin/bash
set -e
old=cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql
new=cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql

echo '--- original migration at the preceding feature commit ---'
git show 34f7b45f6e:"$old" | cat -n

echo '--- changes in the enable-by-default commit ---'
git diff --no-ext-diff --unified=8 34f7b45f6e 676a09ee2b -- "$old" "$new"

echo '--- commit metadata ---'
git show -s --format='format:%H%n%P%n%ad%n%s' --date=iso-strict 34f7b45f6e 676a09ee2b

Repository: SpecterOps/BloodHound

Length of output: 5826


🏁 Script executed:

#!/bin/bash
set -e
echo '--- parameter table definition ---'
rg -n -C 10 'CREATE TABLE[[:space:]]+parameters|parameters[[:space:]]*\(' \
  cmd/api/src/database/migration/migrations/00000000000001_init.sql \
  cmd/api/src/database/migration/migrations/legacy/schema.sql | head -160

echo '--- parameter write paths ---'
rg -n -C 6 'UPDATE[[:space:]]+parameters|updated_at.*parameters|parameters.*updated_at|ParameterKey|GraphStorageOptimizationKey' \
  cmd/api/src --glob '*.go' | head -300

echo '--- migration runner rollback behavior ---'
rg -n -C 8 'Rollback|Down|migrations|goose' cmd/api/src/database/migration/goose.go | head -260

Repository: SpecterOps/BloodHound

Length of output: 50377


Preserve migration history and rollback ownership.

  • Restore the original Up and keep 20260722120000_v9_add_graph_storage_optimization_parameter.sql immutable. Its prior seed is {"after_boot": false, "after_analysis": false, "min_interval_seconds": 86400}.
  • Make 20260805120000_v9_default_enable_graph_storage_optimization.sql update only that prior seed to the enabled value. Do not overwrite values changed outside the migration or update updated_at.
  • Add a Down migration that restores the prior seed. The current empty Down leaves the parameter and column after rollback.
📍 Affects 2 files
  • cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql#L18-L18 (this comment)
  • cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql#L18-L27
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql`
at line 18, Restore the original Up migration in
cmd/api/src/database/migration/migrations/20260722120000_v9_add_graph_storage_optimization_parameter.sql
and keep its prior seed unchanged; add its Down migration to remove the
parameter and column. In
cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql,
update only the existing prior seed to the enabled value when it still matches,
preserving externally changed values and updated_at, and add a Down migration
that restores the prior seed.

Sources: Coding guidelines, Learnings

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql`:
- Around line 33-36: Update the migration’s parameter handling so Down removes
only the row owned and inserted by this migration, while preserving pre-existing
or user-edited values. Track ownership during Up alongside the ON CONFLICT DO
NOTHING behavior, and use that ownership marker when replacing the unconditional
DELETE for analysis.graph_storage_optimization.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 5fc8a4cc-8c76-49dd-a4dd-8c5a7648a0bc

📥 Commits

Reviewing files that changed from the base of the PR and between 676a09e and e4029b2.

📒 Files selected for processing (1)
  • cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql

Comment on lines +33 to +36
ALTER TABLE datapipe_status
DROP COLUMN IF EXISTS last_complete_optimize_at;

DELETE FROM parameters WHERE key = 'analysis.graph_storage_optimization';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Preserve parameter rows that this migration does not own.

ON CONFLICT DO NOTHING in Lines 18-27 allows Up to succeed without inserting the row when analysis.graph_storage_optimization already exists. Line 36 then deletes that existing row during Down. This can erase a user-edited value or a row created by an earlier migration. Track migration ownership and preserve changed rows before deleting the parameter.

🧰 Tools
🪛 Squawk (2.61.0)

[warning] 34-34: Dropping a column may break existing clients.

(ban-drop-column)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@cmd/api/src/database/migration/migrations/20260805120000_v9_default_enable_graph_storage_optimization.sql`
around lines 33 - 36, Update the migration’s parameter handling so Down removes
only the row owned and inserted by this migration, while preserving pre-existing
or user-edited values. Track ownership during Up alongside the ON CONFLICT DO
NOTHING behavior, and use that ownership marker when replacing the unconditional
DELETE for analysis.graph_storage_optimization.

@StephenHinck
StephenHinck merged commit 6915c1f into main Aug 6, 2026
14 checks passed
@StephenHinck
StephenHinck deleted the BED-9170 branch August 6, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dbmigration enhancement New feature or request go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants