blob: b3615abcab9ddd12f8297dfc9ea12bb7681a0316 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2007 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""Django database backend for rdbms.
This acts as a simple wrapper around the MySQLdb database backend to utilize an
alternate configuration. When used in an application running on
Google App Engine, this backend will use the GAE Apiproxy as a communications
driver. When used with dev_appserver, or from outside the context of an App
Engine app, this backend will instead use a driver that communicates over the
Google API for SQL Service.
Communicating over Google API requires valid OAuth 2.0 credentials. Before
the backend can be used with this transport on dev_appserver, users should
first run the Django 'syncdb' management command (or any other of the commands
that interact with the database), and follow the instructions to obtain an
OAuth2 token and persist it to disk for subsequent use.
If you should need to manually force the selection of a particular driver
module, you can do so by specifying it in the OPTIONS section of the database
configuration in For example:
'default': {
'ENGINE': '',
'NAME': 'mydb',
'USER': 'myusername',
'PASSWORD': 'mypassword',
'driver': '',
import logging
import os
import sys
import time
from django.core import exceptions
from django.db import backends
from django.db.backends import signals
from django.utils import safestring
from google.appengine.api import apiproxy_stub_map
from import rdbms
from import client
PROD_SERVER_SOFTWARE = 'Google App Engine'
modules_to_swap = (
old_modules = [(name, sys.modules.pop(name)) for name in modules_to_swap
if name in sys.modules]
sys.modules['MySQLdb'] = rdbms
from django.db.backends.mysql import base
for module_name, module in old_modules:
sys.modules[module_name] = module
('HOST', 'dsn', False),
('INSTANCE', 'instance', True),
('NAME', 'database', True),
('USER', 'user', False),
('PASSWORD', 'password', False),
('OAUTH2_SECRET', 'oauth2_refresh_token', False),
('driver', 'driver_name', False),
('oauth_storage', 'oauth_storage', False),
def _GetDriver(driver_name=None):
"""Imports the driver module specified by the given module name.
If no name is given, this will attempt to automatically determine an
appropriate driver to use based on the current environment. When running on
a production App Engine instance, the ApiProxy driver will be used, otherwise,
the Google API driver will be used. This conveniently allows the backend to
be used with the same configuration on production, and with command line tools
like syncdb.
driver_name: The name of the driver module to import.
The imported driver module, or None if a suitable driver can not be found.
if not driver_name:
base_pkg_path = ''
if apiproxy_stub_map.apiproxy.GetStub('rdbms'):
driver_name = base_pkg_path + 'rdbms_apiproxy'
driver_name = base_pkg_path + 'rdbms_googleapi'
return sys.modules[driver_name]
def Connect(driver_name=None, oauth2_refresh_token=None, **kwargs):
"""Gets an appropriate connection driver, and connects with it.
driver_name: The name of the driver module to use.
oauth2_refresh_token: The OAuth2 refresh token used to aquire an access
token for authenticating requests made by the Google API driver; defaults
to the value provided by the GOOGLE_SQL_OAUTH2_REFRESH_TOKEN environment
variable, if present.
kwargs: Additional keyword arguments to pass to the driver's connect
An rdbms.Connection subclass instance.
exceptions.ImproperlyConfigured: Valid OAuth 2.0 credentials could not be
found in storage and no oauth2_refresh_token was given.
driver = _GetDriver(driver_name)
server_software = os.getenv('SERVER_SOFTWARE', '').split('/')[0]
if (server_software in (DEV_SERVER_SOFTWARE, PROD_SERVER_SOFTWARE) and
if server_software == PROD_SERVER_SOFTWARE:
'Using the Google API driver is not recommended when running on '
'production App Engine. You should instead use the GAE API Proxy '
'driver (')
import oauth2client.client
from import rdbms_googleapi
from import oauth2storage
storage = kwargs.setdefault('oauth_storage',
credentials = storage.get()
if credentials is None or credentials.invalid:
if not oauth2_refresh_token:
oauth2_refresh_token = os.getenv('GOOGLE_SQL_OAUTH2_REFRESH_TOKEN')
if not oauth2_refresh_token:
raise exceptions.ImproperlyConfigured(
'No valid OAuth 2.0 credentials. Before using the Google SQL '
'Service backend on dev_appserver, you must first run " '
'syncdb" and proceed through the given instructions to fetch an '
'OAuth 2.0 token.')
credentials = oauth2client.client.OAuth2Credentials(
None, rdbms_googleapi.CLIENT_ID, rdbms_googleapi.CLIENT_SECRET,
oauth2_refresh_token, None,
return driver.connect(**kwargs)
class DatabaseOperations(base.DatabaseOperations):
"""DatabaseOperations for use with rdbms."""
def last_executed_query(self, cursor, sql, params):
"""Returns the query last executed by the given cursor.
Placeholders found in the given sql string will be replaced with actual
values from the params list.
cursor: The database Cursor.
sql: The raw query containing placeholders.
params: The sequence of parameters.
The string representing the query last executed by the cursor.
return backends.BaseDatabaseOperations.last_executed_query(
self, cursor, sql, params)
class DatabaseWrapper(base.DatabaseWrapper):
"""Django DatabaseWrapper for use with rdbms.
Overrides many pieces of the MySQL DatabaseWrapper for compatibility with
the rdbms API.
vendor = 'rdbms'
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self._last_ping_time = 0
self.client = client.DatabaseClient(self)
self.ops = DatabaseOperations()
except TypeError:
self.ops = DatabaseOperations(self)
def _valid_connection(self):
"""Disable ping on every operation."""
if self.connection is not None:
if time.time() - self._last_ping_time < PING_INTERVAL_SECS:
return True
self._last_ping_time = time.time()
return super(DatabaseWrapper, self)._valid_connection()
return False
def _cursor(self):
if not self._valid_connection():
kwargs = {'conv': base.django_conversions, 'dsn': None}
settings_dict = self.settings_dict
settings_dict.update(settings_dict.get('OPTIONS', {}))
for settings_key, kwarg, required in _SETTINGS_CONNECT_ARGS:
value = settings_dict.get(settings_key)
if value:
kwargs[kwarg] = value
elif required:
raise exceptions.ImproperlyConfigured(
"You must specify a '%s' for database '%s'" %
(settings_key, self.alias))
self.connection = Connect(**kwargs)
encoders = {safestring.SafeUnicode: self.connection.encoders[unicode],
safestring.SafeString: self.connection.encoders[str]}
signals.connection_created.send(sender=self.__class__, connection=self)
cursor = base.CursorWrapper(self.connection.cursor())
return cursor