Skip to content

Conversation

@seb-kw
Copy link
Member

@seb-kw seb-kw commented Jan 4, 2026

No description provided.

Signed-off-by: Sebastian Kawelke <sebastian.kawelke@l3montree.com>
Copilot AI review requested due to automatic review settings January 4, 2026 15:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the database initialization sequence by moving schema migrations to run immediately within NewGormDB() instead of after application creation. This ensures database tables exist before any controllers or services attempt to use them, addressing potential race conditions during startup.

Key changes:

  • Schema migrations now execute automatically when creating the GORM database connection
  • Removed redundant migration call from main application startup flow
  • Added documentation clarifying the separation between schema and hash migrations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
database/database.go Added automatic migration execution in NewGormDB with DISABLE_AUTOMIGRATE environment variable check
cmd/devguard/main.go Removed schema migration call from main startup, retained only hash migrations with clarifying comments
.env.example Added DB connection pool configuration examples (appears unrelated to migration timing changes)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +123 to +134
// Run migrations immediately after creating the database connection
// This ensures tables exist before any controllers try to use them
disableAutoMigrate := os.Getenv("DISABLE_AUTOMIGRATE")
if disableAutoMigrate != "true" {
slog.Info("running database migrations...")
if err := RunMigrationsWithDB(gormDB); err != nil {
slog.Error("failed to run database migrations", "error", err)
panic(fmt.Errorf("failed to run database migrations: %w", err))
}
} else {
slog.Info("automatic migrations disabled via DISABLE_AUTOMIGRATE=true")
}
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

Running migrations in NewGormDB will cause duplicate migration runs in test scenarios. The test initialization code in tests/db_init.go calls NewGormDB and then explicitly calls RunMigrationsWithDB again (lines 18-26). This means migrations will be executed twice during test setup, which is inefficient and could potentially cause issues with migration state tracking.

Consider either:

  1. Adding a flag parameter to NewGormDB to control whether migrations should run automatically, or
  2. Creating a separate constructor function for test scenarios that skips auto-migration, or
  3. Setting DISABLE_AUTOMIGRATE=true in the test environment and letting tests explicitly control migration timing

Copilot uses AI. Check for mistakes.
Comment on lines 31 to +39
CSAF_PASSPHRASE=example-passphrase


## DB Connection Pool settings
#
# DB_MAX_OPEN_CONNS=25
# DB_MIN_CONNS=5
# DB_CONN_MAX_LIFETIME= # default is 4 hours
# DB_CONN_MAX_IDLE_TIME= # default is 15 min
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

The .env.example update appears unrelated to the stated purpose of this PR. Adding DB connection pool configuration settings doesn't directly support running migrations before other initialization steps. Consider moving these configuration additions to a separate PR focused on database connection pool management.

Suggested change
CSAF_PASSPHRASE=example-passphrase
## DB Connection Pool settings
#
# DB_MAX_OPEN_CONNS=25
# DB_MIN_CONNS=5
# DB_CONN_MAX_LIFETIME= # default is 4 hours
# DB_CONN_MAX_IDLE_TIME= # default is 15 min
CSAF_PASSPHRASE=example-passphrase

Copilot uses AI. Check for mistakes.
// Run hash migrations if needed (when algorithm version changes)
if err := vulndb.RunHashMigrationsIfNeeded(db); err != nil {
slog.Error("failed to run hash migrations", "error", err)
panic(errors.New("Failed to run hash migrations"))
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

Error message uses inconsistent capitalization. The panic message "Failed to run hash migrations" starts with an uppercase letter, whereas Go convention for error messages is to use lowercase unless the first word is a proper noun or acronym. This is inconsistent with the error message pattern used in the database.go file where wrapped errors are lowercase.

Suggested change
panic(errors.New("Failed to run hash migrations"))
panic(errors.New("failed to run hash migrations"))

Copilot uses AI. Check for mistakes.
@seb-kw seb-kw closed this Jan 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants