From e917dbec07362d3dee70ae66360f506515261c95 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Wed, 24 Mar 2021 11:16:31 +0300 Subject: [PATCH 1/3] fix(db_api): DDLs are not executed immediately in autocommit mode --- google/cloud/spanner_dbapi/cursor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/google/cloud/spanner_dbapi/cursor.py b/google/cloud/spanner_dbapi/cursor.py index b00675dbb8..a28879faba 100644 --- a/google/cloud/spanner_dbapi/cursor.py +++ b/google/cloud/spanner_dbapi/cursor.py @@ -178,6 +178,8 @@ def execute(self, sql, args=None): ddl = ddl.strip() if ddl: self.connection._ddl_statements.append(ddl) + if self.connection.autocommit: + self.connection.run_prior_DDL_statements() return # For every other operation, we've got to ensure that From 4a84c86dcd079639de305d69f7ae3850e24960b4 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Wed, 24 Mar 2021 12:14:56 +0300 Subject: [PATCH 2/3] fix DDLs committing, add system tests --- google/cloud/spanner_dbapi/connection.py | 5 ++- tests/system/test_system_dbapi.py | 56 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/google/cloud/spanner_dbapi/connection.py b/google/cloud/spanner_dbapi/connection.py index 9befe2027d..926408c928 100644 --- a/google/cloud/spanner_dbapi/connection.py +++ b/google/cloud/spanner_dbapi/connection.py @@ -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() diff --git a/tests/system/test_system_dbapi.py b/tests/system/test_system_dbapi.py index 1659fe239b..c1aaaa158f 100644 --- a/tests/system/test_system_dbapi.py +++ b/tests/system/test_system_dbapi.py @@ -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.""" From a02b4549d7a997c4a261e9a1c5bab717ff278d36 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Wed, 24 Mar 2021 15:49:54 +0300 Subject: [PATCH 3/3] erase insert statement --- tests/system/test_system_dbapi.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/system/test_system_dbapi.py b/tests/system/test_system_dbapi.py index c1aaaa158f..6ca1029ae1 100644 --- a/tests/system/test_system_dbapi.py +++ b/tests/system/test_system_dbapi.py @@ -394,14 +394,10 @@ def test_DDL_autocommit(self): ) conn.close() - # if previous DDL wasn't committed, the next INSERT - # will fail with a ProgrammingError + # if previous DDL wasn't committed, the next DROP TABLE + # statement 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() @@ -422,16 +418,12 @@ def test_DDL_commit(self): conn.commit() conn.close() - # if previous DDL wasn't committed, the next INSERT - # will fail with a ProgrammingError + # if previous DDL wasn't committed, the next DROP TABLE + # statement will fail with a ProgrammingError conn = Connection(Config.INSTANCE, self._db) cur = conn.cursor() - cur.execute( - """ - INSERT INTO Singers (SingerId, Name) VALUES (1, "Name") - """ - ) + cur.execute("DROP TABLE Singers") conn.commit()