Skip to content

Commit d11cb1f

Browse files
authored
Merge pull request #3 from xmalet-nrcan/main
BUG FIX
2 parents 0ca66a9 + 2db8c5e commit d11cb1f

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

nrcan_etl_toolbox/database/interface/abstract_database_objects_handlers.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import TypeVar
55

66
import sqlalchemy.engine
7+
from dateutil import parser as date_parser
78
from psycopg2.errors import UniqueViolation
89
from sqlalchemy import BinaryExpression, create_engine, func
910
from sqlalchemy.exc import DataError, IntegrityError
@@ -22,6 +23,26 @@ class AbstractDatabaseObjectsInterface:
2223

2324
logger = CustomLogger("database_objects_handler", logger_type="default")
2425

26+
@staticmethod
27+
def _is_date_valid(date_string: str) -> bool:
28+
"""
29+
Validates if a date string is in a valid date format.
30+
31+
Args:
32+
date_string: The string to validate
33+
34+
Returns:
35+
bool: True if the date is valid, False otherwise
36+
"""
37+
if not isinstance(date_string, str):
38+
return False
39+
try:
40+
# dateutil.parser can handle many date formats
41+
date_parser.parse(date_string)
42+
return True
43+
except (ValueError, TypeError):
44+
return False
45+
2546
def __init__(self, database_url: str, db_objects_to_treat: list = None, logger_level="DEBUG"):
2647
if db_objects_to_treat is None:
2748
db_objects_to_treat = []
@@ -60,10 +81,10 @@ def _connect_to_database(self, database_url):
6081
def _insert_object(self, db_object: Base) -> bool | None:
6182
with self.session as session:
6283
try:
63-
session.begin(nested=True)
64-
session.add(db_object)
65-
session.commit()
66-
return True
84+
with session.begin(nested=True):
85+
session.add(db_object)
86+
session.commit()
87+
return True
6788
except IntegrityError as e:
6889
session.rollback()
6990
if isinstance(e.orig, UniqueViolation):

nrcan_etl_toolbox/database/orm/base/base_table_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def get_query_for_object(
156156
else:
157157
# Collect conditions dynamically
158158
for attr, value in filters.items():
159-
if value is not None and hasattr(cls, attr):
159+
if value is not None and hasattr(cls, attr) and not isinstance(value, Base):
160160
# Add equality and "LIKE" conditions
161161
column_attr = getattr(cls, attr)
162162

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[project]
22
name = "nrcan_etl_toolbox"
3-
version = "0.1.40"
3+
4+
version = "0.1.45"
5+
46
description = "Package for logging and database interfacing using SQLAlchemy and SQLModels"
57
authors = [
68
{ name = "Xavier Malet", email = "[email protected]" }

0 commit comments

Comments
 (0)