@@ -165,10 +165,12 @@ def _validate_matching_prop(self, entity: _Entity, prop: Property, prop_json: Di
165165 except ValueError as error :
166166 raise ValueError (f"Property { entity .name } .{ prop .name } mismatches property found in JSON file: { error } " )
167167
168- def _load_or_assign_index (self , entity : _Entity , prop : Property , prop_json : Optional [Dict [str , Any ]]):
168+ def _load_or_assign_index (self , entity : _Entity , prop : Property , prop_json : Optional [Dict [str , Any ]]) -> bool :
169169 assert prop .index is not None
170170 index = prop .index
171171
172+ write_json = False
173+
172174 # Fetch index ID/UID from JSON file
173175 iduid_json = None
174176 if (prop_json is not None ) and ("indexId" in prop_json ):
@@ -187,8 +189,13 @@ def _load_or_assign_index(self, entity: _Entity, prop: Property, prop_json: Opti
187189 else : # Assign new ID to new index
188190 index .iduid = IdUid (self .model .last_index_iduid .id + 1 , index .uid )
189191 self .model .last_index_iduid = index .iduid
192+ write_json = True
193+
194+ return write_json
195+
196+ def _load_or_assign_property (self , entity : _Entity , prop : Property , entity_json : Optional [Dict [str , Any ]]) -> bool :
197+ write_json = False
190198
191- def _load_or_assign_property (self , entity : _Entity , prop : Property , entity_json : Optional [Dict [str , Any ]]):
192199 prop_json = None
193200 if prop .has_uid ():
194201 if entity_json is not None :
@@ -197,6 +204,8 @@ def _load_or_assign_property(self, entity: _Entity, prop: Property, entity_json:
197204 # User provided a UID not matching any property (within the entity), make sure it's not assigned
198205 # elsewhere
199206 self ._validate_uid_unassigned (prop .uid )
207+ else :
208+ write_json = prop .name != prop_json ["name" ] # If renaming we shall update the JSON
200209 else :
201210 if entity_json is not None :
202211 prop_json = self ._find_property_json_by_name (entity_json , prop .name )
@@ -210,20 +219,31 @@ def _load_or_assign_property(self, entity: _Entity, prop: Property, entity_json:
210219 prop .iduid .uid = self ._generate_uid ()
211220 prop .iduid = IdUid (entity .last_property_iduid .id + 1 , prop .iduid .uid )
212221 entity .last_property_iduid = prop .iduid
222+ write_json = True
213223
214224 if prop .index is not None :
215- self ._load_or_assign_index (entity , prop , prop_json )
225+ write_json |= self ._load_or_assign_index (entity , prop , prop_json )
226+
227+ return write_json
228+
229+ def _load_or_assign_entity (self , entity : _Entity ) -> bool :
230+ write_json = False
216231
217- def _load_or_assign_entity (self , entity : _Entity ):
218232 # entity_json = None
219233 if entity .has_uid ():
220234 entity_json = self ._find_entity_json_by_uid (entity .uid )
221235 if entity_json is None :
222236 # User provided a UID not matching any entity, make sure it's not assigned elsewhere
223237 self ._validate_uid_unassigned (entity .uid )
238+ else :
239+ write_json = entity .name != entity_json ["name" ] # If renaming we shall update the JSON
224240 else :
225241 entity_json = self ._find_entity_json_by_name (entity .name )
226242
243+ # Write JSON if the number of properties differs (to handle removed property)
244+ if entity_json is not None :
245+ write_json |= len (entity .properties ) != len (entity_json ["properties" ])
246+
227247 if entity_json is not None : # Load existing IDs from JSON
228248 entity .iduid = IdUid .from_str (entity_json ["id" ])
229249 entity .last_property_iduid = IdUid .from_str (entity_json ["lastPropertyId" ])
@@ -233,27 +253,42 @@ def _load_or_assign_entity(self, entity: _Entity):
233253 entity .iduid = IdUid (self .model .last_entity_iduid .id + 1 , entity .iduid .uid )
234254 self .model .last_entity_iduid = entity .iduid
235255 entity .last_property_iduid = IdUid (0 , 0 )
256+ write_json = True
236257
237258 # Load properties
238259 for prop in entity .properties :
239- self ._load_or_assign_property (entity , prop , entity_json )
260+ write_json |= self ._load_or_assign_property (entity , prop , entity_json )
240261
241- def sync (self ):
242- """ Syncs the provided model with the model JSON file. """
262+ return write_json
263+
264+ def sync (self ) -> bool :
265+ """ Syncs the provided model with the model JSON file.
266+ Returns True if the model JSON was written. """
243267
244268 if self .model_json is not None :
245269 self .model .last_entity_iduid = IdUid .from_str (self .model_json ["lastEntityId" ])
246270 self .model .last_index_iduid = IdUid .from_str (self .model_json ["lastIndexId" ])
247271 # self.model.last_relation_iduid =
248272
273+ write_json = False
274+
275+ # Write JSON if the number of entities differs (to handle removed entity)
276+ if self .model_json is not None :
277+ write_json |= len (self .model_json ["entities" ]) != len (self .model .entities )
278+
249279 for entity in self .model .entities :
250- self ._load_or_assign_entity (entity )
280+ write_json |= self ._load_or_assign_entity (entity )
281+
282+ if write_json :
283+ logger .info (f"Model changed, writing model.json: { self .model_filepath } " )
284+ self ._save_model_json ()
251285
252- self . _save_model_json ()
286+ return write_json
253287
254288
255- def sync_model (model : Model , model_filepath : str = "objectbox-model.json" ):
256- """ Syncs the provided model with the model JSON file. """
289+ def sync_model (model : Model , model_filepath : str = "objectbox-model.json" ) -> bool :
290+ """ Syncs the provided model with the model JSON file.
291+ Returns True if the model JSON was written. """
257292
258293 id_sync = IdSync (model , model_filepath )
259- id_sync .sync ()
294+ return id_sync .sync ()
0 commit comments