-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Use BuildConfig model in our code
#12675
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
Open
humitos
wants to merge
15
commits into
main
Choose a base branch
from
humitos/use-buildconfig-model
base: main
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.
+76
−257
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
036a60e
Initial plan
Copilot 054385c
Add BuildConfig model and readthedocs_yaml_data field to Build model
Copilot e1a19aa
Address code review feedback: fix imports and BuildConfig consistency
Copilot f44e7f4
Address PR feedback: always populate readthedocs_yaml_data and remove…
Copilot bfd043d
Run pre-commit on files
humitos 2e20402
Merge branch 'main' of github.com:readthedocs/readthedocs.org into co…
humitos a169f35
Rename field to mention `_config` instead of `_data`
humitos 1a8823e
Work in progress
humitos 6571452
Merge branch 'main' of github.com:readthedocs/readthedocs.org into hu…
humitos 249573e
Save the `BuildConfig` properly using `config.setter`
humitos 5c8ed56
Update tests to pass with the new BuildConfig model
humitos fe4b1d5
Remove comments
humitos b611864
Final updates for tests
humitos 98e7b4b
Merge branch 'main' into humitos/use-buildconfig-model
humitos 44095e5
Update Celery router to perform only 1 query to check for conda usage
humitos 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 |
|---|---|---|
|
|
@@ -306,11 +306,11 @@ def config(self): | |
| success=True, | ||
| ) | ||
| .order_by("-date") | ||
| .only("_config") | ||
| .only("readthedocs_yaml_config") | ||
| .first() | ||
| ) | ||
| if last_build: | ||
| return last_build.config | ||
| if last_build and last_build.readthedocs_yaml_config: | ||
| return last_build.readthedocs_yaml_config.data | ||
| return None | ||
|
|
||
| @property | ||
|
|
@@ -716,7 +716,8 @@ class Build(models.Model): | |
| blank=True, | ||
| ) | ||
|
|
||
| # TODO: remove `_config` field after migrating all builds to use `readthedocs_yaml_config` | ||
| # TODO: remove _config field once we have migrated all builds. | ||
| # _config field is deprecated in favor of readthedocs_yaml_config | ||
| _config = models.JSONField( | ||
| _("Configuration used in the build"), | ||
| null=True, | ||
|
|
@@ -782,8 +783,6 @@ class Build(models.Model): | |
| # Only include EXTERNAL type Version builds. | ||
| external = ExternalBuildManager.from_queryset(BuildQuerySet)() | ||
|
|
||
| CONFIG_KEY = "__config" | ||
|
|
||
| class Meta: | ||
| ordering = ["-date"] | ||
| get_latest_by = "date" | ||
|
|
@@ -798,88 +797,58 @@ class Meta: | |
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self._config_changed = False | ||
|
|
||
| @property | ||
| def previous(self): | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Returns the previous build to the current one. | ||
|
|
||
| Matching the project and version. | ||
| """ | ||
| date = self.date or timezone.now() | ||
| if self.project is not None and self.version is not None: | ||
| return ( | ||
| Build.objects.filter( | ||
| project=self.project, | ||
| version=self.version, | ||
| date__lt=date, | ||
| ) | ||
| .order_by("-date") | ||
| .first() | ||
| ) | ||
| return None | ||
| self._readthedocs_yaml_config = None | ||
| self._readthedocs_yaml_config_changed = False | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @property | ||
| def config(self): | ||
| """ | ||
| Get the config used for this build. | ||
| Helper to get the build config data as a dict. | ||
|
|
||
| Since we are saving the config into the JSON field only when it differs | ||
| from the previous one, this helper returns the correct JSON used in this | ||
| Build object (it could be stored in this object or one of the previous | ||
| ones). | ||
| Keeping it for backwards compatibility for now. | ||
| We could use `readthedocs_yaml_config` directly instead if we want. | ||
| """ | ||
| # TODO: now that we are using a proper JSONField here, we could | ||
| # probably change this field to be a ForeignKey to avoid repeating the | ||
| # config file over and over again and reuse them to save db data as | ||
| # well | ||
| if self._config and self.CONFIG_KEY in self._config: | ||
| return Build.objects.only("_config").get(pk=self._config[self.CONFIG_KEY])._config | ||
| return self._config | ||
| return self.readthedocs_yaml_config.data if self.readthedocs_yaml_config else {} | ||
|
|
||
| @config.setter | ||
| def config(self, value): | ||
| """ | ||
| Set `_config` to value. | ||
| Helper to create a `BuildConfig` from a dict. | ||
|
|
||
| `_config` should never be set directly from outside the class. | ||
| """ | ||
| self._config = value | ||
| self._config_changed = True | ||
|
|
||
| def save(self, *args, **kwargs): # noqa | ||
| """ | ||
| Save object. | ||
|
|
||
| To save space on the db we only save the config if it's different | ||
| from the previous one. | ||
| Keeping it for backwards compatibility for now. We are using it as: | ||
|
|
||
| If the config is the same, we save the pk of the object | ||
| that has the **real** config under the `CONFIG_KEY` key. | ||
| build.config = { | ||
| "search": { | ||
| "ranking": { | ||
| "*index.html": 5, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Additionally, we create or get a BuildConfig object for the new | ||
| readthedocs_yaml_config field to facilitate the migration to the new model. | ||
| We could remove this and create `BuildConfig` objects directly instead if we want. | ||
| """ | ||
| if self.pk is None or self._config_changed: | ||
| if self._config is None: | ||
| self.readthedocs_yaml_config = None | ||
| else: | ||
| build_config, created = BuildConfig.objects.get_or_create(data=self._config) | ||
| self.readthedocs_yaml_config = build_config | ||
|
|
||
| previous = self.previous | ||
| if previous is not None and self._config and self._config == previous.config: | ||
| previous_pk = previous._config.get(self.CONFIG_KEY, previous.pk) | ||
| self._config = {self.CONFIG_KEY: previous_pk} | ||
| self._readthedocs_yaml_config = value | ||
| self._readthedocs_yaml_config_changed = True | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def save(self, *args, **kwargs): # noqa | ||
| if self._readthedocs_yaml_config_changed: | ||
| build_config, _ = BuildConfig.objects.get_or_create(data=self._readthedocs_yaml_config) | ||
|
Member
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. _readthedocs_yaml_config could have been assigned to None. In that case we won't have a build_config object. |
||
| self.readthedocs_yaml_config = build_config | ||
| log.warning( | ||
| "Creating BuildConfig using `build.config` setter.", | ||
| build_id=self.id, | ||
| build_config_data=self._readthedocs_yaml_config, | ||
| build_config_id=build_config.id, | ||
| ) | ||
|
|
||
| if self.version: | ||
| self.version_name = self.version.verbose_name | ||
| self.version_slug = self.version.slug | ||
| self.version_type = self.version.type | ||
|
|
||
| super().save(*args, **kwargs) | ||
| self._config_changed = False | ||
| self._readthedocs_yaml_config = None | ||
| self._readthedocs_yaml_config_changed = False | ||
|
|
||
| def get_absolute_url(self): | ||
| return reverse("builds_detail", args=[self.project.slug, self.pk]) | ||
|
|
||
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
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
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.
This was used because we needed to query other builds. You can change this to just
values_list("readthedocs_yaml_config", flat=True)