|
6 | 6 | import requests |
7 | 7 | from django.conf import settings |
8 | 8 | from django.core.management.base import BaseCommand |
| 9 | +from django.db import transaction |
9 | 10 | from pydantic import BaseModel, ConfigDict, Field, model_validator |
10 | 11 |
|
11 | 12 | from shared.git import get_head_sha1 |
@@ -78,26 +79,27 @@ def handle(self, *args: Any, **kwargs: Any) -> str | None: |
78 | 79 | for release_branch in {c.release_branch for c in channels} |
79 | 80 | } |
80 | 81 |
|
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