Skip to content

Commit

Permalink
add a "migration" to delete all diffs b/c of the new format
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtmccarty committed Sep 26, 2024
1 parent 1250900 commit 323275e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/infrahub/core/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GRAPH_VERSION = 14
GRAPH_VERSION = 15
10 changes: 7 additions & 3 deletions backend/infrahub/core/migrations/graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
from .m012_convert_account_generic import Migration012
from .m013_convert_git_password_credential import Migration013
from .m014_remove_index_attr_value import Migration014
from .m015_diff_format_update import Migration015

if TYPE_CHECKING:
from infrahub.core.root import Root

from ..shared import GraphMigration, InternalSchemaMigration
from ..shared import ArbitraryMigration, GraphMigration, InternalSchemaMigration

MIGRATIONS: list[type[Union[GraphMigration, InternalSchemaMigration]]] = [
MIGRATIONS: list[type[Union[GraphMigration, InternalSchemaMigration, ArbitraryMigration]]] = [
Migration001,
Migration002,
Migration003,
Expand All @@ -37,10 +38,13 @@
Migration012,
Migration013,
Migration014,
Migration015,
]


async def get_graph_migrations(root: Root) -> Sequence[Union[GraphMigration, InternalSchemaMigration]]:
async def get_graph_migrations(
root: Root,
) -> Sequence[Union[GraphMigration, InternalSchemaMigration, ArbitraryMigration]]:
applicable_migrations = []
for migration_class in MIGRATIONS:
migration = migration_class.init()
Expand Down
36 changes: 36 additions & 0 deletions backend/infrahub/core/migrations/graph/m015_diff_format_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from infrahub.core import registry
from infrahub.core.diff.repository.repository import DiffRepository
from infrahub.core.migrations.shared import MigrationResult
from infrahub.dependencies.registry import build_component_registry, get_component_registry
from infrahub.log import get_logger

from ..shared import ArbitraryMigration

if TYPE_CHECKING:
from infrahub.database import InfrahubDatabase

log = get_logger()


class Migration015(ArbitraryMigration):
name: str = "015_diff_format_update"
minimum_version: int = 14

async def validate_migration(self, db: InfrahubDatabase) -> MigrationResult:
result = MigrationResult()

return result

async def execute(self, db: InfrahubDatabase) -> MigrationResult:
default_branch = registry.get_branch_from_registry()
build_component_registry()
component_registry = get_component_registry()
diff_repo = await component_registry.get_component(DiffRepository, db=db, branch=default_branch)

diff_roots = await diff_repo.get_empty_roots()
await diff_repo.delete_diff_roots(diff_root_uuids=[d.uuid for d in diff_roots])
return MigrationResult()
15 changes: 15 additions & 0 deletions backend/infrahub/core/migrations/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,18 @@ async def execute(self, db: InfrahubDatabase) -> MigrationResult:
return result

return result


class ArbitraryMigration(BaseModel):
name: str = Field(..., description="Name of the migration")
minimum_version: int = Field(..., description="Minimum version of the graph to execute this migration")

@classmethod
def init(cls, **kwargs: dict[str, Any]) -> Self:
return cls(**kwargs) # type: ignore[arg-type]

async def validate_migration(self, db: InfrahubDatabase) -> MigrationResult:
raise NotImplementedError()

async def execute(self, db: InfrahubDatabase) -> MigrationResult:
raise NotImplementedError()
2 changes: 2 additions & 0 deletions backend/infrahub/dependencies/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .builder.constraint.schema.uniqueness import SchemaUniquenessConstraintDependency
from .builder.diff.calculator import DiffCalculatorDependency
from .builder.diff.combiner import DiffCombinerDependency
from .builder.diff.conflict_transferer import DiffConflictTransfererDependency
from .builder.diff.coordinator import DiffCoordinatorDependency
from .builder.diff.data_check_synchronizer import DiffDataCheckSynchronizerDependency
from .builder.diff.enricher.aggregated import DiffAggregatedEnricherDependency
Expand Down Expand Up @@ -41,6 +42,7 @@ def build_component_registry() -> ComponentDependencyRegistry:
component_registry.track_dependency(DiffCalculatorDependency)
component_registry.track_dependency(DiffCombinerDependency)
component_registry.track_dependency(DiffRepositoryDependency)
component_registry.track_dependency(DiffConflictTransfererDependency)
component_registry.track_dependency(DiffCoordinatorDependency)
component_registry.track_dependency(DiffDataCheckSynchronizerDependency)
return component_registry
Expand Down

0 comments on commit 323275e

Please sign in to comment.