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: Executing existing DDL statements on executemany statement execution #1032

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions google/cloud/spanner_dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ def executemany(self, operation, seq_of_params):
"Executing DDL statements with executemany() method is not allowed."
)

# For every operation, we've got to ensure that any prior DDL
olavloite marked this conversation as resolved.
Show resolved Hide resolved
# statements were run.
self.connection.run_prior_DDL_statements()
olavloite marked this conversation as resolved.
Show resolved Hide resolved

many_result_set = StreamedManyResultSets()

if class_ in (parse_utils.STMT_INSERT, parse_utils.STMT_UPDATING):
Expand Down
128 changes: 127 additions & 1 deletion tests/system/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from google.cloud.spanner_v1 import gapic_version as package_version
from . import _helpers


DATABASE_NAME = "dbapi-txn"

DDL_STATEMENTS = (
Expand Down Expand Up @@ -344,6 +343,133 @@ def test_DDL_autocommit(shared_instance, dbapi_database):
op.result()


def test_ddl_execute_autocommit_true(shared_instance, dbapi_database):
"""Check that DDL statement in autocommit mode results in successful
DDL statement execution for execute method."""

conn = Connection(shared_instance, dbapi_database)
conn.autocommit = True
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE DdlExecuteAutocommit (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
)
table = dbapi_database.table("DdlExecuteAutocommit")
assert table.exists() is True

cur.close()
conn.close()


def test_ddl_executemany_autocommit_true(shared_instance, dbapi_database):
"""Check that DDL statement in autocommit mode results in exception for
executemany method ."""

conn = Connection(shared_instance, dbapi_database)
conn.autocommit = True
cur = conn.cursor()
with pytest.raises(ProgrammingError):
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
cur.executemany(
"""
CREATE TABLE DdlExecuteManyAutocommit (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
""",
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
[],
)
table = dbapi_database.table("DdlExecuteManyAutocommit")
assert table.exists() is False

cur.close()
conn.close()


def test_ddl_execute(shared_instance, dbapi_database):
"""Check that DDL statement followed by non-DDL execute statement in
non autocommit mode results in successful DDL statement execution."""

conn = Connection(shared_instance, dbapi_database)
want_row = (
1,
"first-name",
)
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE DdlExecute (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
)
table = dbapi_database.table("DdlExecute")
assert table.exists() is False

cur.execute(
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
"""
INSERT INTO DdlExecute (SingerId, Name)
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
VALUES (1, "first-name")
"""
)
assert table.exists() is True
conn.commit()

# read the resulting data from the database
cur.execute("SELECT * FROM DdlExecute")
got_rows = cur.fetchall()

assert got_rows == [want_row]

cur.close()
conn.close()


def test_ddl_executemany(shared_instance, dbapi_database):
"""Check that DDL statement followed by non-DDL executemany statement in
non autocommit mode results in successful DDL statement execution."""

conn = Connection(shared_instance, dbapi_database)
want_row = (
1,
"first-name",
)
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE DdlExecuteMany (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
)
table = dbapi_database.table("DdlExecuteMany")
assert table.exists() is False

cur.executemany(
"""
INSERT INTO DdlExecuteMany (SingerId, Name)
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
VALUES (%s, %s)
""",
[want_row],
)
assert table.exists() is True
conn.commit()

# read the resulting data from the database
cur.execute("SELECT * FROM DdlExecuteMany")
got_rows = cur.fetchall()

assert got_rows == [want_row]

cur.close()
conn.close()


@pytest.mark.skipif(_helpers.USE_EMULATOR, reason="Emulator does not support json.")
def test_autocommit_with_json_data(shared_instance, dbapi_database):
"""
Expand Down