-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
Comment on lines
+33
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
| __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,34 @@ 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 None: | ||
| # Referenced model not in pk_map - the source was filtered out or failed to import. | ||
| return None | ||
|
|
||
| self.source_id = str(new_source_id) | ||
| 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): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice