Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix DDLs committing, add system tests
  • Loading branch information
IlyaFaer committed Mar 24, 2021
commit 4a84c86dcd079639de305d69f7ae3850e24960b4
5 changes: 4 additions & 1 deletion google/cloud/spanner_dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ def commit(self):
"""
if self._autocommit:
warnings.warn(AUTOCOMMIT_MODE_WARNING, UserWarning, stacklevel=2)
elif self.inside_transaction:
return

self.run_prior_DDL_statements()
if self.inside_transaction:
try:
self._transaction.commit()
self._release_session()
Expand Down
56 changes: 56 additions & 0 deletions tests/system/test_system_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,62 @@ def test_execute_many(self):
self.assertEqual(res[0], 1)
conn.close()

def test_DDL_autocommit(self):
"""Check that DDLs in autocommit mode are immediately executed."""
conn = Connection(Config.INSTANCE, self._db)
conn.autocommit = True

cur = conn.cursor()
cur.execute(
"""
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
)
conn.close()

# if previous DDL wasn't committed, the next INSERT
# will fail with a ProgrammingError
conn = Connection(Config.INSTANCE, self._db)

cur = conn.cursor()
cur.execute("""INSERT INTO Singers (SingerId, Name) VALUES (1, "Name")""")

conn.commit()

cur.execute("DROP TABLE Singers")
conn.commit()

def test_DDL_commit(self):
"""Check that DDLs in commit mode are executed on calling `commit()`."""
conn = Connection(Config.INSTANCE, self._db)
cur = conn.cursor()

cur.execute(
"""
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
)
conn.commit()
conn.close()

# if previous DDL wasn't committed, the next INSERT
# will fail with a ProgrammingError
conn = Connection(Config.INSTANCE, self._db)
cur = conn.cursor()

cur.execute(
"""
INSERT INTO Singers (SingerId, Name) VALUES (1, "Name")
"""
)
conn.commit()


def clear_table(transaction):
"""Clear the test table."""
Expand Down