Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions estuary-cdk/estuary_cdk/capture/base_capture_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ async def open(
async def acknowledge(self, acknowledge: request.Acknowledge) -> None:
return None # No-op.

async def migrate_connector_state(
self,
log: FlowLogger,
open: request.Open[EndpointConfig, ResourceConfig, _ConnectorState],
) -> tuple[_ConnectorState, bool]:
"""
Migrate the full connector state across all bindings before processing begins.

Override this method to perform state migrations that affect multiple bindings
or require atomic updates across the entire connector state. This ensures
migrations complete before individual binding tasks start, preventing
interleaved checkpoints from causing state conflicts.

This method receives the full open request, providing access to:
- open.state: The connector state with all bindings
- open.capture.config: The endpoint configuration
- open.capture.bindings: The binding definitions

Common use cases:
- Migrating from flat to dict-based state structures
- Restructuring state keys or formats based on config changes
- Adding new required state fields
- Consolidating or splitting state entries

Args:
log: Logger for the migration process
open: The open request containing state, config, and bindings

Returns:
tuple of (migrated_state, state_was_modified)
- migrated_state: The transformed state (same object or new instance)
- state_was_modified: True if state was changed and needs checkpointing

Default implementation returns (open.state, False) for backward compatibility.
"""
return (open.state, False)

async def handle(
self,
log: FlowLogger,
Expand All @@ -94,6 +131,15 @@ async def handle(
await self._emit(Response(applied=await self.apply(log, apply)))

elif open := request.open:
# Migrate state across all bindings before opening
migrated_state, state_was_modified = await self.migrate_connector_state(log, open)

if state_was_modified:
# Checkpoint the migrated state with full replacement to clear old structures
await self._checkpoint(migrated_state, merge_patch=False)
# Update the open request to use migrated state
open.state = migrated_state
Comment on lines +139 to +141
Copy link
Member

Choose a reason for hiding this comment

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

Per the capture protocol lifecycle (described here), captures should respond with Response.Opened before sending a Response.Checkpoint. Meaning, this migration logic that requires checkpointing should be after the line where the CDK sends Response.Opened:

await self._emit(Response(opened=opened))


opened, capture = await self.open(log, open)
await self._emit(Response(opened=opened))

Expand Down
Loading
Loading