Skip to content

Refactor storage initialization to reduce code duplication - #1089

Merged
DZakh merged 2 commits into
mainfrom
claude/codegen-persistence-library-i1Cd6
Apr 6, 2026
Merged

Refactor storage initialization to reduce code duplication#1089
DZakh merged 2 commits into
mainfrom
claude/codegen-persistence-library-i1Cd6

Conversation

@DZakh

@DZakh DZakh commented Apr 6, 2026

Copy link
Copy Markdown
Member

Summary

This PR refactors the storage and persistence initialization logic by extracting common patterns into reusable functions in PgStorage, eliminating significant code duplication across the codebase.

Key Changes

  • Added new factory functions in PgStorage.res:

    • makeStorageFromEnv: Creates a storage instance using environment variables, consolidating the logic previously duplicated in generated code
    • makePersistenceFromConfig: Creates a persistence instance from a config and storage, reducing boilerplate
  • Updated code generation templates:

    • Removed the makeStorage and codegenPersistence functions from generated code
    • Updated generated code to use the new factory functions from PgStorage
    • Simplified the createTestIndexer binding to use configWithoutRegistrations.allEntities directly instead of accessing through codegenPersistence
  • Updated Main.rs to support optional persistence:

    • Changed persistence parameter in Main.start to be optional
    • When not provided, it's created using PgStorage.makePersistenceFromConfig
  • Updated test and migration helpers:

    • Modified Mock.res, DbHelpers.res, and Migrations.res to use the new factory functions
    • Updated references from Indexer.Generated.codegenPersistence to use the new pattern
  • Minor formatting improvements:

    • Fixed line wrapping in PgStorage.res for better readability

Implementation Details

The refactoring maintains backward compatibility while reducing ~80 lines of duplicated storage initialization code. The new factory functions encapsulate all the environment variable reading and Hasura integration logic that was previously repeated in generated code templates.

https://claude.ai/code/session_01K1BtDSAFuEFegzihzFvfhB

Summary by CodeRabbit

Release Notes

  • Refactor
    • Simplified storage and persistence initialization configuration.
    • Removed ClickHouse data sink integration.
    • Removed Hasura schema and table tracking functionality.

Persistence setup (PgStorage wiring, Hasura callbacks, ClickHouse sink)
was identical for every indexer but lived in codegen'd Generated module.
Now PgStorage.makeStorageFromEnv and PgStorage.makePersistenceFromConfig
handle this in library code, and Main.start creates persistence from
config when not explicitly provided.

https://claude.ai/code/session_01K1BtDSAFuEFegzihzFvfhB
@coderabbitai

coderabbitai Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The changes consolidate persistence initialization logic by introducing helper functions in PgStorage (makeStorageFromEnv, makePersistenceFromConfig) and refactoring call sites to derive persistence from configuration instead of passing pre-constructed persistence objects through generated code and function parameters.

Changes

Cohort / File(s) Summary
Codegen template & code generation
packages/cli/src/hbs_templating/codegen_templates.rs, packages/cli/templates/static/codegen/src/Index.res
Removed codegenPersistence from generated module Generated and stopped passing ~persistence to Main.start; removed ~allEntities argument from createTestIndexer binding.
Storage & persistence construction helpers
packages/envio/src/PgStorage.res
Added makeStorageFromEnv (wires env config and optional Hasura callbacks) and makePersistenceFromConfig (derives persistence from config and storage) as new exported helpers; minor formatting adjustments to existing code.
Initialization signature updates
packages/envio/src/Main.res, packages/envio/src/TestIndexer.res
Updated Main.start to accept optional ~persistence with fallback to construct from config; simplified TestIndexer.makeCreateTestIndexer to derive allEntities internally from config instead of requiring it as a parameter.
Migration & persistence initialization
packages/cli/templates/static/codegen/src/db/Migrations.res
Introduced local persistence value constructed via PgStorage.makePersistenceFromConfig; updated resetStorage and runUpMigrations to use this shared instance instead of Indexer.Generated.codegenPersistence.
Test persistence & storage setup
scenarios/test_codegen/test/EventOrigin_test.res, scenarios/test_codegen/test/helpers/DbHelpers.res, scenarios/test_codegen/test/helpers/Mock.res
Updated persistence construction to route through PgStorage helpers (makePersistenceFromConfig, makeStorageFromEnv) instead of using pre-generated codegenPersistence or manual storage/persistence assembly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • JonoPrest
  • MJYoung114

Poem

🐰 No more persistence passed down the chain,
Storage helpers shine, simplifying the main—
Config constructs what we need, neat and clean,
The cleanest persistence flow you've ever seen! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main objective of the PR: centralizing storage and persistence initialization logic to reduce code duplication across generated and test modules.

✏️ 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 claude/codegen-persistence-library-i1Cd6

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.

@DZakh
DZakh merged commit 434becb into main Apr 6, 2026
7 of 8 checks passed
@DZakh
DZakh deleted the claude/codegen-persistence-library-i1Cd6 branch April 6, 2026 14:43
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