Skip to content

Commit

Permalink
Add some tests for backward migrations #425
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Nov 27, 2024
1 parent 7488769 commit 230a239
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 76 deletions.
28 changes: 14 additions & 14 deletions inventory_management_system_api/migrations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def find_migration_index(name: str, migration_names: list[str]) -> int:
return migration_names.index(name)


def load_forward_migrations_to(name: str) -> dict[str, BaseMigration]:
def load_migrations_forward_to(name: str) -> dict[str, BaseMigration]:
"""
Returns a list of forward migrations that need to be applied to get from the last migration applied to the database
Returns a list of migrations forward that need to be applied to get from the last migration applied to the database
to the given one inclusive.
:param name: Name of the last forward migration to apply. 'latest' will just use the latest one.
:param name: Name of the last migration forward to apply. 'latest' will just use the latest one.
:returns: List of dicts containing the names and instances of the migrations that need to be applied in the order
they should be applied.
"""
Expand All @@ -111,7 +111,7 @@ def load_forward_migrations_to(name: str) -> dict[str, BaseMigration]:
try:
end_index = find_migration_index(name, available_migrations)
except ValueError:
sys.exit(f"Migration '{name}' was not found in the available list of migrations")
sys.exit(f"Migration '{name}' was not found in the available list of migrations.")

if start_index > end_index:
sys.exit(
Expand All @@ -123,12 +123,12 @@ def load_forward_migrations_to(name: str) -> dict[str, BaseMigration]:
return {name: load_migration(name) for name in available_migrations[start_index : end_index + 1]}


def load_backward_migrations_to(name: str) -> tuple[dict[str, BaseMigration], Optional[str]]:
def load_migrations_backward_to(name: str) -> tuple[dict[str, BaseMigration], Optional[str]]:
"""
Returns a list of backward migrations that need to be applied to get from the last migration applied to the database
Returns a list of migrations backward that need to be applied to get from the last migration applied to the database
to the given one inclusive.
:param name: Name of the last backward migration to apply.
:param name: Name of the last migration backward to apply.
:returns: Tuple containing:
- List of dicts containing the names and instances of the migrations that need to be applied in the order
they should be applied.
Expand All @@ -154,12 +154,12 @@ def load_backward_migrations_to(name: str) -> tuple[dict[str, BaseMigration], Op
try:
end_index = find_migration_index(name, available_migrations) - 1
except ValueError:
sys.exit(f"Migration '{name}' was not found in the available list of migrations")
sys.exit(f"Migration '{name}' was not found in the available list of migrations.")

if start_index <= end_index:
sys.exit(
f"Migration '{name}' is already reverted or after the previous migration applied '{previous_migration}. "
"So there is nothing to migrate.'"
f"Migration '{name}' is already reverted or after the previous migration applied '{previous_migration}'. "
"So there is nothing to migrate."
)

final_previous_migration_name = available_migrations[end_index] if end_index >= 0 else None
Expand All @@ -174,9 +174,9 @@ def load_backward_migrations_to(name: str) -> tuple[dict[str, BaseMigration], Op
}, final_previous_migration_name


def execute_forward_migrations(migrations: dict[str, BaseMigration]) -> None:
def execute_migrations_forward(migrations: dict[str, BaseMigration]) -> None:
"""
Executes a list of forward migrations in order.
Executes a list of migrations forward in order.
All `forward_after_transaction`'s are executed AFTER the all of the `forward`'s are executed. This is so that the
latter can be done at once in a transaction.
Expand All @@ -198,9 +198,9 @@ def execute_forward_migrations(migrations: dict[str, BaseMigration]) -> None:
migration.forward_after_transaction(session)

Check warning on line 198 in inventory_management_system_api/migrations/core.py

View check run for this annotation

Codecov / codecov/patch

inventory_management_system_api/migrations/core.py#L196-L198

Added lines #L196 - L198 were not covered by tests


def execute_backward_migrations(migrations: dict[str, BaseMigration], final_previous_migration_name: Optional[str]):
def execute_migrations_backward(migrations: dict[str, BaseMigration], final_previous_migration_name: Optional[str]):
"""
Executes a list of backward migrations in order.
Executes a list of migrations backward in order.
All `backward_after_transaction`'s are executed AFTER the all of the `backward`'s are executed. This is so that the
latter can be done at once in a transaction.
Expand Down
16 changes: 8 additions & 8 deletions inventory_management_system_api/migrations/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

from inventory_management_system_api.core.database import get_database
from inventory_management_system_api.migrations.core import (
execute_backward_migrations,
execute_forward_migrations,
execute_migrations_backward,
execute_migrations_forward,
find_available_migrations,
find_migration_index,
get_previous_migration,
load_backward_migrations_to,
load_forward_migrations_to,
load_migration,
load_migrations_backward_to,
load_migrations_forward_to,
set_previous_migration,
)

Expand Down Expand Up @@ -166,15 +166,15 @@ def setup(self, parser: argparse.ArgumentParser):
)

def run(self, args: argparse.Namespace):
migrations = load_forward_migrations_to(args.name)
migrations = load_migrations_forward_to(args.name)

print("This operation will apply the following migrations:")
for name in migrations.keys():
print(name)

print()
if check_user_sure():
execute_forward_migrations(migrations)
execute_migrations_forward(migrations)
logger.info("Done!")


Expand All @@ -188,15 +188,15 @@ def setup(self, parser: argparse.ArgumentParser):
parser.add_argument("name", help="Name migration to migrate backwards to (inclusive).")

def run(self, args: argparse.Namespace):
migrations, final_previous_migration_name = load_backward_migrations_to(args.name)
migrations, final_previous_migration_name = load_migrations_backward_to(args.name)

print("This operation will revert the following migrations:")
for name in migrations.keys():
print(name)
print()

if check_user_sure():
execute_backward_migrations(migrations, final_previous_migration_name)
execute_migrations_backward(migrations, final_previous_migration_name)
logger.info("Done!")


Expand Down
Loading

0 comments on commit 230a239

Please sign in to comment.