Hello!

I have a couple of models that I use with a MySQL database. For
testing purposes I would like to set up in-memory fixtures for these
models using SQLite. Unfortunately the models are declared with
different schemas. Since SQLite doesn't seem to support schemas I'd
like to somehow normalize them to not use any particular schema.

The following is a simple outline of what I'm trying to do:

class A(Base):
    __tablename__ = 'table_a'
    __table_args__ = {'schema':'schema_a'}
    id = Column(Integer, primary_key=True)

class B(Base):
    __tablename__ = 'table_b'
    __table_args__ = {'schema':'schema_b'}
    id = Column('ID', Integer, primary_key=True)
    a_id = Column('A_ID', Integer, ForeignKey('schema_a.table_a.id'))
    c_id = Column('C_ID', Integer, ForeignKey('schema_c.table_c.id'))

    a = relation("A", backref=backref('bs'))
    b = relation("C")

class C(Base):
    __tablename__ = 'table_c'
    __table_args__ = {'schema': 'schema_c'}
    id = Column(Integer, primary_key=True)

def do_business_logic():
    # A bunch of logic that will use A, B and C.

def run_functional_test():
    # First, set up an in memory DB fixture
    engine = create_engine('sqlite://', echo=True)
    NewBase = declarative_base()
    strip_schema_from_model(NewBase)
    NewBase.metadata.create_all(engine)

    # Now populate it with instances of A, B and C
    ...
    # Run test logic, which at some point calls do_business_logic
    ...
    # Tear down the fixture
    ...

def strip_schema_from_models(decl_base):
    ''' Redefine A, B and C so that:

    1. They all belong to the same schema (the default schema: None or
similar.)
    2. They are constructed in the same way, ie A() will be used to
create a tweaked instance of A
    3. All logic involving instances of A, B and C should also be
valid for instances of the redefined classes

    '''

Is it possible to implement strip_schema_from_model so that all my
criteria are met? One way that comes to mind is to use tometadata to
copy the table information for each model, create new model classes
using this information and override globals() for A, B and C with
these new classes. The really tricky part seems to be to get the
mapper rules to work properly.

Another would be to load the source file for the model declarations
into a string, use string operations to remove all schema information
and eval the result. Maybe this could work but it's not really
tempting.

I would be very thankful for any help on this matter.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to