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

feat(db_api): support executing several DDLs separated by semicolon #277

Merged
merged 3 commits into from Mar 18, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -174,7 +174,10 @@ def execute(self, sql, args=None):
try:
classification = parse_utils.classify_stmt(sql)
if classification == parse_utils.STMT_DDL:
self.connection._ddl_statements.append(sql)
for ddl in sql.split(";"):
ddl = ddl.strip()
if ddl:
self.connection._ddl_statements.append(ddl)
return

# For every other operation, we've got to ensure that
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/spanner_dbapi/test_cursor.py
Expand Up @@ -862,3 +862,33 @@ def test_fetchmany_retry_aborted_statements_checksums_mismatch(self):
cursor.fetchmany(len(row))

run_mock.assert_called_with(statement, retried=True)

def test_ddls_with_semicolon(self):
"""
Check that one script with several DDL statements separated
with semicolons is splitted into several DDLs.
"""
from google.cloud.spanner_dbapi.connection import connect

EXP_DDLS = [
"CREATE TABLE table_name (row_id INT64) PRIMARY KEY ()",
"DROP INDEX index_name",
"DROP TABLE table_name",
]

with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
):
connection = connect("test-instance", "test-database")

cursor = connection.cursor()
cursor.execute(
"CREATE TABLE table_name (row_id INT64) PRIMARY KEY ();"
"DROP INDEX index_name;\n"
"DROP TABLE table_name;"
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
)

self.assertEqual(connection._ddl_statements, EXP_DDLS)