Truncate ClickHouse tables when CDC mirror reconciliation drops a mirror - #1172
Conversation
…C mirror When reconcileRawAnalyticsMirrors detects a table mapping mismatch and drops a mirror, the ClickHouse destination tables retain data from the previous initial copy. PeerDB then rejects mirror recreation with do_initial_copy=true because the destination tables are non-empty: table device_priority exists and is not empty Truncate the destination tables after dropping the mirror so the subsequent CREATE MIRROR IF NOT EXISTS succeeds. Fixes staging deploy failure in #1171.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
Reviewer's GuideAdds truncation of ClickHouse destination tables when a raw analytics CDC mirror is dropped during reconciliation, wiring the ClickHouse client through setup and verifying the behavior in tests. Sequence diagram for truncating ClickHouse tables when a CDC mirror is droppedsequenceDiagram
participant SetupClickHouseCdc as setupClickHouseCdc
participant PeerDbClient as PeerDbClient
participant ClickHouseClient as ClickHouseCommandClient
participant TruncateTables as truncateClickHouseDestinationTables
SetupClickHouseCdc->>PeerDbClient: reconcileRawAnalyticsMirrors(peerDbClient, clickHouseClient)
activate PeerDbClient
PeerDbClient-->>SetupClickHouseCdc: existingMirrorsWithConfigs
loop eachRawAnalyticsMirror
PeerDbClient->>PeerDbClient: query(DROP MIRROR mirrorName)
PeerDbClient-->>SetupClickHouseCdc: mirrorDropped
SetupClickHouseCdc->>TruncateTables: truncateClickHouseDestinationTables(clickHouseClient, tableNames)
activate TruncateTables
loop eachTableName
TruncateTables->>ClickHouseClient: command(TRUNCATE TABLE IF EXISTS postgres_fitness.tableName)
end
deactivate TruncateTables
end
deactivate PeerDbClient
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesMirror Reconciliation Cleanup
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
🚥 Pre-merge checks | ✅ 1 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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. Comment |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The
truncateClickHouseDestinationTableshelper hardcodes thepostgres_fitnessschema, which could make this fragile if schemas differ across environments; consider deriving the schema from configuration or from the existing mirror/table mapping instead of embedding it in the query string. - In
truncateClickHouseDestinationTables, you currently truncate tables sequentially in aforloop; usingPromise.allon a mapped array ofclickHouseClient.commandcalls would reduce latency when truncating multiple tables.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `truncateClickHouseDestinationTables` helper hardcodes the `postgres_fitness` schema, which could make this fragile if schemas differ across environments; consider deriving the schema from configuration or from the existing mirror/table mapping instead of embedding it in the query string.
- In `truncateClickHouseDestinationTables`, you currently truncate tables sequentially in a `for` loop; using `Promise.all` on a mapped array of `clickHouseClient.command` calls would reduce latency when truncating multiple tables.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Storybook previews for This comment updates automatically on each PR push. |
Address review feedback: tables are independent and can be truncated concurrently rather than sequentially.
|
Addressed sourcery-ai review feedback:
|
Problem
Staging deploy fails at Configure ClickHouse CDC with:
Root Cause
reconcileRawAnalyticsMirrorsdetects a table mapping mismatch → drops the mirror → SQL template triesCREATE MIRROR IF NOT EXISTSwithdo_initial_copy = true→ PeerDB rejects because destination ClickHouse tables still have data from the previous mirror's initial copy.Fix
After dropping a mirror during reconciliation, truncate all its ClickHouse destination tables so the subsequent
CREATE MIRRORsucceeds.Changes
src/db/clickhouse-cdc.ts: AddtruncateClickHouseDestinationTables(), call it afterDROP MIRRORinreconcileRawAnalyticsMirrorssrc/db/clickhouse-cdc.test.ts: Verify truncate commands fire for the dropped mirror's tables (and not for unaffected mirrors)Summary by Sourcery
Ensure ClickHouse CDC mirror reconciliation can safely recreate mirrors when source table mappings change by clearing stale destination data.
Bug Fixes:
Tests:
Summary by CodeRabbit
Bug Fixes
Tests