Skip to content

Commit 7382c50

Browse files
Eagerly write updates to Salesforce (#12)
I was about 800 records through updates when the program crashed because of an unrecognized state code. This is the second time a crash has happened. The issue is that none of the prior changes are saved in this case, so we have to rerun the entire program from the start. Now, the program eagerly writes updates when they are computed. Even if we crash on record 1000, the first 999 will have been written.
1 parent e719b3a commit 7382c50

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/main.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def main() -> None:
3434
zipcode_search_engine = SearchEngine()
3535
geocoder = Nominatim(user_agent="parking_reform_network_data_enrichment")
3636

37-
uid_to_changes: dict[str, dict[str, str]] = {}
37+
changed_records = 0
3838
for entry in entries:
3939
original_model_dump = entry.model_dump(by_alias=True)
4040

@@ -48,17 +48,20 @@ def main() -> None:
4848
entry.populate_metro_area(us_zip_to_metro, us_city_and_state_to_metro)
4949

5050
changes = entry.compute_changes(original_model_dump)
51-
if changes:
52-
logger.info(f"Changes made to {entry.uid}: {sorted(changes.keys())}")
53-
uid_to_changes[entry.uid] = changes
54-
55-
logger.info(f"Total changes made: {len(uid_to_changes)}")
51+
if not changes:
52+
continue
53+
54+
changed_records += 1
55+
changed_keys = sorted(changes.keys())
56+
if args.write:
57+
salesforce_api.write_change(salesforce_client, entry.uid, changes)
58+
logger.info(f"Changes saved to Salesforce for {entry.uid}: {changed_keys}")
59+
else:
60+
logger.info(
61+
f"Changes computed (but not written) for {entry.uid}: {changed_keys}"
62+
)
5663

57-
if args.write:
58-
salesforce_api.write_changes(salesforce_client, uid_to_changes)
59-
logger.info(f"Updated Salesforce records ({len(uid_to_changes)} records)")
60-
else:
61-
logger.warning("Results not written to Salesforce.")
64+
logger.info(f"Total records changed: {changed_records}")
6265

6366

6467
if __name__ == "__main__":

src/salesforce_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ def load_data(client: Salesforce) -> list[SalesforceEntry]:
2727
]
2828

2929

30-
def write_changes(
31-
client: Salesforce, uid_to_changes: dict[str, dict[str, str]]
32-
) -> None:
33-
for uid, changes in uid_to_changes.items():
34-
client.Contact.update(uid, changes) # type: ignore[operator]
30+
def write_change(client: Salesforce, uid: str, changes: dict[str, str]) -> None:
31+
client.Contact.update(uid, changes) # type: ignore[operator]

src/state_codes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"District of Columbia": "DC",
1111
"Florida": "FL",
1212
"Georgia": "GA",
13+
"Guam": "GU",
1314
"Hawaii": "HI",
1415
"Idaho": "ID",
1516
"Illinois": "IL",

0 commit comments

Comments
 (0)