Skip to content

refactor(db): move ensureXxxTables() guards from methods to constructors#81

Merged
zbigniewsobiecki merged 1 commit intodevfrom
feature/move-ensure-tables-to-constructors
Mar 24, 2026
Merged

refactor(db): move ensureXxxTables() guards from methods to constructors#81
zbigniewsobiecki merged 1 commit intodevfrom
feature/move-ensure-tables-to-constructors

Conversation

@aaight
Copy link
Copy Markdown
Collaborator

@aaight aaight commented Mar 24, 2026

Summary

  • Moves all ensureXxxTables() guard calls from every public method body to the repository constructors, eliminating 126+ redundant per-method calls
  • Updates IndexDatabase.initialize() to drop all tables (including ones eagerly created by constructors) so the reset sequence remains correct
  • All 2256 tests pass with zero regressions

What was implemented

  • ModuleRepository: ensureModulesTables() moved to constructor; 31 per-method calls removed
  • InteractionRepository: ensureInteractionsTables(), ensureModulesTables(), ensureInteractionDefinitionLinks() moved to constructor; 22+ per-method calls removed
  • FlowRepository: ensureFlowsTables(), ensureInteractionsTables(), ensureModulesTables() moved to constructor; 33+ per-method calls removed
  • InteractionAnalysis: ensureInteractionsTables(), ensureModulesTables() moved to constructor; 15 per-method calls removed
  • CallGraphService: ensureModulesTables() moved to constructor; 3 per-method calls removed
  • FeatureRepository: ensureFeaturesTables(), ensureFlowsTables() moved to constructor; 2 per-method calls removed
  • DomainRepository: ensureDomainsTable() moved to constructor; 12 per-method calls removed
  • IndexDatabase.initialize(): Extended DROP TABLE list to include feature_flows, features, flow_subflow_steps, flow_definition_steps, flow_steps, interaction_definition_links, interactions, contract_participants, contracts — tables that are now eagerly created by repository constructors and must be cleared on initialize

Key decisions

  • Root cause of test failures: Moving ensure calls to constructors caused IndexDatabase to eagerly create tables (including flow_definition_steps which references both flows and definitions) before initialize() was called. SQLite validates ON DELETE CASCADE chains when dropping tables, so DROP TABLE IF EXISTS flows failed with "no such table: main.definitions" because flow_definition_steps referenced both. Fixed by updating initialize() to drop all tables in correct dependency order
  • Safe because idempotent: The ensure* functions use CREATE TABLE IF NOT EXISTS logic — calling them once in the constructor is functionally identical to calling them per-method

Test plan

  • All 2256 tests pass (pnpm test)
  • Build passes with no type errors (pnpm run build)
  • Lint passes with no errors (Biome check)

Trello: https://trello.com/c/OBDKBd0E/33-as-a-developer-i-want-ensurexxxtables-guards-moved-to-repository-constructors-so-that-126-redundant-per-method-guard-calls-are-e

🤖 Generated with Claude Code

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Collaborator

@nhopeatall nhopeatall left a comment

Choose a reason for hiding this comment

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

LGTM — This is a clean mechanical refactoring that correctly moves idempotent ensure*Tables() guard calls from per-method bodies to constructors. The change is safe because:

  1. Idempotency preserved: All ensure* functions use CREATE TABLE IF NOT EXISTS / tableExists() checks, so calling them once in the constructor is functionally equivalent to calling them per-method.

  2. initialize() DROP order is correct: The new DROP statements (feature_flows, features, flow_subflow_steps, flow_definition_steps, flow_steps, interaction_definition_links, interactions, contract_participants, contracts) are added in proper dependency order (child tables before parent tables), which handles the SQLite FK validation issue described in the PR.

  3. Standalone repository usage (tests) is actually improved — tables are guaranteed to exist at construction time rather than relying on the first method call to create them.

  4. CI confirms: All 2256 tests pass, lint clean, types clean.

Minor observation (not blocking): ContractRepository still uses per-method ensureContractsTables() calls (15 occurrences) and wasn't included in this refactoring. Could be a follow-up if desired.

@zbigniewsobiecki zbigniewsobiecki merged commit 311bd42 into dev Mar 24, 2026
2 checks passed
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.

4 participants