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: missing method implementation and potential bug in the DatabaseWrapper class #545

Merged
merged 8 commits into from Dec 17, 2020
20 changes: 13 additions & 7 deletions django_spanner/base.py
Expand Up @@ -5,7 +5,9 @@
# https://developers.google.com/open-source/licenses/bsd

from django.db.backends.base.base import BaseDatabaseWrapper
from google.cloud import spanner_dbapi as Database, spanner_v1 as spanner

import google.cloud.spanner_dbapi as Database
c24t marked this conversation as resolved.
Show resolved Hide resolved
import google.cloud.spanner_v1 as spanner

from .client import DatabaseClient
from .creation import DatabaseCreation
Expand Down Expand Up @@ -81,8 +83,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
# special characters for REGEXP_CONTAINS operators (e.g. \, *, _) must be
# escaped on database side.
pattern_esc = r'REPLACE(REPLACE(REPLACE({}, "\\", "\\\\"), "%%", r"\%%"), "_", r"\_")'
# These are all no-ops in favor of using REGEXP_CONTAINS in the customized
# lookups.

# These are all no-ops in favor of using REGEXP_CONTAINS
# in the customized lookups.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# These are all no-ops in favor of using REGEXP_CONTAINS
# in the customized lookups.
# These are all no-ops in favor of using REGEXP_CONTAINS in the customized
# lookups.

Why rewrap this?

pattern_ops = {
"contains": "",
"icontains": "",
Expand Down Expand Up @@ -124,7 +127,9 @@ def get_new_connection(self, conn_params):
return Database.connect(**conn_params)

def init_connection_state(self):
pass
self.connection.close()
c24t marked this conversation as resolved.
Show resolved Hide resolved
database = self.connection.database
self.connection.__init__(self.instance, database)

def create_cursor(self, name=None):
return self.connection.cursor()
Expand All @@ -134,12 +139,13 @@ def _set_autocommit(self, autocommit):
self.connection.autocommit = autocommit

def is_usable(self):
if self.connection is None:
if self.connection is None or self.connection.is_closed:
return False

try:
# Use a cursor directly, bypassing Django's utilities.
self.connection.cursor().execute("SELECT 1")
except Database.Error:
return False
else:
return True

return True