Skip to content

Commit 8770456

Browse files
authored
fix: Modified populate script to not change the official DB value if csv field is empty. (#1053)
1 parent 1643b21 commit 8770456

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

api/src/scripts/populate_db.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,28 @@ def get_safe_value(row, column_name, default_value):
8080
"""
8181
Get a safe value from the row
8282
"""
83-
if not row[column_name] or pandas.isna(row[column_name]) or f"{row[column_name]}".strip() == "":
84-
return default_value if default_value is not None else None
85-
return f"{row[column_name]}".strip()
83+
value = row[column_name]
84+
if not value or pandas.isna(value) or f"{value}".strip() == "":
85+
return default_value
86+
return f"{value}".strip()
87+
88+
@staticmethod
89+
def get_safe_boolean_value(row, column_name, default_value: bool | None) -> bool | None:
90+
"""
91+
Get a safe boolean value from the row
92+
Only allowed values are "true" and "false" (case insensitive)
93+
Anything else returns the default.
94+
"""
95+
value = row[column_name]
96+
if value is None or pandas.isna(value) or f"{value}".strip() == "":
97+
return default_value
98+
# I am not sure if pandas will convert "TRUE" and "FALSE" to boolean, so go back to using a string
99+
value = f"{value}".strip().lower()
100+
if value == "true":
101+
return True
102+
if value == "false":
103+
return False
104+
return default_value
86105

87106
@staticmethod
88107
def get_location_id(country_code, subdivision_name, municipality):

api/src/scripts/populate_db_gtfs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,10 @@ def populate_db(self, session: "Session"):
192192
# Create or update the GTFS feed
193193
data_type = self.get_data_type(row)
194194
stable_id = self.get_stable_id(row)
195-
is_official_from_csv = self.get_safe_value(row, "is_official", "false").lower() == "true"
195+
is_official_from_csv = self.get_safe_boolean_value(row, "is_official", None)
196196
feed = self.query_feed_by_stable_id(session, stable_id, data_type)
197197
if feed:
198198
self.logger.debug(f"Updating {feed.__class__.__name__}: {stable_id}")
199-
if feed.official != is_official_from_csv:
200-
feed.official = is_official_from_csv
201-
feed.official_updated_at = datetime.now(pytz.utc)
202199
else:
203200
feed = self.get_model(data_type)(
204201
id=generate_unique_id(),
@@ -219,8 +216,12 @@ def populate_db(self, session: "Session"):
219216
source="mdb",
220217
)
221218
]
222-
feed.official = is_official_from_csv
223-
feed.official_updated_at = datetime.now(pytz.utc)
219+
220+
# If the is_official field from the CSV is empty, the value here will be None and we don't touch the DB
221+
if is_official_from_csv is not None:
222+
if feed.official != is_official_from_csv:
223+
feed.official = is_official_from_csv
224+
feed.official_updated_at = datetime.now(pytz.utc)
224225

225226
# Populate common fields from Feed
226227
feed.feed_name = self.get_safe_value(row, "name", "")

functions-python/export_csv/src/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def export_csv(csv_file_path: str):
130130

131131
count = 0
132132
for feed in fetch_feeds():
133+
# We're counting on the writer to leave an empty field in the csv for None values
133134
writer.writerow(feed)
134135
count += 1
135136

0 commit comments

Comments
 (0)