Skip to content

[self-hosted] specify sql file location of auth, activity and main store#5487

Merged
braginini merged 2 commits intomainfrom
feature/add-sql-stores-file-paths
Mar 3, 2026
Merged

[self-hosted] specify sql file location of auth, activity and main store#5487
braginini merged 2 commits intomainfrom
feature/add-sql-stores-file-paths

Conversation

@braginini
Copy link
Copy Markdown
Collaborator

@braginini braginini commented Mar 2, 2026

Describe your changes

Allows specifying custom SQLite file paths via store.file, activityStore.file, and authStore.file in the combined server config. Without this, all SQLite databases are
hardcoded to {dataDir}/store.db, {dataDir}/events.db, and {dataDir}/idp.db respectively.

The combined server propagates the file paths to the underlying store packages via NB_STORE_ENGINE_SQLITE_FILE and NB_ACTIVITY_EVENT_SQLITE_FILE env vars (matching the
existing DSN env var pattern). The auth store file is passed directly through the embedded IdP config.

Both absolute paths and SQLite URI query parameters (e.g., ?cache=shared) are supported.

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • New Features

    • Customize database file locations for primary store, activity events, and auth store.
    • Environment variables allow runtime overrides of SQLite file paths.
    • Supports relative (resolved against data directory) and absolute file paths for SQLite stores.
  • Documentation

    • Example configuration updated with optional file-path fields for store, activityStore, and authStore.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

Adds optional SQLite file path configuration for server stores and embedded IdP, wires those values into environment variables during startup, and updates SQLite store initializers to accept and resolve custom file paths (relative to dataDir) and preserve query parameters.

Changes

Cohort / File(s) Summary
Configuration Structure
combined/cmd/config.go
Added public File string (yaml:"file") to StoreConfig and applied AuthStore.File when building embedded IdP config (resolves relative paths against management DataDir).
Server Initialization
combined/cmd/root.go, combined/cmd/token.go
Set environment variables NB_STORE_ENGINE_SQLITE_FILE and NB_ACTIVITY_EVENT_SQLITE_FILE from config when respective Store.File or ActivityStore.File are provided.
Configuration Example
combined/config.yaml.example
Added commented example entries for store.file, activityStore.file, and authStore.file.
Activity Store Implementation
management/server/activity/store/sql_store.go
Read NB_ACTIVITY_EVENT_SQLITE_FILE to override default events DB filename; resolve relative paths against dataDir and use absolute conn string with sqlite.Open(connStr).
Main Store Implementation
management/server/store/sql_store.go
Read NB_STORE_ENGINE_SQLITE_FILE, split file and query (?), preserve user query or add cache=shared on non-Windows, and open sqlite with composed connection string instead of fixed "?cache=shared".

Sequence Diagram(s)

sequenceDiagram
  participant Config as Config (combined/cmd)
  participant Env as Env (process env)
  participant Init as Init (token/root)
  participant Store as Store (sql_store.go)
  participant Activity as ActivityStore (activity/sql_store.go)

  Config->>Init: provide Server.Store.File / ActivityStore.File / AuthStore.File
  Init->>Env: set NB_STORE_ENGINE_SQLITE_FILE, NB_ACTIVITY_EVENT_SQLITE_FILE
  Env->>Store: NB_STORE_ENGINE_SQLITE_FILE read
  Store->>Store: resolve path (abs/rel -> dataDir), split query, compose connStr
  Store->>Store: sqlite.Open(connStr)
  Env->>Activity: NB_ACTIVITY_EVENT_SQLITE_FILE read
  Activity->>Activity: resolve path (abs/rel -> dataDir), compose connStr
  Activity->>Activity: sqlite.Open(connStr)
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

Suggested Reviewers

  • mlsmaycon
  • shuuri-labs
  • bcmmbaga

Poem

"I nibble configs by moonlit stream,
Paths once fixed now dance and gleam. 🐇
From dataDir to places new,
Each DB finds a home that's you.
Hooray—files set, the servers beam!"

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: enabling specification of SQL file locations for auth, activity, and main stores in the self-hosted server.
Description check ✅ Passed The description covers the key aspects of the change with technical details about configuration options, environment variables, and supported features. The checklist is completed with feature enhancement selected, and documentation justification provided.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/add-sql-stores-file-paths

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In `@combined/cmd/config.go`:
- Around line 572-574: The assignment of c.Server.AuthStore.File to
authStorageFile should resolve relative paths against mgmt.DataDir so the auth
DB lives under the management data directory like the other sqlite stores;
update the logic where authStorageFile is set (referencing
c.Server.AuthStore.File and authStorageFile) to check if the value is non-empty
and not an absolute path (use filepath.IsAbs) and if so join it with
mgmt.DataDir (use filepath.Join and optionally filepath.Clean) before assigning;
keep absolute paths unchanged.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 82da606 and e881e0e.

📒 Files selected for processing (6)
  • combined/cmd/config.go
  • combined/cmd/root.go
  • combined/cmd/token.go
  • combined/config.yaml.example
  • management/server/activity/store/sql_store.go
  • management/server/store/sql_store.go

Comment thread combined/cmd/config.go
pascal-fischer
pascal-fischer previously approved these changes Mar 3, 2026
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
combined/cmd/config.go (1)

572-578: Inconsistent use of path vs filepath for filesystem paths.

Line 572 uses path.Join while line 576 uses filepath.Join. The path package uses forward slashes unconditionally (POSIX-style), while filepath uses OS-native separators. For filesystem operations, filepath should be used consistently to ensure Windows compatibility.

♻️ Proposed fix for consistency
-		authStorageFile = path.Join(mgmt.DataDir, "idp.db")
+		authStorageFile = filepath.Join(mgmt.DataDir, "idp.db")
 		if c.Server.AuthStore.File != "" {
 			authStorageFile = c.Server.AuthStore.File
 			if !filepath.IsAbs(authStorageFile) {
 				authStorageFile = filepath.Join(mgmt.DataDir, authStorageFile)
 			}
 		}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@combined/cmd/config.go` around lines 572 - 578, The code uses path.Join to
build authStorageFile but elsewhere uses filepath.Join; replace
path.Join(mgmt.DataDir, "idp.db") with filepath.Join(mgmt.DataDir, "idp.db") so
all filesystem joins use the OS-aware filepath package (ensure the filepath
import is present and remove the unused path import if it becomes unused); keep
the rest of the logic that overrides authStorageFile from
c.Server.AuthStore.File unchanged (the symbols to look for are authStorageFile,
mgmt.DataDir, and c.Server.AuthStore.File).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@combined/cmd/config.go`:
- Around line 572-578: The code uses path.Join to build authStorageFile but
elsewhere uses filepath.Join; replace path.Join(mgmt.DataDir, "idp.db") with
filepath.Join(mgmt.DataDir, "idp.db") so all filesystem joins use the OS-aware
filepath package (ensure the filepath import is present and remove the unused
path import if it becomes unused); keep the rest of the logic that overrides
authStorageFile from c.Server.AuthStore.File unchanged (the symbols to look for
are authStorageFile, mgmt.DataDir, and c.Server.AuthStore.File).

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e881e0e and 6f682b0.

📒 Files selected for processing (1)
  • combined/cmd/config.go

@braginini braginini merged commit 403babd into main Mar 3, 2026
43 checks passed
@braginini braginini deleted the feature/add-sql-stores-file-paths branch March 3, 2026 10:53
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.

3 participants