Skip to content

Commit

Permalink
Add migrate_data (#1029)
Browse files Browse the repository at this point in the history
  • Loading branch information
dycw authored Jan 3, 2025
1 parent c1e879d commit e9ddc1e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dev = [
"python-dotenv >= 1.0.1, < 1.1",
"redis >= 5.2.1, < 5.3",
"rich >= 13.8.1, < 13.9", # if 13.9, twine upload fails https://github.com/dycw/python-utilities/actions/runs/11125686648/job/30913966455
"scipy >= 1.14.1, < 1.15",
"scipy >= 1.15.0, < 1.16",
"slack-sdk >= 3.34.0, < 3.35",
"sqlalchemy >= 2.0.36, < 2.1",
"streamlit >= 1.41.1, < 1.42",
Expand Down Expand Up @@ -256,7 +256,7 @@ zzz-test-redis = [
"whenever >= 0.6.16, < 0.7",
]
zzz-test-rich = ["rich >= 13.8.1, < 13.9"]
zzz-test-scipy = ["scipy >= 1.14.1, < 1.15"]
zzz-test-scipy = ["scipy >= 1.15.0, < 1.16"]
zzz-test-sentinel = []
zzz-test-slack-sdk = [
"aiohttp >= 3.11.7, < 3.12", # for slack-sdk
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ rpds-py==0.22.3
# via
# jsonschema
# referencing
scipy==1.14.1
scipy==1.15.0
# via
# dycw-utilities (pyproject.toml)
# clarabel
Expand Down
2 changes: 1 addition & 1 deletion requirements/cvxpy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pytest-xdist==3.6.1
# via dycw-utilities (pyproject.toml)
qdldl==0.1.7.post5
# via osqp
scipy==1.14.1
scipy==1.15.0
# via
# clarabel
# cvxpy
Expand Down
2 changes: 1 addition & 1 deletion requirements/scipy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pytest-rerunfailures==15.0
# via dycw-utilities (pyproject.toml)
pytest-xdist==3.6.1
# via dycw-utilities (pyproject.toml)
scipy==1.14.1
scipy==1.15.0
# via dycw-utilities (pyproject.toml)
sortedcontainers==2.4.0
# via hypothesis
Expand Down
42 changes: 42 additions & 0 deletions src/tests/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
insert_items,
is_orm,
is_table_or_orm,
migrate_data,
selectable_to_string,
upsert_items,
yield_primary_key_columns,
Expand Down Expand Up @@ -839,6 +840,47 @@ def test_error_snake_non_unique_error(self, *, id_: int, value: bool) -> None:
_ = _map_mapping_to_table(mapping, table, snake=True)


class TestMigrateData:
@FLAKY
@given(
data=data(),
names=sets(_table_names(), min_size=2, max_size=2),
values=lists(
tuples(integers(0, 10), booleans() | none()),
min_size=1,
unique_by=lambda x: x[0],
),
)
@settings_with_reduced_examples(phases={Phase.generate})
async def test_main(
self, *, data: DataObject, names: set[str], values: list[tuple[int, bool]]
) -> None:
engine1 = await sqlalchemy_engines(data)
name1, name2 = names
table1 = self._make_table(name1)
await insert_items(
engine1, [({"id_": id_, "value": v}, table1) for id_, v in values]
)
async with engine1.begin() as conn:
result1 = (await conn.execute(select(table1))).all()
assert len(result1) == len(values)

engine2 = await sqlalchemy_engines(data)
table2 = self._make_table(name2)
await migrate_data(table1, engine1, engine2, table_or_orm_to=table2)
async with engine2.begin() as conn:
result2 = (await conn.execute(select(table2))).all()
assert len(result2) == len(values)

def _make_table(self, name: str, /) -> Table:
return Table(
name,
MetaData(),
Column("id_", Integer, primary_key=True),
Column("value", Boolean, nullable=True),
)


class TestNormalizeInsertItem:
@given(case=sampled_from(["tuple", "dict"]), id_=integers(0, 10))
def test_pair_of_tuple_or_str_mapping_and_table(
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.88.10"
__version__ = "0.88.11"
Loading

0 comments on commit e9ddc1e

Please sign in to comment.