Skip to content

Commit

Permalink
fix(jans-pycloudlib): set sql_mode while connecting to mysql (#7713)
Browse files Browse the repository at this point in the history
Signed-off-by: iromli <isman.firmansyah@gmail.com>
Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com>
  • Loading branch information
iromli and moabu committed Feb 14, 2024
1 parent 15fdacb commit 0df5213
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions jans-pycloudlib/jans/pycloudlib/persistence/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sqlalchemy import func
from sqlalchemy import select
from sqlalchemy import delete
from sqlalchemy import event
from ldif import LDIFParser
from ldap3.utils import dn as dnutils

Expand Down Expand Up @@ -256,11 +257,19 @@ def __init__(self, manager: Manager, *args: _t.Any, **kwargs: _t.Any) -> None:

self.dialect = self.adapter.dialect
self._metadata: _t.Optional[MetaData] = None
self._engine = None

@cached_property
@property
def engine(self) -> Engine:
"""Lazy init of engine instance object."""
return create_engine(self.engine_url, pool_pre_ping=True, hide_parameters=True)
if not self._engine:
self._engine = create_engine(self.engine_url, pool_pre_ping=True, hide_parameters=True)

if self.dialect == "mysql":
event.listen(self._engine, "first_connect", set_mysql_strict_mode)

# initialized engine
return self._engine

@property
def engine_url(self) -> str:
Expand Down Expand Up @@ -592,3 +601,9 @@ def render_sql_properties(manager: Manager, src: str, dest: str) -> None:
"server_time_zone": os.environ.get("CN_SQL_DB_TIMEZONE", "UTC"),
}
f.write(rendered_txt)


def set_mysql_strict_mode(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("SET SESSION sql_mode = 'TRADITIONAL'")
cursor.close()

0 comments on commit 0df5213

Please sign in to comment.