Skip to content

Commit baa02ee

Browse files
author
Ilya Gurov
authored
fix(db_api): ensure DDL statements are being executed (#290)
* fix(db_api): DDLs are not executed immediately in autocommit mode * fix DDLs committing, add system tests * erase insert statement
1 parent e5d4901 commit baa02ee

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

google/cloud/spanner_dbapi/connection.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ def commit(self):
243243
"""
244244
if self._autocommit:
245245
warnings.warn(AUTOCOMMIT_MODE_WARNING, UserWarning, stacklevel=2)
246-
elif self.inside_transaction:
246+
return
247+
248+
self.run_prior_DDL_statements()
249+
if self.inside_transaction:
247250
try:
248251
self._transaction.commit()
249252
self._release_session()

google/cloud/spanner_dbapi/cursor.py

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def execute(self, sql, args=None):
178178
ddl = ddl.strip()
179179
if ddl:
180180
self.connection._ddl_statements.append(ddl)
181+
if self.connection.autocommit:
182+
self.connection.run_prior_DDL_statements()
181183
return
182184

183185
# For every other operation, we've got to ensure that

tests/system/test_system_dbapi.py

+48
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,54 @@ def test_execute_many(self):
378378
self.assertEqual(res[0], 1)
379379
conn.close()
380380

381+
def test_DDL_autocommit(self):
382+
"""Check that DDLs in autocommit mode are immediately executed."""
383+
conn = Connection(Config.INSTANCE, self._db)
384+
conn.autocommit = True
385+
386+
cur = conn.cursor()
387+
cur.execute(
388+
"""
389+
CREATE TABLE Singers (
390+
SingerId INT64 NOT NULL,
391+
Name STRING(1024),
392+
) PRIMARY KEY (SingerId)
393+
"""
394+
)
395+
conn.close()
396+
397+
# if previous DDL wasn't committed, the next DROP TABLE
398+
# statement will fail with a ProgrammingError
399+
conn = Connection(Config.INSTANCE, self._db)
400+
cur = conn.cursor()
401+
402+
cur.execute("DROP TABLE Singers")
403+
conn.commit()
404+
405+
def test_DDL_commit(self):
406+
"""Check that DDLs in commit mode are executed on calling `commit()`."""
407+
conn = Connection(Config.INSTANCE, self._db)
408+
cur = conn.cursor()
409+
410+
cur.execute(
411+
"""
412+
CREATE TABLE Singers (
413+
SingerId INT64 NOT NULL,
414+
Name STRING(1024),
415+
) PRIMARY KEY (SingerId)
416+
"""
417+
)
418+
conn.commit()
419+
conn.close()
420+
421+
# if previous DDL wasn't committed, the next DROP TABLE
422+
# statement will fail with a ProgrammingError
423+
conn = Connection(Config.INSTANCE, self._db)
424+
cur = conn.cursor()
425+
426+
cur.execute("DROP TABLE Singers")
427+
conn.commit()
428+
381429

382430
def clear_table(transaction):
383431
"""Clear the test table."""

0 commit comments

Comments
 (0)