Skip to content

Commit af3b57f

Browse files
authored
Merge pull request #276 from netboxlabs/275-branch-last_sync
2 parents a94636c + 57b805d commit af3b57f

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

netbox_branching/models/branches.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ def schema_name(self):
127127
def connection_name(self):
128128
return f'schema_{self.schema_name}'
129129

130-
@property
131-
def synced_time(self):
132-
return self.last_sync or self.created
133-
134130
def clean(self):
135131

136132
# Enforce the maximum number of total branches
@@ -204,12 +200,16 @@ def get_unsynced_changes(self):
204200
"""
205201
Return a queryset of all ObjectChange records created in main since the Branch was last synced or created.
206202
"""
203+
# TODO: Remove this fallback logic in a future release
204+
# Backward compatibility for branches created before v0.5.6, which did not have last_sync set automatically
205+
# upon provisioning. Defaults to the branch creation time.
206+
last_sync = self.last_sync or self.created
207207
if self.status == BranchStatusChoices.READY:
208208
return ObjectChange.objects.using(DEFAULT_DB_ALIAS).exclude(
209209
application__branch=self
210210
).filter(
211211
changed_object_type__in=get_branchable_object_types(),
212-
time__gt=self.synced_time
212+
time__gt=last_sync
213213
)
214214
return ObjectChange.objects.none()
215215

@@ -251,10 +251,13 @@ def is_stale(self):
251251
"""
252252
Indicates whether the branch is too far out of date to be synced.
253253
"""
254+
if self.last_sync is None:
255+
# Branch has not yet been provisioned
256+
return False
254257
if not (changelog_retention := get_config().CHANGELOG_RETENTION):
255258
# Changelog retention is disabled
256259
return False
257-
return self.synced_time < timezone.now() - timedelta(days=changelog_retention)
260+
return self.last_sync < timezone.now() - timedelta(days=changelog_retention)
258261

259262
def sync(self, user, commit=True):
260263
"""
@@ -639,7 +642,10 @@ def provision(self, user):
639642

640643
logger.info('Provisioning completed')
641644

642-
Branch.objects.filter(pk=self.pk).update(status=BranchStatusChoices.READY)
645+
Branch.objects.filter(pk=self.pk).update(
646+
status=BranchStatusChoices.READY,
647+
last_sync=timezone.now(),
648+
)
643649
BranchEvent.objects.create(branch=self, user=user, type=BranchEventTypeChoices.PROVISIONED)
644650

645651
provision.alters_data = True

netbox_branching/templates/netbox_branching/branch.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,17 @@ <h5 class="card-header">{% trans "Branch" %}</h5>
8383
<tr>
8484
<th scope="row">{% trans "Last synced" %}</th>
8585
<td>
86-
{{ object.synced_time|isodatetime }}
87-
{% if object.is_stale %}
88-
<span class="text-danger" title="{% trans "Branch is stale and can no longer be synced" %}">
89-
<i class="mdi mdi-alert-circle"></i>
90-
</span>
86+
{% if object.last_sync %}
87+
{{ object.last_sync|isodatetime }}
88+
{% if object.is_stale %}
89+
<span class="text-danger" title="{% trans "Branch is stale and can no longer be synced" %}">
90+
<i class="mdi mdi-alert-circle"></i>
91+
</span>
92+
{% endif %}
93+
<div class="small text-muted">{{ object.last_sync|timesince }} {% trans "ago" %}</div>
94+
{% else %}
95+
{{ ''|placeholder }}
9196
{% endif %}
92-
<div class="small text-muted">{{ object.synced_time|timesince }} {% trans "ago" %}</div>
9397
</td>
9498
</tr>
9599
<tr>

0 commit comments

Comments
 (0)