@@ -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
0 commit comments