Skip to content

Commit

Permalink
db: add convert_str_or_none helper function
Browse files Browse the repository at this point in the history
We have to do this in several parts of the code, so add a helper
function with docstring rather than duplicating the code and comment
everywhere.
  • Loading branch information
stintel committed Dec 13, 2023
1 parent 2de8d56 commit 41ba419
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions app/db/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
engine = create_engine(db_url, echo=False, connect_args=connect_args)


def convert_str_or_none(input):
""" Convert value to str or None
We're saving values of different type in the config_value field of WillowConfigTable
We therefore need to cast the value to str.
Casting None to str would result in "None" being save in the database.
This is a helper function to avoid that.
"""
if input is None:
return None
else:
return str(input)


def get_config_db():
config = WillowConfig()

Expand Down Expand Up @@ -78,9 +92,9 @@ def migrate_user_config(config):
db_config = WillowConfigTable(
config_type=WillowConfigType.config,
config_name=k,
config_value=convert_str_or_none(v)
)
# we need to cast to str here as we're saving different types (bool, int, str)
# casting None to str would save "None" in the db

if v is not None:
db_config.config_value = str(v)
session.add(db_config)
Expand Down Expand Up @@ -192,24 +206,14 @@ def save_config_to_db(config):
record = WillowConfigTable(
config_type=WillowConfigType.config,
config_name=name,
config_value=convert_str_or_none(value)
)
# we need to cast to str here as we're saving different types (bool, int, str)
# casting None to str would save "None" in the db
if value is None:
record.config_value = None
else:
record.config_value = str(value)

else:
if record.config_value == str(value):
if record.config_value == convert_str_or_none(value):
continue

# we need to cast to str here as we're saving different types (bool, int, str)
# casting None to str would save "None" in the db
if value is None:
record.config_value = None
else:
record.config_value = str(value)
record.config_value = convert_str_or_none(value)

session.add(record)

Expand Down

0 comments on commit 41ba419

Please sign in to comment.