Skip to content

Commit

Permalink
Make the valid_migration function optional
Browse files Browse the repository at this point in the history
In practice I find it is rarely used, hence it is now optional to save
writing an empty function.
  • Loading branch information
pgjones committed Feb 24, 2024
1 parent a522189 commit 7d3fcc0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
11 changes: 6 additions & 5 deletions docs/how_to_guides/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,16 +16,17 @@ 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

This comment has been minimized.

Copy link
@larsblumberg

larsblumberg Mar 4, 2024

Thank you @pgjones, that's a win for a better developer experience. As you state in the commit's message, some projects such as mine don't ever need this function.

with the following signature,

.. code-block:: python
async def valid_migration(connection: quart_db.Connection) -> bool:
...
``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
------------
Expand Down
2 changes: 1 addition & 1 deletion src/quart_db/_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 0 additions & 4 deletions tests/migrations/0.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ async def migrate(connection: Connection) -> None:
options OPTIONS_T
)"""
)


async def valid_migration(connection: Connection) -> bool:
return True

0 comments on commit 7d3fcc0

Please sign in to comment.