Skip to content

Commit 7413c7d

Browse files
feat: fetch channels and branch tips in a transaction
The atomic update makes it slightly more robust against interruptions. This is done in a separate commit to keep the previous addition of fetching branch tips readable.
1 parent de34e94 commit 7413c7d

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

src/shared/management/commands/fetch_all_channels.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
from django.conf import settings
88
from django.core.management.base import BaseCommand
9+
from django.db import transaction
910
from pydantic import BaseModel, ConfigDict, Field, model_validator
1011

1112
from shared.git import get_head_sha1
@@ -78,26 +79,27 @@ def handle(self, *args: Any, **kwargs: Any) -> str | None:
7879
for release_branch in {c.release_branch for c in channels}
7980
}
8081

81-
branches: dict[str, NixpkgsBranch] = {}
82-
for name, head in branch_tips.items():
83-
branch, _ = NixpkgsBranch.objects.update_or_create(
84-
name=name,
85-
defaults={"head_sha1_commit": head},
86-
)
87-
branches[name] = branch
88-
89-
for monitored in channels:
90-
NixChannel.objects.update_or_create(
91-
channel_branch=monitored.channel,
92-
defaults=dict(
93-
release_branch=branches[monitored.release_branch],
94-
head_sha1_commit=monitored.revision,
95-
state=monitored.status,
96-
variant=monitored.variant,
97-
),
98-
)
99-
100-
# Can't `pprint()` to `self.stdout` directly...
101-
stream = StringIO()
102-
pprint(monitored.__dict__, stream=stream)
103-
self.stdout.write(stream.getvalue())
82+
with transaction.atomic():
83+
branches: dict[str, NixpkgsBranch] = {}
84+
for name, head in branch_tips.items():
85+
branch, _ = NixpkgsBranch.objects.update_or_create(
86+
name=name,
87+
defaults={"head_sha1_commit": head},
88+
)
89+
branches[name] = branch
90+
91+
for monitored in channels:
92+
NixChannel.objects.update_or_create(
93+
channel_branch=monitored.channel,
94+
defaults=dict(
95+
release_branch=branches[monitored.release_branch],
96+
head_sha1_commit=monitored.revision,
97+
state=monitored.status,
98+
variant=monitored.variant,
99+
),
100+
)
101+
102+
# Can't `pprint()` to `self.stdout` directly...
103+
stream = StringIO()
104+
pprint(monitored.__dict__, stream=stream)
105+
self.stdout.write(stream.getvalue())

0 commit comments

Comments
 (0)