Repository navigation
[PULP-2500] Deactivate local DistributedPublication in favour of pulpcore's #1546
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鈥檒l 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Pulp Deb now is guaranteed to use pulpcore's distributed-publication tracking. | ||
| This change deactivates deb's DistributedPublication code and does the data migration. | ||
| Table deletion should be done in a future release do enable zero-downtime upgrades. |
65 changes: 65 additions & 0 deletions
65
pulp_deb/app/migrations/0044_remove_distributedpublication.py
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import logging | ||
| from itertools import islice | ||
|
|
||
| from django.db import migrations, transaction | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| BATCH_SIZE = 5000 | ||
|
|
||
|
|
||
| def copy_to_core(apps, schema_editor): | ||
| """Best-effort data backfill.""" | ||
| try: | ||
| with transaction.atomic(using=schema_editor.connection.alias): | ||
| DebDistributedPublication = apps.get_model("deb", "DistributedPublication") | ||
| CoreDistributedPublication = apps.get_model("core", "DistributedPublication") | ||
| db_alias = schema_editor.connection.alias | ||
| rows = ( | ||
| DebDistributedPublication.objects.using(db_alias) | ||
| .values( | ||
| "pulp_id", | ||
| "pulp_created", | ||
| "pulp_last_updated", | ||
| "expires_at", | ||
| "distribution_id", | ||
| "publication_id", | ||
| ) | ||
| .iterator(chunk_size=BATCH_SIZE) | ||
| ) | ||
| created_field = CoreDistributedPublication._meta.get_field("pulp_created") | ||
| updated_field = CoreDistributedPublication._meta.get_field("pulp_last_updated") | ||
| auto_now_add = created_field.auto_now_add | ||
| auto_now = updated_field.auto_now | ||
| try: | ||
| # bulk_create normally refreshes these fields; preserve their legacy values. | ||
| created_field.auto_now_add = False | ||
| updated_field.auto_now = False | ||
| while True: | ||
| batch = list(islice(rows, BATCH_SIZE)) | ||
| if not batch: | ||
| break | ||
| CoreDistributedPublication.objects.using(db_alias).bulk_create( | ||
| [CoreDistributedPublication(**row) for row in batch], | ||
| batch_size=BATCH_SIZE, | ||
| ignore_conflicts=True, | ||
| ) | ||
| finally: | ||
| created_field.auto_now_add = auto_now_add | ||
| updated_field.auto_now = auto_now | ||
| except Exception: | ||
| logger.warning( | ||
| "Could not copy distributed publication data to pulpcore; " | ||
| "continuing because the legacy table is retained.", | ||
| exc_info=True, | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("core", "0149_distributedpublication"), | ||
| ("deb", "0043_alter_release_unique_together_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(copy_to_core), | ||
| ] |
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
79 changes: 79 additions & 0 deletions
79
pulp_deb/tests/unit/test_distributed_publication_migration.py
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import unittest | ||
| from importlib import import_module | ||
| from types import SimpleNamespace | ||
| from uuid import uuid4 | ||
|
|
||
| from django.db import connection | ||
| from django.db.migrations.loader import MigrationLoader | ||
| from django.test import TestCase | ||
| from django.utils import timezone | ||
|
|
||
| from pulp_deb.app.models import AptDistribution, AptPublication, AptRepository | ||
|
|
||
|
|
||
| class TestDistributedPublicationMigration(TestCase): | ||
| @unittest.skip("FIXME: remove when DistributedPublication table is dropped") | ||
| def test_copies_all_rows(self): | ||
| migration = import_module("pulp_deb.app.migrations.0044_remove_distributedpublication") | ||
| # Create Repository, Publication and Distribution | ||
| repository = AptRepository.objects.create(name=f"migration-{uuid4()}") | ||
| repository_version = repository.latest_version() | ||
| publication = AptPublication.objects.create(repository_version=repository_version) | ||
| distribution = AptDistribution.objects.create( | ||
| name=f"migration-{uuid4()}", | ||
| base_path=f"migration-{uuid4()}", | ||
| publication=publication, | ||
| ) | ||
|
|
||
| # Get models from before the migration | ||
| migration_apps = ( | ||
| MigrationLoader(connection) | ||
| .project_state( | ||
| [ | ||
| ("core", "0149_distributedpublication"), | ||
| ("deb", "0043_alter_release_unique_together_and_more"), | ||
| ] | ||
| ) | ||
| .apps | ||
| ) | ||
| DebDistributedPublication = migration_apps.get_model("deb", "DistributedPublication") | ||
| CoreDistributedPublication = migration_apps.get_model("core", "DistributedPublication") | ||
|
|
||
| # Create a set of deb's DistributedPublicaitions | ||
| source_rows = [ | ||
| DebDistributedPublication( | ||
| distribution_id=distribution.pk, | ||
| publication_id=publication.pk, | ||
| expires_at=timezone.now(), | ||
| ) | ||
| for _ in range(migration.BATCH_SIZE + 2) | ||
| ] | ||
| DebDistributedPublication.objects.bulk_create(source_rows) | ||
| source_ids = [row.pulp_id for row in source_rows] | ||
|
|
||
| # Migrate data and assert it's copied correctly | ||
| migration.copy_to_core( | ||
| migration_apps, | ||
| SimpleNamespace(connection=connection), | ||
| ) | ||
| source_data = set( | ||
| DebDistributedPublication.objects.filter(pulp_id__in=source_ids).values_list( | ||
| "pulp_id", | ||
| "pulp_created", | ||
| "pulp_last_updated", | ||
| "expires_at", | ||
| "distribution_id", | ||
| "publication_id", | ||
| ) | ||
| ) | ||
| copied_data = set( | ||
| CoreDistributedPublication.objects.filter(pulp_id__in=source_ids).values_list( | ||
| "pulp_id", | ||
| "pulp_created", | ||
| "pulp_last_updated", | ||
| "expires_at", | ||
| "distribution_id", | ||
| "publication_id", | ||
| ) | ||
| ) | ||
| self.assertEqual(copied_data, source_data) |
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.
Uh oh!
There was an error while loading. Please reload this page.