|
| 1 | +"""Using AsyncMigrationTracker for version management.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pytest_databases.docker.postgres import PostgresService |
| 8 | + |
| 9 | +pytestmark = pytest.mark.xdist_group("postgres") |
| 10 | + |
| 11 | +__all__ = ("test_tracker_instance",) |
| 12 | + |
| 13 | + |
| 14 | +async def test_tracker_instance(postgres_service: PostgresService) -> None: |
| 15 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 16 | + migration_dir = Path(temp_dir) / "migrations" |
| 17 | + migration_dir.mkdir() |
| 18 | + |
| 19 | + # start-example |
| 20 | + from sqlspec.adapters.asyncpg import AsyncpgConfig |
| 21 | + from sqlspec.migrations.tracker import AsyncMigrationTracker |
| 22 | + |
| 23 | + # Create tracker with custom table name |
| 24 | + tracker = AsyncMigrationTracker(version_table_name="ddl_migrations") |
| 25 | + |
| 26 | + dsn = ( |
| 27 | + f"postgresql://{postgres_service.user}:{postgres_service.password}" |
| 28 | + f"@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}" |
| 29 | + ) |
| 30 | + config = AsyncpgConfig( |
| 31 | + pool_config={"dsn": dsn}, |
| 32 | + migration_config={ |
| 33 | + "enabled": True, |
| 34 | + "script_location": str(migration_dir), |
| 35 | + "version_table_name": "ddl_migrations", |
| 36 | + "auto_sync": True, # Enable automatic version reconciliation |
| 37 | + }, |
| 38 | + ) |
| 39 | + |
| 40 | + # Use the session to work with migrations |
| 41 | + async with config.provide_session() as session: |
| 42 | + # Ensure the tracking table exists |
| 43 | + await tracker.ensure_tracking_table(session) |
| 44 | + |
| 45 | + # Get current version (None if no migrations applied) |
| 46 | + current = await tracker.get_current_version(session) |
| 47 | + print(f"Current version: {current}") |
| 48 | + # end-example |
| 49 | + |
| 50 | + assert isinstance(tracker, AsyncMigrationTracker) |
| 51 | + assert config.migration_config["version_table_name"] == "ddl_migrations" |
0 commit comments