-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Make Project -> ForeignKey -> RemoteRepository
#8259
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
Conversation
dfc2572
to
60b491b
Compare
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.
@agjohnson please take a close look at this PR and let me know if it does make sense to you.
def migrate_data(apps, schema_editor): | ||
RemoteRepository = apps.get_model('oauth', 'RemoteRepository') | ||
queryset = RemoteRepository.objects.filter(project__isnull=False).select_related('project') | ||
for rr in queryset: | ||
rr.project.remote_repository = rr | ||
rr.save() |
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.
@agjohnson this data migration from the old field to the new field is an important change to take a look at carefully.
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.
also, this may be slow in production if we have many RemoteRepositoy
linked to a Project
.
:query string expand: allows to add/expand some extra fields in the response. | ||
Allowed values are ``project`` and ``organization``. | ||
Allowed values are ``projects`` and ``remote_organization``. | ||
Multiple fields can be passed separated by commas. |
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.
@agjohnson this a change in the API request/response required to make sense with the new relation. It also reserves ?expand=organization
to be used for Organization
objects.
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.
I was going to say we should keep project
around for backwards compatibility, but this API should really only be internal use anyways. So yeah, agreed this seems fine.
4a325ba
to
8160a3d
Compare
- add `Project.remote_repository` - migrate data from `RemoteRepository.project` to `Project.remote_repository` - drop `RemoteRepository.project` - change `?expand=project,organization` by `?expand=projects,remote_organization` on /api/v3/remote/repositories/ endpoint
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.
Changes look great! 💯
I think we may want to add remote_repository
to the Project ModelAdmin raw_id_fields
raw_id_fields = ('users', 'main_language_project', 'remote_repository',)
readthedocs/oauth/migrations/0014_remove_remoterepository_project.py
Outdated
Show resolved
Hide resolved
…ect.py Co-authored-by: Maksudul Haque <[email protected]>
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.
Looks great! If anyone shares my concern about performance here, we might want to find a way to make the migration faster or operate in chunks. It might be possible with a bulk query 🤷
:query string expand: allows to add/expand some extra fields in the response. | ||
Allowed values are ``project`` and ``organization``. | ||
Allowed values are ``projects`` and ``remote_organization``. | ||
Multiple fields can be passed separated by commas. |
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.
I was going to say we should keep project
around for backwards compatibility, but this API should really only be internal use anyways. So yeah, agreed this seems fine.
queryset = RemoteRepository.objects.filter(project__isnull=False).select_related('project') | ||
for rr in queryset.iterator(): | ||
rr.project.remote_repository = rr | ||
rr.save() |
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.
Took me a second to follow this logic, but I think this looks good. I suppose I would have concerns about performance here, we will have a few thousand remote repositories to step through individually.
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.
I just checked this: 22k. We should be fine.
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.
I double-checked this logic and it didn't work.
rr.project.remote_repository = rr
That doesn't seem to keep the relation.
I also tried,
rr.project.remote_repository_id = rr.pk
I don't understand why.
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.
Meh, I found that I was just saving the wrong object.
We need either:
rr.project.save()
or
rr.projects.add(rr.project)
However, both are producing an error that I'm not sure to understand 😞
database_1 | 2021-06-17 12:05:45.877 UTC [193] ERROR: cannot ALTER TABLE "projects_project" because it has pending trigger events
database_1 | 2021-06-17 12:05:45.877 UTC [193] STATEMENT: SET CONSTRAINTS "oauth_remotereposito_project_id_246c363d_fk_projects_" IMMEDIATE; ALTER TABLE "oauth_remoterepository_2020" DROP CONSTRAINT "oauth_remotereposito_project_id_246c363d_fk_projects_"
Migrate the relation between
Project
andRemoteRepository
to be aForeignKey
fromProject
model, so we can attach multiple projects to the sameRemoteRepository
. This would allow us to import the same repository multiple times and use VCS permissions to manage it.🧑🏭 Work done
Project.remote_repository
RemoteRepository.project
toProject.remote_repository
RemoteRepository.project
?expand=project,organization
by?expand=projects,remote_organization
on/api/v3/remote/repositories/
endpoint📝 Notes
/api/v3/remote/repositories/?expand=project,organization
project
in singular nowproject
as a RTD object andorganization
as a VCS object looks weird?expand=projects,organization,remoteorganization
instead.select_related
anymoreRemoteRepository
andRemoteOrganization
#7510RemoteRepository
with aproject
attached have to be updatedproject.remote_repository
does not raiseRemoteRepository.DoesNotExist
anymore; now it just returnNone
(e.g.readthedocs.builds.tasks.send_build_status
)remote_repository.project = project
toremote_repository.projects.add(project)
Closes #8232
Related #8229