Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How to setup multiple databases in TG2
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mike Driscoll  
View profile  
 More options Jun 9 2009, 10:10 am
From: Mike Driscoll <kyoso...@gmail.com>
Date: Tue, 9 Jun 2009 07:10:22 -0700 (PDT)
Local: Tues, Jun 9 2009 10:10 am
Subject: Re: How to setup multiple databases in TG2
For the record, this was resolved. I didn't know how to set up the
second session object.

Thanks!

Mike

On Jun 5, 2:06 pm, percious <ch...@percious.com> wrote:

> It's not too bad, but you have to do your own custom app setup, and
> modify your init_model stuff to create multiple transaction manager.

> app_cfg.py:

> from webapp.model import init_model
> from sqlalchemy import engine_from_config
> from tg.configuration import AppConfig, Bunch
> from pylons import config as pylons_config

> class MyAppConfig(AppConfig):
>     def setup_sqlalchemy(self):
>         """Setup SQLAlchemy database engine"""
>         users_engine = engine_from_config(pylons_config,
> 'sqlalchemy_users.')
>         samples_engine = engine_from_config(pylons_config,
> 'sqlalchemy_samples.')
>         config['pylons.app_globals'].sa_engine = users_engine
>         config['pylons.app_globals'].sa_users_engine = users_engine
>         config['pylons.app_globals'].sa_samples_engine =
> samples_engine

>         # Pass the engine to initmodel, to be able to introspect
> tables
>         init_model(users_engine, samples_engine)

> base_config = MyAppConfig()
> ...

> model/__init__.py:

> import model.users
> import model.samples

> from model.users.mappers import *
> from model.samples.mappers import *

> from zope.sqlalchemy import ZopeTransactionExtension
> from sqlalchemy.orm import scoped_session, sessionmaker

> # Global session manager.  DBSession() returns the session object
> # appropriate for the current web request.
> maker = sessionmaker(autoflush=True, autocommit=False,
>                      extension=ZopeTransactionExtension())

> DBSession = UsersDBSession = scoped_session(maker)
> maker3 = sessionmaker(autoflush=True, autocommit=False,
>                      extension=ZopeTransactionExtension())

> SamplesDBSession = scoped_session(maker3)
> from model.users.metadata import metadata as users_metadata
> from model.samples.metadata import metadata as samples_metadata

> def init_model(users_engine, samples_engine):
>     """Call me before using any of the tables or classes in the
> model."""

>     global UsersDBSession, SamplesDBSession, users_metadata,
> samples_metadata

>     UsersDBSession.configure(bind=users_engine)
>     SamplesDBSession.configure(bind=samples_engine)

>     users_metadata.bind = users_engine
>     samples_metadata.bind = samples_engine

> This is sort of an off-the cuff answer, we will provide a better one
> in the next documentation release.

> Come find me on IRC if you need some more help.

> cheers.
> -chris

> On Jun 4, 10:31 am, Mike Driscoll <kyoso...@gmail.com> wrote:

> > Hi,

> > According to Mark Ramm's blog, supporting multiple databases is
> > supposed to be easy (seehttp://compoundthinking.com/blog/index.php/2008/07/31/10-reasons-why-...).
> > So where is the documentation that says how to do it??

> > I "think" I need to put multiple sqlalchemy.url's in my
> > development.ini file. But where oh where do I set up the SA engines
> > at? The model's __init__ claims to support multiple databases too
> > using MetaData, but it doesn't show where to bind the engine unless
> > you're supposed to use the undocumented init_model function somehow.

> > I'm pretty sure the init_model came from Perkins. Maybe he can tell me
> > where to put my call to the function and how to set it up for multiple
> > databases?

> > That would be great!

> > Thanks,

> > Mike

> > On May 28, 12:47 pm, Mike Driscoll <kyoso...@gmail.com> wrote:

> > > Hi,

> > > I am having a little trouble wrapping my head around how to configure
> > > my model's __init__.py file for multiple databases. I am working on an
> > > application that accesses 3 databases and does reflection on select
> > > tables from all three.

> > > I assume I need to comment out this line:

> > > metadata = DeclarativeBase.metadata

> > > and instead create three metadata instances based on MetaData(),
> > > correct?

> > > Finally, as I understand it, I can use the "init_model" function to do
> > > the mapping for some of them, but I am not understanding where to
> > > create the engine object to pass to it. Also, once I have my tables
> > > mapped in the function, what do I put in my real model file if
> > > anything?

> > > Hopefully that stuff makes sense. I am using Python 2.5 and TG2 in a
> > > virtualenv on Windows XP.

> > > Thanks!

> > > Mike


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.