- """Ensure objects from row_data_1st are in row_data_2nd objects, DB."""
- name_1st, objs_1st, _ = row_data_1st
- name_2nd, objs_2nd, db_2nd = row_data_2nd
- for obj in [obj for obj in objs_1st if obj not in objs_2nd]:
- print(f'SYNC: adding {name_1st}->{name_2nd} {getattr(obj, id_name)}')
- obj.save(db_2nd)
-
-
-def sync_relations(relation_1st: RelationData,
- relation_2nd: RelationData,
- yt_query_id: QueryId
+ """Equalize both DB's object collections; prefer newer states to older."""
+ cls, id_name = shared
+ obj_colls = cls.get_all(dbs[0]), cls.get_all(dbs[1])
+ for obj_0 in [obj for obj in obj_colls[0] if obj not in obj_colls[1]]:
+ id_ = getattr(obj_0, id_name)
+ sync_down = True
+ msg_verb = 'adding'
+ for obj_1 in [obj for obj in obj_colls[1] # other collection has …
+ if id_ == getattr(obj, id_name)]: # … obj, but non-equal
+ msg_verb = 'updating'
+ if hasattr(obj_0, ATTR_NAME_LAST_UPDATE):
+ last_update_0 = getattr(obj_0, ATTR_NAME_LAST_UPDATE)
+ last_update_1 = getattr(obj_1, ATTR_NAME_LAST_UPDATE)
+ sync_down = last_update_0 > last_update_1
+ if not sync_down:
+ print(f'SYNC: {msg_verb} '
+ f'{host_names[1]}->{host_names[0]} {id_}')
+ obj_1.save(dbs[0])
+ if sync_down:
+ print(f'SYNC: {msg_verb} {host_names[0]}->{host_names[1]} {id_}')
+ obj_0.save(dbs[1])
+
+
+def sync_relations(host_names: tuple[str, str],
+ dbs: tuple[DatabaseConnection, DatabaseConnection],
+ yt_result: YoutubeVideo