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(mysql): generate IF NOT EXISTS if force=True #10785

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
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
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)