Skip to content

(fix) cqlengine: handle missing table metadata after schema change in…#744

Open
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:fix/sync-table-metadata-keyerror
Open

(fix) cqlengine: handle missing table metadata after schema change in…#744
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:fix/sync-table-metadata-keyerror

Conversation

@mykaul
Copy link

@mykaul mykaul commented Mar 13, 2026

… sync_table

After CREATE TABLE or ALTER TABLE, the local metadata cache may not yet contain the new table if schema agreement timed out or the automatic metadata refresh was skipped. _sync_table() and _get_table_metadata() unconditionally accessed cluster.metadata.keyspaces[ks].tables[table], which raised KeyError in this case.

Wrap both lookups in try/except KeyError. On miss, force a targeted cluster.refresh_table_metadata() call and retry once. If the table is still not present after the forced refresh, raise a descriptive CQLEngineException instead of a bare KeyError.

This follows the same defensive pattern already used in _sync_type(), which calls cluster.refresh_user_type_metadata() after CREATE TYPE.

Add unit tests for _get_table_metadata verifying: immediate hit (no refresh), successful retry after refresh, and failure after refresh.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds resilience to cqlengine schema management by retrying table metadata lookups after forcing a targeted metadata refresh, and introduces unit tests for the _get_table_metadata retry behavior.

Changes:

  • Add refresh-and-retry handling for missing table metadata in _sync_table.
  • Add refresh-and-retry handling for missing table metadata in _get_table_metadata.
  • Add new unit tests covering _get_table_metadata success/refresh/failure paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
cassandra/cqlengine/management.py Retries table metadata lookup after cluster.refresh_table_metadata(...) in both _sync_table and _get_table_metadata.
tests/unit/cqlengine/test_management.py New unit tests validating _get_table_metadata behavior when metadata is missing and becomes available (or stays missing).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mykaul mykaul force-pushed the fix/sync-table-metadata-keyerror branch from d937ce8 to 6f1be93 Compare March 14, 2026 09:06
… sync_table

After CREATE TABLE or ALTER TABLE, the local metadata cache may not yet
contain the new table if schema agreement timed out or the automatic
metadata refresh was skipped.  _sync_table() and _get_table_metadata()
unconditionally accessed cluster.metadata.keyspaces[ks].tables[table],
which raised KeyError in this case.

Wrap both lookups in try/except KeyError.  On miss, force a targeted
cluster.refresh_table_metadata() call and retry once.  If the table is
still not present after the forced refresh, raise a descriptive
CQLEngineException instead of a bare KeyError.

This follows the same defensive pattern already used in _sync_type(),
which calls cluster.refresh_user_type_metadata() after CREATE TYPE.

Add unit tests for _get_table_metadata verifying: immediate hit (no
refresh), successful retry after refresh, and failure after refresh.
@mykaul mykaul force-pushed the fix/sync-table-metadata-keyerror branch from 6f1be93 to d5c3f18 Compare March 14, 2026 09:18
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