Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions scripts/lavafuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_sql_type(python_type):

type_map = {
'datetime': 'INTEGER',
'date': 'INTEGER',
'date': 'TEXT',
}
return type_map.get(python_type, 'TEXT')

Expand Down Expand Up @@ -397,7 +397,8 @@ def lava_insert_sqlite_data(table_name, data, object_columns, headers, column_ma
data (list): A list of rows to insert, where each row is a sequence of values
corresponding to the headers.
object_columns (dict): A dictionary mapping column names to their data types.
Supports 'datetime' type for automatic timestamp conversion.
'datetime' values are stored as Unix timestamps (UTC).
'date' values are stored as YYYY-MM-DD strings (no time / TZ).
headers (list): A list of column headers. Each header can be a string or a tuple
where the first element is the column name.
column_map (dict): Column mapping configuration (currently unused in the function).
Expand Down Expand Up @@ -445,6 +446,26 @@ def lava_insert_sqlite_data(table_name, data, object_columns, headers, column_ma
value = value.replace(tzinfo=datetime.timezone.utc)
epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
value = (value - epoch).total_seconds()
elif sanitized_column in object_columns and object_columns[sanitized_column] == 'date':
# Store calendar dates as YYYY-MM-DD only, never midnight timestamps.
# Timestamps invite timezone day-shifts for date-only fields.
d = None
if isinstance(value, datetime.datetime):
d = value.date()
elif isinstance(value, datetime.date):
d = value
elif isinstance(value, str):
text = value.strip()
if text:
try:
d = datetime.date.fromisoformat(text[:10])
except ValueError:
try:
d = datetime.datetime.fromisoformat(text).date()
except ValueError:
d = None
if d is not None:
value = d.isoformat()
processed_row.append(value)
rows_to_insert.append(tuple(processed_row))

Expand Down
Loading