Look up namespaced SQL tables by their schema-qualified key - #1794
Open
DMZ22 wants to merge 1 commit into
Open
Conversation
The SQL adapter builds its metadata with MetaData(schema=namespace), so SQLAlchemy keys each table as "<namespace>.<name>" (Table.key). Every metadata.tables lookup used the bare table name, which raised "KeyError: '<table>'" as soon as a namespace was set — writing, reading or deleting a table in a schema was impossible. Resolve the lookup key through a helper that prepends the namespace when one is configured, matching how the tables are actually keyed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Writing a resource to a table inside a namespace (schema) raised
KeyError:Cause.
SqlAdapter.__init__buildsMetaData(schema=self.control.namespace). When a schema is set, SQLAlchemy keys every table by its schema-qualified name —Table.keyis"<namespace>.<name>", andmetadata.tablesis keyed the same way:But every lookup in the adapter used the bare name —
self.metadata.tables[table_name]— sowrite_row_stream,read_schema,read_cell_stream,delete_resourceandwrite_resource_with_metadataallKeyErrorthe moment a namespace is in play.Fix. Route the six lookups through a small helper that prepends the namespace when one is configured, matching how the tables are actually keyed:
No behaviour changes when
namespaceisNone(the helper returns the name unchanged), so the existing non-namespaced paths are untouched.Verification
Reproduced and fixed end-to-end on SQLite with an attached schema (no server needed):
Added
test_sql_adapter_namespace_issue_1602in the adapter spec's Bugs section — it attaches a second SQLite database as a schema, writes a package into it, and reads the rows back. Reverting onlyadapter.pywhile keeping the test makes it fail with the originalKeyError, so it guards the fix. The full SQL adapter suite is 38 passed / 0 failed (59 skipped are the server-backed backends).ruff format --checkis clean, andruff checkreports the same count onadapter.pybefore and after, so this adds no new lint.