Skip to content

refactor(storage): narrow backend interfaces#182

Merged
SantiagoDePolonia merged 2 commits intomainfrom
refactor/storage-narrow-interfaces
Mar 26, 2026
Merged

refactor(storage): narrow backend interfaces#182
SantiagoDePolonia merged 2 commits intomainfrom
refactor/storage-narrow-interfaces

Conversation

@SantiagoDePolonia
Copy link
Copy Markdown
Contributor

@SantiagoDePolonia SantiagoDePolonia commented Mar 26, 2026

Summary

  • shrink storage.Storage to lifecycle-only and replace backend-specific Type()/any accessors with narrow typed interfaces
  • remove repeated storage config translation from feature factories by centralizing it in config.StorageConfig.BackendConfig()
  • update auditlog, usage, batch, aliases, and executionplans factories/readers to dispatch on typed backends directly

Validation

  • go test ./...
  • make test-e2e
  • make lint

Summary by CodeRabbit

  • Refactor
    • Centralized storage configuration and sensible defaults for SQLite/Postgres/MongoDB to ensure consistent backend setup.
    • Unified backend selection and dispatch across components for more consistent behavior when initializing storage backends.
    • Split generic storage API into backend-specific interfaces (typed accessors) to clarify connections and simplify lifecycle handling.
    • Tests updated to use the new backend accessors.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 21f7ab6a-741f-4e9d-9d35-6a906b4e4855

📥 Commits

Reviewing files that changed from the base of the PR and between 28a3d05 and afcc23c.

📒 Files selected for processing (7)
  • internal/aliases/factory.go
  • internal/auditlog/factory.go
  • internal/auditlog/reader_factory.go
  • internal/batch/factory.go
  • internal/executionplans/factory.go
  • internal/storage/storage.go
  • internal/usage/factory.go

📝 Walkthrough

Walkthrough

Refactors storage configuration and backend dispatch: adds StorageConfig.BackendConfig(), simplifies Storage to lifecycle-only, introduces typed backend interfaces (SQLiteStorage, PostgreSQLStorage, MongoDBStorage) and ResolveBackend[T], and updates multiple factories/tests to use typed backend resolution instead of string-based switches.

Changes

Cohort / File(s) Summary
Config
config/config.go
Added StorageConfig.BackendConfig() to produce an internal storage.Config with defaults for type, SQLite path, and MongoDB database.
Core storage types & resolver
internal/storage/storage.go
Replaced introspection-style Storage with lifecycle-only Close(); added SQLiteStorage, PostgreSQLStorage, MongoDBStorage typed interfaces; added generic ResolveBackend[T] to centralize backend dispatch.
SQLite implementation
internal/storage/sqlite.go, internal/storage/sqlite_test.go
Constructor now returns SQLiteStorage; renamed accessor SQLiteDB()DB(); removed cross-backend accessors; tests updated to use DB().
PostgreSQL implementation
internal/storage/postgresql.go
Constructor now returns PostgreSQLStorage; removed Type/SQLite/Postgres/Mongo accessor methods from implementation.
MongoDB implementation
internal/storage/mongodb.go
Constructor now returns MongoDBStorage; removed cross-backend accessor methods from implementation.
Factory refactors (aliases, auditlog, batch, executionplans, usage)
internal/aliases/factory.go, internal/auditlog/factory.go, internal/auditlog/reader_factory.go, internal/batch/factory.go, internal/executionplans/factory.go, internal/usage/factory.go
Replaced local buildStorageConfig usage with cfg.Storage.BackendConfig(); removed per-file type switches and nil/type assertions; delegate backend construction to storage.ResolveBackend[...] with typed callbacks; added database/sql imports where required.
Batch SQLite test
internal/batch/store_sqlite_test.go
Test updated to pass st.DB() into SQLite batch store constructor (reflecting accessor rename).

Sequence Diagram(s)

sequenceDiagram
  participant Factory as Factory (component)
  participant Resolver as storage.ResolveBackend
  participant SQLite as SQLite backend
  participant Postgres as PostgreSQL backend
  participant Mongo as MongoDB backend

  Factory->>Resolver: call ResolveBackend[T](store, sqliteCb, pgCb, mongoCb)
  alt store implements SQLiteStorage
    Resolver->>SQLite: sqliteCb(*sql.DB)
    SQLite-->>Resolver: T (store-specific)
  else store implements PostgreSQLStorage
    Resolver->>Postgres: pgCb(*pgxpool.Pool)
    Postgres-->>Resolver: T (store-specific)
  else store implements MongoDBStorage
    Resolver->>Mongo: mongoCb(*mongo.Database)
    Mongo-->>Resolver: T (store-specific)
  end
  Resolver-->>Factory: returns T or error
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Suggested labels

enhancement

Poem

🐰 I hopped through configs, neat and spry,
Typed backends now, no strings to tie.
ResolveBackend hummed a tidy song,
Factories danced and errors shrank long.
Close the store — the rabbit waves goodbye. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(storage): narrow backend interfaces' accurately reflects the primary change: simplifying the Storage interface and using narrowly typed backend-specific interfaces instead of introspection methods.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/storage-narrow-interfaces

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 `@internal/storage/storage.go`:
- Around line 67-83: Add a centralized backend dispatch helper in
internal/storage (e.g., func WithBackendDispatch(s Storage, sqliteFn
func(*sql.DB) error, pgFn func(*pgxpool.Pool) error, mongoFn
func(*mongo.Database) error) error or ResolveBackend returning a discriminated
result) that contains the SQLite/PostgreSQL/MongoDB type switch currently
duplicated across factory.go files; use the existing SQLiteStorage,
PostgreSQLStorage and MongoDBStorage interfaces to detect and extract
DB/Pool/Database and call the appropriate callback, then update
internal/auditlog/factory.go, internal/usage/factory.go and
internal/aliases/factory.go to call this helper instead of repeating the type
switch so future backends are added in one place.
🪄 Autofix (Beta)

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: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0fb607ba-a243-4c48-8877-75fa57c47b29

📥 Commits

Reviewing files that changed from the base of the PR and between a478572 and 28a3d05.

📒 Files selected for processing (13)
  • config/config.go
  • internal/aliases/factory.go
  • internal/auditlog/factory.go
  • internal/auditlog/reader_factory.go
  • internal/batch/factory.go
  • internal/batch/store_sqlite_test.go
  • internal/executionplans/factory.go
  • internal/storage/mongodb.go
  • internal/storage/postgresql.go
  • internal/storage/sqlite.go
  • internal/storage/sqlite_test.go
  • internal/storage/storage.go
  • internal/usage/factory.go

Comment thread internal/storage/storage.go
@SantiagoDePolonia SantiagoDePolonia merged commit c4bb2eb into main Mar 26, 2026
16 checks passed
@SantiagoDePolonia SantiagoDePolonia deleted the refactor/storage-narrow-interfaces branch April 1, 2026 21:29
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.

1 participant