Skip to content

chore!: remove deprecated init/0 callback and atom metadata key - #35

Merged
yordis merged 1 commit into
mainfrom
remove-deprecated-v3-features
Nov 20, 2025
Merged

yordis merged 1 commit into
mainfrom
remove-deprecated-v3-features

Conversation

@yordis

@yordis yordis commented Nov 20, 2025 •

Copy link
Copy Markdown
Member

Note

Remove deprecated init/0 callback across handler and tests; enforce binary metadata keys in Pipeline; update example handler and test configurations.

  • Event Handling:
    • Remove all support for deprecated init/0 in Commanded.Event.Handler (callback, invocation, warnings, and optional callbacks).
    • Keep after_start/1 as the initialization path; subscribed flow now calls only after_start/1.
  • Middleware:
    • Enforce binary metadata keys in Commanded.Middleware.Pipeline.assign_metadata/3 (docs and guard updated).
  • Examples:
    • Replace init/0 with after_start/1 in BankAccountBatchHandler; return {:stop, reason} on failure; add @impl annotations.
  • Tests:
    • Remove tests and support module for deprecated init/0.
    • Add/adjust tests for after_start/1; tweak log capture levels where needed.
  • Config:
    • Set test logger level to :info (from :debug).

Written by Cursor Bugbot for commit 228399e. This will update automatically on new commits. Configure here.

@coderabbitai

coderabbitai Bot commented Nov 20, 2025 •

Copy link
Copy Markdown

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Removes the deprecated init/0 callback from the event handler behaviour, exposes init/1/handle/2/handle_batch/1 in optional callbacks, simplifies after_start/1 to always return :ok, narrows assign_metadata/3 to binary keys, deletes init/0 tests and test helper modules, and adjusts test logging config/usage.

Changes

Cohort / File(s) Summary
Event handler behaviour
lib/commanded/event/handler.ex
Remove public init/0 and its deprecation paths; update @optional_callbacks to include init: 1, handle: 2, handle_batch: 1; simplify after_start handling to always call/return :ok and remove deprecated logging.
Middleware guard change
lib/commanded/middleware/pipeline.ex
Tighten assign_metadata/3 guard to when is_binary(key) and update docs to require binary keys (atoms no longer accepted).
Example handler startup & clauses
test/example_domain/bank_account/bank_account_batch_handler.ex
Replace init/0 with after_start/1, add @impl Handler annotations, add handle_batch([]) -> :ok, and stop process on non-:ok return from startup.
Test cleanup & logging adjustments
test/event_handler_after_start_test.exs, test/event/handler_init_test.exs, test/event/support/init/init_handler.ex, test/aggregates/aggregate_test.exs, config/test.exs
Remove tests, helpers, imports and mocks exercising init/0; update test logging level and capture_log usage; delete Commanded.Event.InitHandler test module; change test logger default to :info and enable debug capture where needed.

Sequence Diagram(s)

sequenceDiagram
  participant Supervisor
  participant Subscription
  participant Handler as EventHandler

  rect rgb(250,240,245)
    Note over Supervisor,Subscription: Old flow (with deprecated init/0)
    Supervisor->>Subscription: start subscription
    Subscription->>Handler: {:subscribed, _meta}
    Handler->>Handler: (deprecated) call init/0
    alt init/0 returns :ok
      Handler->>Handler: call after_start(state) -> :ok
      Handler->>Supervisor: continue
    else init/0 returns {:stop, reason}
      Handler->>Supervisor: stop(reason)
    end
  end

  rect rgb(245,255,250)
    Note over Supervisor,Subscription: New flow (init/0 removed)
    Supervisor->>Subscription: start subscription
    Subscription->>Handler: {:subscribed, _meta}
    Handler->>Handler: call after_start(state) -> :ok
    Handler->>Supervisor: continue (no init/0 path)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • Inspect lib/commanded/event/handler.ex for any lingering init/0 references and correct @optional_callbacks.
  • Verify subscription/startup flows and stop semantics in handlers (including example BankAccountBatchHandler).
  • Check calls/tests for assign_metadata/3 that previously used atom keys.

Possibly related PRs

Poem

🐇 I hopped through code at break of dawn,
old init/0 quietly withdrawn.
after_start now greets the day,
binary keys lead the way.
I nibble tests and bound along.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main breaking changes: removal of deprecated init/0 callback and enforcement of binary metadata keys.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, detailing the removal of init/0, migration to after_start/1, metadata key enforcement, and config/test updates.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch remove-deprecated-v3-features

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.

@yordis
yordis marked this pull request as ready for review November 20, 2025 21:29
@yordis
yordis force-pushed the remove-deprecated-v3-features branch from 8f80856 to d7fbe24 Compare November 20, 2025 21:32
Comment thread test/event/handler_init_test.exs
@yordis
yordis force-pushed the remove-deprecated-v3-features branch from d7fbe24 to 0b969c0 Compare November 20, 2025 21:54

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
lib/commanded/middleware/pipeline.ex (1)

69-78: assign_metadata/3 now intentionally enforces binary keys

The updated docstring and when is_binary(key) guard line up with the goal of disallowing atom metadata keys. Existing middleware that passes atom keys will now fail with a FunctionClauseError, which is acceptable for a breaking change but worth calling out in upgrade notes. If you want a clearer failure mode, you could instead accept any key here and raise an ArgumentError when it’s not a binary.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7fbe24 and 0b969c0.

📒 Files selected for processing (5)
  • lib/commanded/event/handler.ex (2 hunks)
  • lib/commanded/middleware/pipeline.ex (2 hunks)
  • test/event/handler_init_test.exs (1 hunks)
  • test/event/support/init/init_handler.ex (0 hunks)
  • test/event_handler_after_start_test.exs (0 hunks)
💤 Files with no reviewable changes (2)
  • test/event/support/init/init_handler.ex
  • test/event_handler_after_start_test.exs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Quality Assurance (1.18.x, 27)
🔇 Additional comments (3)
test/event/handler_init_test.exs (1)

5-5: Alias cleanup is consistent with remaining tests

The alias list now only includes modules actually referenced in this file (EchoHandler, Handler, ReplyEvent, RuntimeConfigHandler), which matches the removal of init/0-specific tests and support modules.

lib/commanded/event/handler.ex (2)

616-621: @optional_callbacks updated to drop init/0 from the behaviour

Only init/1 is kept as an optional callback, which matches the behaviour definitions in this module and the removal of any @callback init/0. This cleanly severs the old init/0 API surface while preserving the existing optional callbacks for error handling, partitioning, and batch/handle variants.


708-711: Default after_start/1 is now a no-op, no longer bridging to init/0

after_start/1 now simply returns :ok, so handlers that don’t override it will behave as before minus the legacy init/0 indirection. Custom handlers that rely on after_start/1 still get the same callback contract (:ok / {:ok, state} / {:stop, reason}); handlers that were depending on init/0 being invoked must now migrate to init/1 and/or after_start/1, which is consistent with the breaking-change intent of this PR.

@yordis
yordis force-pushed the remove-deprecated-v3-features branch 2 times, most recently from a261b91 to d54240e Compare November 20, 2025 22:05
Comment thread test/example_domain/bank_account/bank_account_batch_handler.ex
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
@yordis
yordis force-pushed the remove-deprecated-v3-features branch from d54240e to 228399e Compare November 20, 2025 22:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
test/event/handler_init_test.exs (1)

5-5: Orphaned test support file.

The file test/event/support/init/init_handler.ex defining InitHandler with the deprecated init/0 callback should be deleted as it's no longer referenced.

🧹 Nitpick comments (1)
test/example_domain/bank_account/bank_account_batch_handler.ex (1)

27-28: Consider removing the empty batch clause.

The empty batch handler at line 28 appears unnecessary since handle_batch/1 at line 31 recursively processes the list and naturally terminates when the list is empty. The explicit empty clause adds no additional behavior.

Apply this diff to remove the redundant clause:

-  @impl Handler
-  def handle_batch([]), do: :ok
-
   @impl Handler
   def handle_batch([{%BankAccountOpened{} = event, _metadata} | rest]) do
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b969c0 and 228399e.

📒 Files selected for processing (8)
  • config/test.exs (1 hunks)
  • lib/commanded/event/handler.ex (2 hunks)
  • lib/commanded/middleware/pipeline.ex (2 hunks)
  • test/aggregates/aggregate_test.exs (1 hunks)
  • test/event/handler_init_test.exs (1 hunks)
  • test/event/support/init/init_handler.ex (0 hunks)
  • test/event_handler_after_start_test.exs (0 hunks)
  • test/example_domain/bank_account/bank_account_batch_handler.ex (1 hunks)
💤 Files with no reviewable changes (2)
  • test/event_handler_after_start_test.exs
  • test/event/support/init/init_handler.ex
🧰 Additional context used
🧬 Code graph analysis (1)
test/example_domain/bank_account/bank_account_batch_handler.ex (2)
lib/commanded/event/handler.ex (4)
  • after_start (709-711)
  • start_link (656-662)
  • start_link (830-854)
  • handle_batch (1126-1128)
test/event_handler_after_start_test.exs (1)
  • after_start (23-30)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Quality Assurance (1.18.x, 27)
  • GitHub Check: Cursor Bugbot
🔇 Additional comments (11)
lib/commanded/middleware/pipeline.ex (1)

72-74: Verified: Breaking change is safe; no atom key usage found in codebase.

The removal of atom key support has been verified. The only existing call to assign_metadata in the codebase (in tests) already uses a binary key ("updated_by"), confirming no callers will break from the narrowed guard clause. The implementation is correct.

config/test.exs (1)

6-7: LGTM!

Raising the global logger level to :info reduces log noise during tests while still allowing individual tests to capture debug logs when needed (via ExUnit's capture_log option at line 11).

test/aggregates/aggregate_test.exs (1)

41-47: LGTM!

Correctly coordinates with the global logger level change. The Logger.configure(level: :debug) ensures debug logs are emitted, and capture_log([level: :debug]) captures them for assertion.

test/event/handler_init_test.exs (1)

8-44: LGTM!

The test suite correctly focuses on init/1 callback functionality and runtime configuration, with all init/0 related tests properly removed.

lib/commanded/event/handler.ex (3)

616-621: LGTM!

Correctly updates the optional callbacks to reflect the API changes: removes init: 0, retains init: 1, and adds handle: 2 and handle_batch: 1 which have default implementations generated by __before_compile__.


709-710: LGTM!

Default after_start/1 simplified to a no-op, removing the deprecated init/0 fallback path. This aligns with the PR's breaking changes.


915-936: LGTM!

The subscription handler correctly invokes only after_start/1, with proper handling of all valid return values. The deprecated init/0 path and deprecation warning have been cleanly removed.

test/example_domain/bank_account/bank_account_batch_handler.ex (4)

10-10: LGTM!

Alias added to support the @impl annotations introduced throughout the file.


13-20: LGTM!

Correctly migrates from init/0 to after_start/1 and uses the proper return value {:stop, :unable_to_start} on error. This addresses the past review comment about the invalid return value.


22-25: LGTM!

Proper @impl annotation added to clarify this implements the Handler behaviour callback.


30-39: LGTM!

Proper @impl annotation and correct batch processing logic with tail-recursive pattern.

@yordis
yordis merged commit 4762803 into main Nov 20, 2025
6 of 7 checks passed
@yordis
yordis deleted the remove-deprecated-v3-features branch November 20, 2025 22:22
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