Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-2658] Alembic should use batch mode for autogenerate #3223

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reflex/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def run_autogenerate(rev, context):
render_item=cls._alembic_render_item,
process_revision_directives=writer, # type: ignore
compare_type=False,
render_as_batch=True, # for sqlite compatibility
)
env.run_migrations()
changes_detected = False
Expand Down
17 changes: 12 additions & 5 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,23 @@ class AlembicThing(Model, table=True): # type: ignore

sqlmodel.SQLModel.metadata.clear()

# Create column t2
# Create column t2, mark t1 as optional with default
class AlembicThing(Model, table=True): # type: ignore
t1: str
t1: Optional[str] = "default"
t2: str = "bar"

Model.migrate(autogenerate=True)
assert len(list(versions.glob("*.py"))) == 2

with reflex.model.session() as session:
session.add(AlembicThing(t2="baz"))
session.commit()
result = session.exec(sqlmodel.select(AlembicThing)).all()
assert len(result) == 1
assert len(result) == 2
assert result[0].t1 == "foo"
assert result[0].t2 == "bar"
assert result[1].t1 == "default"
assert result[1].t2 == "baz"

sqlmodel.SQLModel.metadata.clear()

Expand All @@ -120,8 +125,9 @@ class AlembicThing(Model, table=True): # type: ignore

with reflex.model.session() as session:
result = session.exec(sqlmodel.select(AlembicThing)).all()
assert len(result) == 1
assert len(result) == 2
assert result[0].t2 == "bar"
assert result[1].t2 == "baz"

# Add table
class AlembicSecond(Model, table=True): # type: ignore
Expand Down Expand Up @@ -158,8 +164,9 @@ class AlembicThing(Model, table=True): # type: ignore
assert errctx.match(r"no such table: alembicsecond")
# first table should still exist
result = session.exec(sqlmodel.select(AlembicThing)).all()
assert len(result) == 1
assert len(result) == 2
assert result[0].t2 == "bar"
assert result[1].t2 == "baz"

sqlmodel.SQLModel.metadata.clear()

Expand Down
Loading