Skip to content

Commit 6e06543

Browse files
committed
Add typealias for migration classes
1 parent dcbdf7d commit 6e06543

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

backend/infrahub/cli/db.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ def get_timestamp_string() -> str:
6363

6464
if TYPE_CHECKING:
6565
from infrahub.cli.context import CliContext
66-
from infrahub.core.migrations.shared import (
67-
ArbitraryMigration,
68-
GraphMigration,
69-
InternalSchemaMigration,
70-
MigrationRequiringRebase,
71-
)
66+
from infrahub.core.migrations.shared import MigrationTypes
7267
from infrahub.database import InfrahubDatabase
7368
from infrahub.database.index import IndexManagerBase
7469

@@ -287,10 +282,10 @@ async def index(
287282

288283
async def detect_migration_to_run(
289284
current_graph_version: int, migration_number: int | str | None = None
290-
) -> Sequence[GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase]:
285+
) -> Sequence[MigrationTypes]:
291286
"""Return a sequence of migrations to apply to upgrade the database."""
292287
rprint("Checking current state of the database")
293-
migrations: list[GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase] = []
288+
migrations: list[MigrationTypes] = []
294289

295290
if migration_number:
296291
migration = get_migration_by_number(migration_number)
@@ -316,9 +311,7 @@ async def detect_migration_to_run(
316311

317312

318313
async def migrate_database(
319-
db: InfrahubDatabase,
320-
migrations: Sequence[GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase],
321-
initialize: bool = False,
314+
db: InfrahubDatabase, migrations: Sequence[MigrationTypes], initialize: bool = False
322315
) -> bool:
323316
"""Apply the latest migrations to the database, this function will print the status directly in the console.
324317

backend/infrahub/core/migrations/graph/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
from .m043_backfill_hfid_display_label_in_db import Migration043
4848

4949
if TYPE_CHECKING:
50-
from ..shared import ArbitraryMigration, GraphMigration, InternalSchemaMigration, MigrationRequiringRebase
50+
from ..shared import MigrationTypes
5151

52-
MIGRATIONS: list[type[GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase]] = [
52+
53+
MIGRATIONS: list[type[MigrationTypes]] = [
5354
Migration001,
5455
Migration002,
5556
Migration003,
@@ -96,9 +97,7 @@
9697
]
9798

9899

99-
async def get_graph_migrations(
100-
current_graph_version: int,
101-
) -> Sequence[GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase]:
100+
async def get_graph_migrations(current_graph_version: int) -> Sequence[MigrationTypes]:
102101
applicable_migrations = []
103102
for migration_class in MIGRATIONS:
104103
migration = migration_class.init()
@@ -109,9 +108,7 @@ async def get_graph_migrations(
109108
return applicable_migrations
110109

111110

112-
def get_migration_by_number(
113-
migration_number: int | str,
114-
) -> GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase:
111+
def get_migration_by_number(migration_number: int | str) -> MigrationTypes:
115112
# Convert to string and pad with zeros if needed
116113
try:
117114
num = int(migration_number)

backend/infrahub/core/migrations/shared.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, Sequence
3+
from typing import TYPE_CHECKING, Any, Sequence, TypeAlias
44

55
from pydantic import BaseModel, ConfigDict, Field
66
from typing_extensions import Self
@@ -245,3 +245,6 @@ async def execute_against_branch(self, db: InfrahubDatabase, branch: Branch) ->
245245
async def execute(self, db: InfrahubDatabase) -> MigrationResult:
246246
"""Method that will be run against the default branch."""
247247
raise NotImplementedError()
248+
249+
250+
MigrationTypes: TypeAlias = GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationRequiringRebase

0 commit comments

Comments
 (0)