Repository navigation
fix(sqlalchemy-spanner): support spanner_storing in Alembic op.create_index() and compare_indexes() - #18840
Open
rocksnow1942 wants to merge 2 commits into
Open
fix(sqlalchemy-spanner): support spanner_storing in Alembic op.create_index() and compare_indexes()#18840rocksnow1942 wants to merge 2 commits into
rocksnow1942 wants to merge 2 commits into
Conversation
…_index() and compare_indexes()
1. In SpannerDDLCompiler.visit_create_index, support string column names
in spanner_storing without requiring them in index.table.c. When Alembic
executes op.create_index(), the dummy Table only contains the indexed
key columns; looking up storing columns in index.table.c raises KeyError.
2. Register SpannerImpl subclassing alembic.ddl.impl.DefaultImpl for
dialect 'spanner+spanner' when Alembic is installed:
- Override compare_indexes to compare spanner_storing as sets,
avoiding spurious autogenerate diffs when reflected columns
are reordered.
- Override create_index to ensure string storing columns are appended
to index.table.c as NullType columns if missing.
Fixes googleapis#18835
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces Alembic DDL support for Google Cloud Spanner, specifically implementing index comparison and creation with STORING columns. It adds SpannerImpl (inheriting from Alembic's DefaultImpl) to handle comparing indexes with storing columns and dynamically appending missing storing columns to the table schema during index creation. Unit tests are added to verify these behaviors and edge cases. Feedback was provided to simplify column name extraction in SpannerImpl.create_index by directly accessing col.name instead of using getattr, ensuring consistency with other parts of the codebase.
This branch has not been deployed
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.
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #18835 🦕
Summary
This PR resolves two issues when using
sa.Index(..., spanner_storing=[...])with Alembic:op.create_index(..., spanner_storing=["col"])raisingKeyErroron string column names:op.create_index("ix", "tbl", ["c1"], spanner_storing=["c2"])runs inside an Alembic migration,alembic.operations.schemaobj.SchemaObjects.indexconstructs an ad-hocTable("tbl")containing only the indexed key columns (["c1"]).SpannerDDLCompiler.visit_create_indexpreviously resolved string column names viaindex.table.c[col], raisingKeyError: 'c2'. It now extracts the column name directly (c if isinstance(c, str) else c.name).SpannerImpl.create_indexappends missing storing columns toindex.table.casNullTypecolumns (matching Alembic'sPostgresqlImpl.create_indexandMSSQLImpl.create_index).Alembic
--autogenerateignoringspanner_storingchanges:SpannerImpl(DefaultImpl)for"spanner+spanner"when Alembic is installed, overridingcompare_indexesto comparespanner_storingcolumn sets (set(m_storing) != set(r_storing)). Comparing as sets detects additions, removals, and modifications ofSTORINGcolumns while avoiding false-positive diffs when Spanner reflection returnsSTORINGcolumns in table-column order.