-
Notifications
You must be signed in to change notification settings - Fork 50
Update migrations 39-45 to perform the manifest data migration for new fields [2.26] #2385
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
Draft
gerrod3
wants to merge
1
commit into
pulp:2.26
Choose a base branch
from
gerrod3:handle-data-migrate
base: 2.26
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,61 @@ | ||
| # Generated by Django 4.2.10 on 2024-03-05 11:22 | ||
| import json | ||
| import warnings | ||
|
|
||
| from django.db import migrations, models | ||
| from django.core.files.storage import default_storage | ||
|
|
||
| from pulp_container.constants import ( | ||
| MEDIA_TYPE, | ||
| ) | ||
|
|
||
| def print_warning_for_initializing_manifest_data(apps, schema_editor): | ||
| warnings.warn( | ||
| "Run 'pulpcore-manager container-handle-image-data' to move the manifests' " | ||
| "data from artifacts to the new 'data' database field." | ||
| ) | ||
| def get_content_data(artifact): | ||
| with default_storage.open(artifact.file.name, mode="rb") as file: | ||
| raw_data = file.read() | ||
| content_data = json.loads(raw_data) | ||
| return content_data, raw_data | ||
|
|
||
| def migrate_manifest_data(apps, schema_editor): | ||
| """ | ||
| Migrate the backing artifact manifest to the new 'data' field. | ||
| Also, initialize the 'annotations' and 'labels' fields. | ||
|
|
||
| 'is_bootable' and 'is_flatpak' fields are not initialized since they require annotations and | ||
| labels to be initialized first. | ||
| """ | ||
| Manifest = apps.get_model("container", "Manifest") | ||
| to_update = [] | ||
| manifests = Manifest.objects.filter( | ||
| contentartifact__artifact__isnull=False | ||
| ).distinct().prefetch_related("_artifacts").select_related("config_blob") | ||
| for manifest in manifests.iterator(chunk_size=1000): | ||
| manifest_artifact = manifest._artifacts.get() | ||
| manifest_data, raw_bytes_data = get_content_data(manifest_artifact) | ||
| manifest.data = raw_bytes_data.decode("utf-8") | ||
| if not manifest.annotations: | ||
| init_annotations(manifest, manifest_data) | ||
| if not manifest.labels: | ||
| init_labels(manifest) | ||
|
|
||
| to_update.append(manifest) | ||
| if len(to_update) > 1000: | ||
| Manifest.objects.bulk_update(to_update, ["data", "annotations", "labels"]) | ||
| to_update.clear() | ||
| if to_update: | ||
| Manifest.objects.bulk_update(to_update, ["data", "annotations", "labels"]) | ||
|
|
||
| def init_annotations(manifest, manifest_data): | ||
| # annotations are part of OCI only | ||
| if manifest.media_type not in (MEDIA_TYPE.MANIFEST_OCI, MEDIA_TYPE.INDEX_OCI): | ||
| return False | ||
|
|
||
| manifest.annotations = manifest_data.get("annotations", {}) | ||
|
|
||
| def init_labels(manifest): | ||
| if manifest.config_blob: | ||
| config_artifact = manifest.config_blob._artifacts.get() | ||
|
Contributor
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. Same |
||
| config_data, _ = get_content_data(config_artifact) | ||
| manifest.labels = config_data.get("config", {}).get("Labels") or {} | ||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
|
|
@@ -24,7 +70,7 @@ class Migration(migrations.Migration): | |
| field=models.TextField(null=True), | ||
| ), | ||
| migrations.RunPython( | ||
| print_warning_for_initializing_manifest_data, | ||
| migrate_manifest_data, | ||
| reverse_code=migrations.RunPython.noop, | ||
| elidable=True, | ||
| ), | ||
|
|
||
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
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.
While it sounds like the missing artifact thing may have been a rare issue, do you think we should try to either A) skip and print a warning or B) raise a proper error if an artifact (exactly 1) doesn't exist?