Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jans-pycloudlib): set sql_mode while connecting to mysql #7713

Merged
merged 2 commits into from
Feb 14, 2024
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
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()