print(f"Existing Ids: {len(existing_ids)}") if json_data is list: schemes = json_data else: assert isinstance(json_data, dict) schemes = [json_data] for scheme in schemes: validate_code_scheme.verify_scheme(scheme) id = scheme["SchemeID"] if id in existing_ids: skipped_existing += 1 continue fcw.set_code_scheme(dataset_id, scheme) print(f"Written: {id}") added += 1 print(f"Added: {added}, Skipped: {skipped_existing}") elif content_type == "messages": added = 0 skipped_existing = 0 all_messages = fcw.get_all_messages(dataset_id) existing_ids = set() highest_seq_no = -1 for message in all_messages: existing_ids.add(message["MessageID"]) if message["SequenceNumber"] > highest_seq_no: highest_seq_no = message["SequenceNumber"]
if DATASET_ID not in dataset_ids: print("WARNING: dataset {} does not exist, this will create a new dataset". format(DATASET_ID)) if CONTENT_TYPE not in ["users", "schemes", "messages"]: print("update content_type {} not known".format(CONTENT_TYPE)) exit(1) json_data = json.loads(open(PATH, 'r').read()) if CONTENT_TYPE == "users": validate_user_list.verify_JSON_path(PATH) users_list = json_data print("Setting users for '{}': {}".format(DATASET_ID, users_list)) fcw.set_user_ids(DATASET_ID, users_list) print("Done") elif CONTENT_TYPE == "schemes": for scheme in json_data: validate_code_scheme.verify_scheme(scheme) id = scheme["SchemeID"] fcw.set_code_scheme(DATASET_ID, scheme) print("Updated: {}".format(id)) elif CONTENT_TYPE == "messages": for message in json_data: validate_message_structure.verify_message(message) messages = json_data fcw.add_and_update_dataset_messages_content_batch(DATASET_ID, messages) print('Done')
print( "Target Firestore still has datasets, can't restore. Please delete all existing data first:" ) print("Run:") print("firebase -P {} firestore:delete -r datasets".format(project_id)) print("") exit(1) with open(BACKUP_PATH, 'r') as f: for line in f: dataset = json.loads(line) dataset_id = dataset["dataset_id"] print(f"Restoring dataset {dataset_id}") for segment_id, segment in dataset["segments"].items(): print(f"Starting to restore segment {segment_id}") fcw.set_user_ids(segment_id, segment["users"]) for scheme in segment["schemes"]: fcw.set_code_scheme(segment_id, scheme) fcw.restore_segment_messages_content_batch(segment_id, segment["messages"]) fcw.set_segment_metrics(segment_id, segment["metrics"]) print(f"Restore complete: segment {segment_id}") print(f"Starting to restore segment_counts for dataset {dataset_id}") segment_count = len(dataset["segments"]) if segment_count > 1: fcw.set_segment_count(dataset_id, segment_count) print(f"Restore complete: segment_counts for dataset {dataset_id}") print(f"Restored dataset {dataset_id}")