Skip to content

Commit

Permalink
chore(mysql): handle timezone setting failure better
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Dec 21, 2022
1 parent 14128df commit 33de3bf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import atexit
import contextlib
import warnings
from typing import Literal

import sqlalchemy as sa
Expand Down Expand Up @@ -107,13 +108,18 @@ def do_connect(
def begin(self):
with super().begin() as bind:
previous_timezone = bind.execute('SELECT @@session.time_zone').scalar()
bind.execute("SET @@session.time_zone = 'UTC'")
try:
bind.execute("SET @@session.time_zone = 'UTC'")
except Exception as e: # noqa: BLE001
warnings.warn(f"Couldn't set MySQL timezone: {str(e)}")

try:
yield bind
finally:
query = "SET @@session.time_zone = '{}'"
bind.execute(query.format(previous_timezone))
try:
bind.execute(f"SET @@session.time_zone = '{previous_timezone}'")
except Exception as e: # noqa: BLE001
warnings.warn(f"Couldn't reset MySQL timezone: {str(e)}")

def _get_schema_using_query(self, query: str) -> sch.Schema:
"""Infer the schema of `query`."""
Expand Down

0 comments on commit 33de3bf

Please sign in to comment.