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

fix(db_api): ensure DDL statements are being executed #290

Merged
merged 3 commits into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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/connection.py
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
2 changes: 2 additions & 0 deletions google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should execute the statements immediately regardless of whether autocommit is set or not. Because Spanner handles DDL separately from transactions, there's no way to roll back DDL statements (which we will need to clearly document) and so I think having different behavior based on autocommit is unnecessary.

@AVaksman WDYT? Note that we now support executing multiple DDL statements at once which should alleviate the batching concern.

Copy link
Contributor Author

@IlyaFaer IlyaFaer Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larkee, no problem, I'll delete the line.

I'm writing a system test to check the case.

Also I've found out that in !autocommit mode DDLs are also not executed, even if one will call commit(). Will fix it in this PR as well.

Безымянный

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larkee, you know, about regarding autocommit mode... I think users don't usually go deep into frameworks code. So, we support DDLs separated with ;, still users may use frameworks, which are generating and executing DDLs one by one. In this case it's probably better to look after autocommit mode, to make DDLs pumping up in !autocommit mode - to execute them all at once little bit later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a good point. In that case, we'll need to add run_prior_DDL_statements() to commit() as currently the statements aren't being executed even when commit() is called.

self.connection.run_prior_DDL_statements()
return

# For every other operation, we've got to ensure that
Expand Down
56 changes: 56 additions & 0 deletions tests/system/test_system_dbapi.py
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")
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
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")
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
"""
)
conn.commit()


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