Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit f62f664

Browse files
author
Ilya Gurov
authored
fix: alembic migration fails in case of a sequential upgrade (#200)
1 parent a986949 commit f62f664

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,21 @@ with engine.begin() as connection:
104104

105105
SQLAlchemy uses [Alembic](https://alembic.sqlalchemy.org/en/latest/#) tool to organize database migrations.
106106

107+
Spanner dialect doesn't provide a default migration environment, it's up to user to write it. One thing to be noted here - one should explicitly set `alembic_version` table not to use migration revision id as a primary key:
108+
```python
109+
with connectable.connect() as connection:
110+
context.configure(
111+
connection=connection,
112+
target_metadata=target_metadata,
113+
version_table_pk=False, # don't use primary key in the versions table
114+
)
115+
```
116+
As Spanner restricts changing a primary key value, not setting the flag to `False` can cause migration problems.
117+
107118
**Warning!**
108119
A migration script can produce a lot of DDL statements. If each of the statements are executed separately, performance issues can occur. To avoid these, it's highly recommended to use the [Alembic batch context](https://alembic.sqlalchemy.org/en/latest/batch.html) feature to pack DDL statements into groups of statements.
109120

121+
110122
## Features and limitations
111123

112124
### Interleaved tables

test_migration_env.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def run_migrations_offline():
6262
target_metadata=target_metadata,
6363
literal_binds=True,
6464
dialect_opts={"paramstyle": "named"},
65+
version_table_pk=False,
6566
)
6667

6768
with context.begin_transaction():
@@ -82,7 +83,11 @@ def run_migrations_online():
8283
)
8384

8485
with connectable.connect() as connection:
85-
context.configure(connection=connection, target_metadata=target_metadata)
86+
context.configure(
87+
connection=connection,
88+
target_metadata=target_metadata,
89+
version_table_pk=False,
90+
)
8691

8792
with context.begin_transaction():
8893
context.run_migrations()

0 commit comments

Comments
 (0)