Ramblings

March 10, 2008

Single place for settings for database

Filed under: web2py — michaelangela @ 4:17 am

While using web2py, this is how I am working with databases now.

Make a new application called ‘init’
In init.models create an empty __init__.py file so you can import the settings.
In init.models add create a settings.py file.
in init.models.settings add:

DB_DEV=’dev connection string’
DB_STAGING=’staging connection string’
DB_PRODUCTION=’production connection string’

def getDBConnection(request):
connect_string=None
try:
# we will only have http_x_farwarded_host when using mod_proxy on apach
# so it can only be the staging or production server
if request.env.http_x_forwarded_host:
connect_string=DB_STAGING if ‘staging’ in request.env.http_x_forwarded_host else DB_PRODUCTION
# so if there is no http_x_forwarded_host then it is the local dev box
elif not request.env.http_x_forwarded_host:
connect_string=DB_DEV
except:
connect_string=DB_DEV
return connect_string

In applications.yourapp.models.your_db_file

from applications.init.models import settings
db=SQLDB(settings.getDBConnection(request))

The global request variable is used to determine the host so it must be passed in. This can be expanded of course to include other things like email settings or whatever is needed.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: