async def test_migration_change_value(settings, notes, db): migration_settings = MigrationSettings( connection_uri=settings.mongodb_dsn, database_name=settings.mongodb_db_name, path="tests/migrations/migrations_for_test/change_value", ) await run_migrate(migration_settings) await init_beanie(database=db, document_models=[Note]) inspection = await Note.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await Note.find_one({"title": "five"}) assert note is not None note = await Note.find_one({"title": "5"}) assert note is None migration_settings.direction = RunningDirections.BACKWARD await run_migrate(migration_settings) inspection = await Note.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await Note.find_one({"title": "5"}) assert note is not None note = await Note.find_one({"title": "five"}) assert note is None
async def test_migration_free_fall(settings, notes, db): if not db.client.is_mongos and not len(db.client.nodes) > 1: return pytest.skip( "MongoDB server does not support transactions as it is neighter a mongos instance not a replica set." ) migration_settings = MigrationSettings( connection_uri=settings.mongodb_dsn, database_name=settings.mongodb_db_name, path="tests/migrations/migrations_for_test/free_fall", ) await run_migrate(migration_settings) await init_beanie(database=db, document_models=[Note]) inspection = await Note.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await Note.find_one({}) assert note.title == "0" migration_settings.direction = RunningDirections.BACKWARD await run_migrate(migration_settings) inspection = await OldNote.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await OldNote.find_one({}) assert note.name == "0"
async def test_migration_by_two(settings, notes, db): migration_settings = MigrationSettings( connection_uri=settings.mongodb_dsn, database_name=settings.mongodb_db_name, path="tests/migrations/migrations_for_test/many_migrations", distance=2, ) await init_beanie(database=db, document_models=[Note]) await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in ["one", "two", "3", "4", "5", "6", "7"] await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in ["one", "two", "three", "4", "5", "6", "7"] await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in [ "one", "two", "three", "four", "five", "six", "seven", ] await run_migrate(migration_settings) migration_settings.direction = RunningDirections.BACKWARD await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in ["one", "two", "three", "4", "5", "6", "7"] await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in ["one", "two", "3", "4", "5", "6", "7"] await run_migrate(migration_settings) async for note in Note.find_all(): assert note.title in ["1", "2", "3", "4", "5", "6", "7"] await run_migrate(migration_settings)
async def test_migration_pack_unpack(settings, notes, db): migration_settings = MigrationSettings( connection_uri=settings.mongodb_dsn, database_name=settings.mongodb_db_name, path="tests/migrations/migrations_for_test/pack_unpack", ) await run_migrate(migration_settings) await init_beanie(database=db, document_models=[Note]) inspection = await Note.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await Note.find_one({}) assert note.tag.name == "test" migration_settings.direction = RunningDirections.BACKWARD await run_migrate(migration_settings) inspection = await OldNote.inspect_collection() assert inspection.status == InspectionStatuses.OK note = await OldNote.find_one({}) assert note.tag_name == "test"