diff --git a/google/cloud/spanner_dbapi/cursor.py b/google/cloud/spanner_dbapi/cursor.py index bcb614cf7e..b00675dbb8 100644 --- a/google/cloud/spanner_dbapi/cursor.py +++ b/google/cloud/spanner_dbapi/cursor.py @@ -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 diff --git a/tests/unit/spanner_dbapi/test_cursor.py b/tests/unit/spanner_dbapi/test_cursor.py index 889061cd83..4d5db01eac 100644 --- a/tests/unit/spanner_dbapi/test_cursor.py +++ b/tests/unit/spanner_dbapi/test_cursor.py @@ -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;" + ) + + self.assertEqual(connection._ddl_statements, EXP_DDLS)