Skip to content

Commit

Permalink
Added a conditional import to the gaerdbms dialect which attempts
Browse files Browse the repository at this point in the history
to import rdbms_apiproxy vs. rdbms_googleapi to work
on both dev and production platforms.  Also now honors the
``instance`` attribute.  Courtesy Sean Lynch.
[ticket:2649]
  • Loading branch information
zzzeek committed Feb 2, 2013
1 parent 681a0b1 commit a6697a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 9 additions & 0 deletions doc/build/changelog/changelog_08.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
.. changelog::
:version: 0.8.0

.. change::
:tags: bug, mysql, gae
:tickets: 2649

Added a conditional import to the ``gaerdbms`` dialect which attempts
to import rdbms_apiproxy vs. rdbms_googleapi to work
on both dev and production platforms. Also now honors the
``instance`` attribute. Courtesy Sean Lynch.

.. change::
:tags: bug, sql
:tickets: 2496
Expand Down
28 changes: 22 additions & 6 deletions lib/sqlalchemy/dialects/mysql/gaerdbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.. dialect:: mysql+gaerdbms
:name: Google Cloud SQL
:dbapi: rdbms
:connectstring: mysql+gaerdbms:///<dbname>?instance=instancename
:connectstring: mysql+gaerdbms:///<dbname>?instance=<instancename>
:url: https://developers.google.com/appengine/docs/python/cloud-sql/developers-guide
This dialect is based primarily on the :mod:`.mysql.mysqldb` dialect with minimal
Expand Down Expand Up @@ -35,18 +35,34 @@ class MySQLDialect_gaerdbms(MySQLDialect_mysqldb):

@classmethod
def dbapi(cls):
from google.appengine.api import rdbms
return rdbms
# from django:
# http://code.google.com/p/googleappengine/source/
# browse/trunk/python/google/storage/speckle/
# python/django/backend/base.py#118
# see also [ticket:2649]
# see also http://stackoverflow.com/q/14224679/34549
from google.appengine.api import apiproxy_stub_map

if apiproxy_stub_map.apiproxy.GetStub('rdbms'):
from google.storage.speckle.python.api import rdbms_apiproxy
return rdbms_apiproxy
else:
from google.storage.speckle.python.api import rdbms_googleapi
return rdbms_googleapi

@classmethod
def get_pool_class(cls, url):
# Cloud SQL connections die at any moment
return NullPool

def create_connect_args(self, url):
return [[], {'database': url.database,
'user': url.username,
'password': url.password}]
opts = url.translate_connect_args()
# 'dsn' and 'instance' are because we are skipping
# the traditional google.api.rdbms wrapper

opts['dsn'] = ''
opts['instance'] = url.query['instance']
return [], opts

def _extract_error_code(self, exception):
match = re.compile(r"^(\d+):").match(str(exception))
Expand Down

0 comments on commit a6697a8

Please sign in to comment.