|
| 1 | +import pandas as pd |
| 2 | + |
| 3 | +from bedrock_ge.gi.schemas import ( |
| 4 | + BedrockGIDatabase, |
| 5 | + InSituTestSchema, |
| 6 | + LocationSchema, |
| 7 | + ProjectSchema, |
| 8 | + SampleSchema, |
| 9 | +) |
| 10 | +from bedrock_ge.gi.validate import check_foreign_key |
| 11 | + |
| 12 | + |
| 13 | +def merge_databases( |
| 14 | + target_db: BedrockGIDatabase, |
| 15 | + incoming_db: BedrockGIDatabase, |
| 16 | +) -> BedrockGIDatabase: |
| 17 | + """Merges the incoming Bedrock GI database into the target Bedrock GI database. |
| 18 | +
|
| 19 | + The function concatenates the pandas DataFrames of the second dict of |
| 20 | + DataFrames to the first dict of DataFrames for the keys they have in common. |
| 21 | + Keys that are unique to either dictionary will be included in the final |
| 22 | + concatenated dictionary. |
| 23 | +
|
| 24 | + Args: |
| 25 | + target_db (BedrockGIDatabase): The Bedrock GI database into which the incoming data will be merged. |
| 26 | + incoming_db (BedrockGIDatabase): The Bedrock GI database containing the data to be merged. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + BedrockGIDatabase: Merged Bedrock GI database. |
| 30 | + """ |
| 31 | + # write the body of this function that merges the incoming_db (BedrockGIDatabase) into the target_db (BedrockGIDatabase). |
| 32 | + # duplicate rows in the incoming_db (BedrockGIDatabase) will be dropped. |
| 33 | + # After merging tables validate them with the schemas from bedrock_ge.gi.schemas and check that foreign keys are correct. |
| 34 | + # In case the incoming_db contains tables that are not in the target_db, add them to the target_db. |
| 35 | + # The function must return a BedrockGIDatabase object. |
| 36 | + merged_project = pd.concat( |
| 37 | + [target_db.Project, incoming_db.Project], ignore_index=True |
| 38 | + ) |
| 39 | + ProjectSchema.validate(merged_project) |
| 40 | + |
| 41 | + merged_location = pd.concat( |
| 42 | + [target_db.Location, incoming_db.Location], ignore_index=True |
| 43 | + ) |
| 44 | + LocationSchema.validate(merged_location) |
| 45 | + check_foreign_key("project_uid", merged_project, merged_location) |
| 46 | + |
| 47 | + merged_insitu = {} |
| 48 | + |
| 49 | + merged_db = { |
| 50 | + "Project": merged_project, |
| 51 | + "Location": merged_location, |
| 52 | + } |
| 53 | + |
| 54 | + # merged_db = BedrockGIDatabase( |
| 55 | + # Project=target_db.Project.append(incoming_db.Project), |
| 56 | + # Location=target_db.Location.append(incoming_db.Location), |
| 57 | + # InSituTests={ |
| 58 | + # k: target_db.InSituTests[k].append(incoming_db.InSituTests[k]) |
| 59 | + # for k in target_db.InSituTests |
| 60 | + # if k in incoming_db.InSituTests |
| 61 | + # }, |
| 62 | + # Sample=target_db.Sample.append(incoming_db.Sample), |
| 63 | + # LabTests={ |
| 64 | + # k: target_db.LabTests[k].append(incoming_db.LabTests[k]) |
| 65 | + # for k in target_db.LabTests |
| 66 | + # if k in incoming_db.LabTests |
| 67 | + # }, |
| 68 | + # Other={ |
| 69 | + # k: target_db.Other[k].append(incoming_db.Other[k]) |
| 70 | + # for k in target_db.Other |
| 71 | + # if k in incoming_db.Other |
| 72 | + # }, |
| 73 | + # ) |
| 74 | + return merged_db |
0 commit comments