Well, you'll have to find some way to pass the engine
variable to your custom_db_api
module. This might be marginally cleaner...
Base = declarative_base()
class Something(Base):
pass
def initialize(engine):
Something.__table__ = Table('something', Base.metadata, autoload_with=engine)
...or if you can infer the correct engine initialization parameter from some 'global', like sys.argv
, you could use something like this...
import sys
Base = declarative_base()
if len(sys.argv) > 1 and sys.argv[1] == '--use-alt-db':
engine = create_engine('mysql://user:pass@alt_host/db_name')
else:
engine = create_engine('mysql://user:pass@main_host/db_name')
table = Table('something', Base.metadata, autoload_with=engine)
class Something(Base):
__table__ = table
It kinda depends on how you intend to tell the program which DB to use.