-
Notifications
You must be signed in to change notification settings - Fork 1
Implement transfer job models #290
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e6d5f9
Deriving what TransferJob used to store and shrinking TransferredItem
aaronjae22 5e43e67
Making a progress write and its row succeed or fail together
aaronjae22 2c40d08
Following the model rework through admin and the factories
aaronjae22 655e11a
Regenerating the transfer migration
aaronjae22 a9b3b27
Covering the derived owner, the coarse state and the failed-save guar…
aaronjae22 d85fd78
Owning the collection vocabulary and dropping the failed-save revert
aaronjae22 bc09bd6
Dropping TransferredItem model plus its implementation in admin and f…
aaronjae22 08f7806
Regenerating the transfer migration
aaronjae22 2b1bb96
Covering the collection vocabulary and method form
aaronjae22 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Generated by Django 5.1.3 on 2026-09-14 19:12 | ||
|
|
||
| import django.db.models.deletion | ||
| import testbed.core.models | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('core', '0009_token_actor_binding'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='TransferJob', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('source_base_url', models.URLField(help_text='Base URL of the source server. Everything else about the source is discovered from it.', max_length=500)), | ||
| ('source_actor_url', models.URLField(blank=True, help_text='Source Actor URL as supplied or resolved by discovery. Advisory, not authoritative.', max_length=500, null=True)), | ||
| ('authorized_actor_url', models.URLField(blank=True, help_text='The Actor URL the source returned in `activitypub_actor` on the callback (LOLA §5.3).', max_length=500, null=True)), | ||
| ('state', models.CharField(choices=[('active', 'Active'), ('finished', 'Finished'), ('failed', 'Failed')], default='active', help_text='The working phase is derived from the data, not stored.', max_length=16)), | ||
| ('retry_when', models.DateTimeField(blank=True, help_text="Set from a 429's Retry-After. A job is paused.", null=True)), | ||
| ('policy', models.JSONField(blank=True, default=testbed.core.models.default_transfer_policy, help_text='Chosen when the job is created. Changing the decision means a new job rather than an edit.')), | ||
| ('progress', models.JSONField(blank=True, default=dict, help_text='Per-collection resume state. Read and written only.')), | ||
| ('artifacts', models.JSONField(blank=True, default=dict, help_text='References to captured raw payloads, never the payload bodies.')), | ||
| ('error', models.TextField(blank=True, default='', help_text='The single terminal reason this job failed: source unreachable, authorization revoked, etc.')), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_at', models.DateTimeField(auto_now=True)), | ||
| ('destination_actor', models.ForeignKey(help_text='The destination Actor imported content is written to.', on_delete=django.db.models.deletion.CASCADE, related_name='inbound_transfers', to='core.actor')), | ||
| ], | ||
| options={ | ||
| 'ordering': ['-created_at'], | ||
| 'indexes': [models.Index(fields=['destination_actor', 'state'], name='transferjob_actor_state_idx'), models.Index(fields=['state', 'retry_when'], name='transferjob_state_retry_idx')], | ||
| 'constraints': [models.CheckConstraint(condition=models.Q(('error', ''), ('state', 'failed'), _negated=True), name='transferjob_failed_has_reason')], | ||
| }, | ||
| ), | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import pytest | ||
|
|
||
| from testbed.core.factories import TransferJobFactory | ||
| from testbed.core.models import TransferJob | ||
| from testbed.core.transfer.jobs import Collection | ||
|
|
||
|
|
||
| # The collection vocabulary | ||
|
|
||
|
|
||
| def test_collections_exclude_followers(): | ||
| # LOLA §6.6 Not Fetched: the Followers collection is reconstructed by followers choosing | ||
| # to re-follow, never copied. | ||
| assert set(Collection.values) == { | ||
| "content", | ||
| "outbox", | ||
| "following", | ||
| "blocked", | ||
| "liked", | ||
| } | ||
| assert "followers" not in Collection.values | ||
|
|
||
|
|
||
| def test_an_unknown_collection_is_rejected_on_write(): | ||
| # Without this a typo writes progress under a key nothing ever reads, and that collection | ||
| # silently restarts from page 1 on every resume, re-importing what it already imported | ||
| job = TransferJobFactory() | ||
|
|
||
| with pytest.raises(ValueError): | ||
| job.set_collection_progress("contnet", cursor="page-2") | ||
|
|
||
|
|
||
| def test_an_unknown_collection_is_rejected_on_read(): | ||
| # Reading is validated too. An unknown name would otherwise return {}, which is | ||
| # indistinguishable from "this collection has not started yet" | ||
| job = TransferJobFactory() | ||
|
|
||
| with pytest.raises(ValueError): | ||
| job.get_collection_progress("contnet") | ||
|
|
||
|
|
||
| # Reading and writing resume state | ||
|
|
||
|
|
||
| def test_unrecorded_collection_reads_as_empty(): | ||
| job = TransferJobFactory() | ||
|
|
||
| assert job.get_collection_progress("content") == {} | ||
|
|
||
|
|
||
| def test_set_persists_to_the_database(): | ||
| # The whole point of the model: state must survive the request that wrote it | ||
| job = TransferJobFactory() | ||
|
|
||
| job.set_collection_progress("content", cursor="page-2", seen=20) | ||
|
|
||
| reloaded = TransferJob.objects.get(pk=job.pk) | ||
| assert reloaded.get_collection_progress("content") == { | ||
| "cursor": "page-2", | ||
| "seen": 20, | ||
| } | ||
|
|
||
|
|
||
| def test_set_merges_rather_than_replaces(): | ||
| job = TransferJobFactory() | ||
| job.set_collection_progress("content", cursor="page-1", seen=10) | ||
|
|
||
| job.set_collection_progress("content", seen=20) | ||
|
|
||
| reloaded = TransferJob.objects.get(pk=job.pk) | ||
| assert reloaded.get_collection_progress("content") == { | ||
| "cursor": "page-1", | ||
| "seen": 20, | ||
| } | ||
|
|
||
|
|
||
| def test_writing_one_collection_leaves_its_siblings_alone(): | ||
| # A fetch walk moves through collections one at a time. | ||
| # Recording outbox's position must not lose where content got to | ||
| job = TransferJobFactory() | ||
| job.set_collection_progress("content", cursor="page-3") | ||
| job.set_collection_progress("following", cursor="page-1") | ||
|
|
||
| job.set_collection_progress("outbox", cursor="page-1") | ||
|
|
||
| reloaded = TransferJob.objects.get(pk=job.pk) | ||
| assert reloaded.get_collection_progress("content") == {"cursor": "page-3"} | ||
| assert reloaded.get_collection_progress("following") == {"cursor": "page-1"} | ||
| assert reloaded.get_collection_progress("outbox") == {"cursor": "page-1"} | ||
|
|
||
|
|
||
| def test_both_accessors_return_copies(): | ||
| # A caller mutating what either function handed back would change the job in memory without | ||
| # persisting it, giving a resume position that exists in the process and not in the database. | ||
| job = TransferJobFactory() | ||
| job.set_collection_progress("content", cursor="page-1") | ||
|
|
||
| from_get = job.get_collection_progress("content") | ||
| from_set = job.set_collection_progress("content", seen=5) | ||
|
|
||
| assert from_set == {"cursor": "page-1", "seen": 5} # set returns the merged state | ||
|
|
||
| from_get["cursor"] = "page-99" | ||
| from_set["seen"] = 999 | ||
|
|
||
| assert job.get_collection_progress("content") == {"cursor": "page-1", "seen": 5} | ||
|
|
||
|
|
||
| def test_set_writes_only_the_progress_column(): | ||
| job = TransferJobFactory() | ||
| TransferJob.objects.filter(pk=job.pk).update(state=TransferJob.State.FINISHED) | ||
|
|
||
| # `job` still holds the stale in-memory state | ||
| job.set_collection_progress("content", cursor="page-1") | ||
|
|
||
| reloaded = TransferJob.objects.get(pk=job.pk) | ||
| assert reloaded.state == TransferJob.State.FINISHED | ||
| assert reloaded.get_collection_progress("content") == {"cursor": "page-1"} | ||
|
|
||
|
|
||
| def test_set_refreshes_updated_at(): | ||
| job = TransferJobFactory() | ||
| before = TransferJob.objects.get(pk=job.pk).updated_at | ||
|
|
||
| job.set_collection_progress("content", cursor="page-1") | ||
|
|
||
| assert TransferJob.objects.get(pk=job.pk).updated_at > before |
Oops, something went wrong.
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.
Is this doing a query of actors that have the role? That's not what I'd expect from in a factory - I would think that instead the TransferJobFactory would call UserWithActorsFactory(role=Actor.ROLE_DESTINATION) to set the role on the new user, or set the role after calling the factory, or something lie that.
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.
The factory queries because the post_save signal on User already created both actors by the time the factory gets control. This is something I'll work around later on