Skip to content

Commit

Permalink
fix(mysql): generate IF NOT EXISTS if force=True (#10785)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored Feb 4, 2025
1 parent c7d1d38 commit d613b55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ def get_schema(
return sch.Schema(fields)

def create_database(self, name: str, force: bool = False) -> None:
sql = sge.Create(kind="DATABASE", exist=force, this=sg.to_identifier(name)).sql(
self.name
)
sql = sge.Create(
kind="DATABASE", exists=force, this=sg.to_identifier(name)
).sql(self.name)
with self.begin() as cur:
cur.execute(sql)

def drop_database(self, name: str, force: bool = False) -> None:
sql = sge.Drop(kind="DATABASE", exist=force, this=sg.to_identifier(name)).sql(
sql = sge.Drop(kind="DATABASE", exists=force, this=sg.to_identifier(name)).sql(
self.name
)
with self.begin() as cur:
Expand Down
24 changes: 23 additions & 1 deletion ibis/backends/mysql/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
MYSQL_PASS,
MYSQL_USER,
)
from ibis.backends.tests.errors import MySQLOperationalError
from ibis.backends.tests.errors import MySQLOperationalError, MySQLProgrammingError
from ibis.util import gen_name

MYSQL_TYPES = [
Expand Down Expand Up @@ -245,3 +245,25 @@ def test_invalid_port():
url = f"mysql://{MYSQL_USER}:{MYSQL_PASS}@{MYSQL_HOST}:{port}/{IBIS_TEST_MYSQL_DB}"
with pytest.raises(MySQLOperationalError):
ibis.connect(url)


def test_create_database_exists(con):
con.create_database(dbname := gen_name("dbname"))

with pytest.raises(MySQLProgrammingError):
con.create_database(dbname)

con.create_database(dbname, force=True)

con.drop_database(dbname, force=True)


def test_drop_database_exists(con):
con.create_database(dbname := gen_name("dbname"))

con.drop_database(dbname)

with pytest.raises(MySQLOperationalError):
con.drop_database(dbname)

con.drop_database(dbname, force=True)

0 comments on commit d613b55

Please sign in to comment.