Skip to content
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

Switches FileDistribution use Distribution #484

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES/8387.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Switches ``FileDistribution`` to inherit from ``Distribution`` instead of ``BaseDistribution``.
Additionally ship a migration which moves the data from ``BaseDistribution`` to ``Distribution``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 2.2.19 on 2021-03-19 17:42

from django.db import connection, migrations, models, transaction
import django.db.models.deletion


pks_to_delete = []


def migrate_data_from_old_master_model_to_new_master_model(apps, schema_editor):
FileDistribution = apps.get_model('file', 'FileDistribution')
CoreDistribution = apps.get_model('core', 'Distribution')
for old_file_distribution in FileDistribution.objects.all():
with transaction.atomic():
new_master_model_entry = CoreDistribution(
pulp_id=old_file_distribution.pulp_id,
pulp_created=old_file_distribution.pulp_created,
pulp_last_updated=old_file_distribution.pulp_last_updated,
pulp_type=old_file_distribution.pulp_type,
name=old_file_distribution.name,
base_path=old_file_distribution.base_path,
content_guard=old_file_distribution.content_guard,
remote=old_file_distribution.remote,
publication=old_file_distribution.publication,
)
new_master_model_entry.save()
old_file_distribution.distribution_ptr = new_master_model_entry
old_file_distribution.save()
pks_to_delete.append(old_file_distribution.pulp_id)


def delete_remaining_old_master_model_entries(apps, schema_editor):
with connection.cursor() as cursor:
for pk in pks_to_delete:
cursor.execute("DELETE from core_basedistribution WHERE pulp_id = %s", [pk])


class Migration(migrations.Migration):

dependencies = [
('core', '0062_add_new_distribution_mastermodel'),
('file', '0008_add_manifest_field'),
]

operations = [
migrations.AddField(
model_name='filedistribution',
name='distribution_ptr',
field=models.OneToOneField(auto_created=True, null=True, default=None,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True, primary_key=False,
related_name='file_filedistribution', serialize=False,
to='core.Distribution'),
preserve_default=False,
),
migrations.RunPython(migrate_data_from_old_master_model_to_new_master_model),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really have the reverse_code here. I can do that in a follow up pr if you prefer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdellweg I'm not able to put it together due to time constraints, but I think it would be great if you'd be willing to post-merge. Like we talked about at the pulpcore meeting, doing it prior to the 3.12 release would be ideal because this migration is about to get replicated across almost all plugins very soon.

migrations.RemoveField(
model_name='filedistribution',
name='basedistribution_ptr',
),
migrations.AlterField(
model_name='filedistribution',
name='distribution_ptr',
field=models.OneToOneField(auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True, primary_key=True,
related_name='file_filedistribution', serialize=False,
to='core.Distribution'),
preserve_default=False,
),
migrations.RemoveField(
model_name='filedistribution',
name='publication',
),
migrations.RunPython(delete_remaining_old_master_model_entries),
]
4 changes: 2 additions & 2 deletions pulp_file/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from pulpcore.plugin.models import (
Content,
Distribution,
FilesystemExporter,
Publication,
PublicationDistribution,
Remote,
Repository,
)
Expand Down Expand Up @@ -98,7 +98,7 @@ def finalize_new_publication(self):
validate_publication_paths(self)


class FileDistribution(PublicationDistribution):
class FileDistribution(Distribution):
"""
Distribution for 'file' content.
"""
Expand Down
16 changes: 12 additions & 4 deletions pulp_file/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from pulpcore.plugin.serializers import (
ContentChecksumSerializer,
DetailRelatedField,
DistributionSerializer,
FilesystemExporterSerializer,
PublicationDistributionSerializer,
PublicationSerializer,
RemoteSerializer,
RepositorySerializer,
Expand Down Expand Up @@ -91,7 +91,7 @@ class FilePublicationSerializer(PublicationSerializer):

distributions = DetailRelatedField(
help_text=_("This publication is currently hosted as defined by these distributions."),
source="file_filedistribution",
source="distribution_set",
view_name="filedistributions-detail",
many=True,
read_only=True,
Expand All @@ -106,13 +106,21 @@ class Meta:
fields = PublicationSerializer.Meta.fields + ("distributions", "manifest")


class FileDistributionSerializer(PublicationDistributionSerializer):
class FileDistributionSerializer(DistributionSerializer):
"""
Serializer for File Distributions.
"""

publication = DetailRelatedField(
required=False,
help_text=_("Publication to be served"),
view_name_pattern=r"publications(-.*/.*)?-detail",
queryset=models.Publication.objects.exclude(complete=False),
allow_null=True,
)

class Meta:
fields = PublicationDistributionSerializer.Meta.fields
fields = DistributionSerializer.Meta.fields + ("publication",)
model = FileDistribution


Expand Down
4 changes: 2 additions & 2 deletions pulp_file/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
)
from pulpcore.plugin.tasking import enqueue_with_reservation, fs_publication_export
from pulpcore.plugin.viewsets import (
BaseDistributionViewSet,
ContentFilter,
DistributionViewSet,
ExporterViewSet,
ExportViewSet,
OperationPostponedResponse,
Expand Down Expand Up @@ -167,7 +167,7 @@ def create(self, request):
return OperationPostponedResponse(result, request)


class FileDistributionViewSet(BaseDistributionViewSet):
class FileDistributionViewSet(DistributionViewSet):
"""
<!-- User-facing documentation, rendered as html-->
FileDistributions host <a href="#operation/publications_file_file_list">File
Expand Down