From 7d3fcc00eaf046ffeee04a34778859a176a55707 Mon Sep 17 00:00:00 2001 From: pgjones Date: Sat, 24 Feb 2024 16:15:57 +0000 Subject: [PATCH] Make the valid_migration function optional In practice I find it is rarely used, hence it is now optional to save writing an empty function. --- docs/how_to_guides/migrations.rst | 11 ++++++----- src/quart_db/_migration.py | 2 +- tests/migrations/0.py | 4 ---- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/how_to_guides/migrations.rst b/docs/how_to_guides/migrations.rst index d5e20e2..849de47 100644 --- a/docs/how_to_guides/migrations.rst +++ b/docs/how_to_guides/migrations.rst @@ -5,8 +5,8 @@ Migrations Quart-DB considers migrations to be linear and forward only, as such migrations are numbered from 0, to 1, onwards. Each migration is a -python file containing two functions, the first ``migrate`` has the -following signature, +python file containing a ``migrate`` function with the following +signature, .. code-block:: python @@ -16,7 +16,8 @@ following signature, ``migrate`` should run whatever queries are required for the migration. -The second ``valid_migration`` has the following signature, +The file can also contain an optional ``valid_migration`` function +with the following signature, .. code-block:: python @@ -24,8 +25,8 @@ The second ``valid_migration`` has the following signature, ... ``valid_migration`` should check that the migration was successful and -the data in the database is as expected. It can just ``return True`` -if this is something you'd prefer to skip. +the data in the database is as expected. It can be omitted if this is +something you'd prefer to skip. Transactions ------------ diff --git a/src/quart_db/_migration.py b/src/quart_db/_migration.py index 7548a0d..c8de6fb 100644 --- a/src/quart_db/_migration.py +++ b/src/quart_db/_migration.py @@ -75,7 +75,7 @@ async def _run_migration(connection: ConnectionABC, migration: int, migrations_p module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) await module.migrate(connection) - valid = await module.valid_migration(connection) + valid = not hasattr(module, "valid_migration") or await module.valid_migration(connection) if not valid: raise MigrationFailedError(f"Migration {migration} is not valid") diff --git a/tests/migrations/0.py b/tests/migrations/0.py index e13ecbd..b69cb36 100644 --- a/tests/migrations/0.py +++ b/tests/migrations/0.py @@ -21,7 +21,3 @@ async def migrate(connection: Connection) -> None: options OPTIONS_T )""" ) - - -async def valid_migration(connection: Connection) -> bool: - return True