-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(aci): Implement DataSource.normalize_before_relocation_import #103002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,23 @@ | ||
| import builtins | ||
| import dataclasses | ||
| import logging | ||
| from typing import Generic, TypeVar | ||
|
|
||
| from django.db import models | ||
| from django.db.models.signals import pre_save | ||
| from django.dispatch import receiver | ||
|
|
||
| from sentry.backup.scopes import RelocationScope | ||
| from sentry.backup.dependencies import NormalizedModelName, PrimaryKeyMap | ||
| from sentry.backup.helpers import ImportFlags | ||
| from sentry.backup.scopes import ImportScope, RelocationScope | ||
| from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model | ||
| from sentry.utils.registry import NoRegistrationExistsError | ||
| from sentry.workflow_engine.models.data_source_detector import DataSourceDetector | ||
| from sentry.workflow_engine.registry import data_source_type_registry | ||
| from sentry.workflow_engine.types import DataSourceTypeHandler | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
|
|
@@ -25,6 +30,13 @@ class DataPacket(Generic[T]): | |
| @region_silo_model | ||
| class DataSource(DefaultFieldsModel): | ||
| __relocation_scope__ = RelocationScope.Organization | ||
| # DataSource.source_id dynamically references different models based on the 'type' field. | ||
| # We declare all possible dependencies here to ensure proper import ordering. | ||
| __relocation_dependencies__ = { | ||
| "monitors.monitor", # For DATA_SOURCE_CRON_MONITOR | ||
| "sentry.querysubscription", # For DATA_SOURCE_SNUBA_QUERY_SUBSCRIPTION | ||
| "uptime.uptimesubscription", # For DATA_SOURCE_UPTIME_SUBSCRIPTION | ||
| } | ||
|
|
||
| organization = FlexibleForeignKey("sentry.Organization") | ||
|
|
||
|
|
@@ -49,6 +61,43 @@ def type_handler(self) -> builtins.type[DataSourceTypeHandler]: | |
| raise ValueError(f"Unknown data source type: {self.type}") | ||
| return handler | ||
|
|
||
| def normalize_before_relocation_import( | ||
| self, pk_map: PrimaryKeyMap, scope: ImportScope, flags: ImportFlags | ||
| ) -> int | None: | ||
| old_pk = super().normalize_before_relocation_import(pk_map, scope, flags) | ||
| if old_pk is None: | ||
| return None | ||
|
|
||
| # Map source_id based on the data source type | ||
| try: | ||
| handler = data_source_type_registry.get(self.type) | ||
| model_name = NormalizedModelName(handler.get_relocation_model_name()) | ||
| old_source_id = int(self.source_id) | ||
| new_source_id = pk_map.get_pk(model_name, old_source_id) | ||
|
|
||
| if new_source_id is not None: | ||
| self.source_id = str(new_source_id) | ||
| else: | ||
| # Referenced model not in pk_map. This may be correct (reset_pks=False) or broken | ||
| # (reset_pks=True but referenced model was filtered out or failed to import). | ||
|
||
| logger.warning( | ||
| "DataSource source_id not remapped - referenced model not in pk_map", | ||
| extra={ | ||
| "data_source_id": old_pk, | ||
| "type": self.type, | ||
| "source_id": old_source_id, | ||
| "model": str(model_name), | ||
| }, | ||
| ) | ||
| except Exception: | ||
| logger.exception( | ||
| "DataSource.normalize_before_relocation_import failed", | ||
| extra={"data_source_id": old_pk, "type": self.type, "source_id": self.source_id}, | ||
| ) | ||
| return None | ||
kcons marked this conversation as resolved.
Show resolved
Hide resolved
kcons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return old_pk | ||
|
|
||
|
|
||
| @receiver(pre_save, sender=DataSource) | ||
| def ensure_type_handler_registered(sender, instance: DataSource, **kwargs): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the order matter because we need the pk mappings for the other tables to exist before this table is relocated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.