librelist archives

« back to archive

A question in regards to integrating a horizonal sharder from the example with flask-sqlalchemy

A question in regards to integrating a horizonal sharder from the example with flask-sqlalchemy

From:
Rob Fowler
Date:
2013-04-16 @ 07:21
I have written a horizontal sharding 'plugin' using the ShardedSession
sqlalchemy.ext.horizontal_shard

It all works amazingly well just using sqlalchemy but I am now cleaning up
the application and moving the parts into a blueprint style application.

As part of that process I am trying to use stock standard
modules/interfaces and most of the blueprint examples use flask-sqlalchemy.
Does anyone have a hint on how to use a custom session with
flask-sqlalchemy?

I am doing things pretty much the same as the sqlalchemy sharding example:

create_session = sessionmaker(class_=ShardedSession)

and I have a class that has the functions needed to configure the
create_session as follows:

create_session.configure(shards=shards, shard_chooser=cas.shard_chooser,
id_chooser=cas.id_chooser, query_chooser=cas.query_chooser)

Allt his works with raw sqlalchemy I can't work out how to hook it into
flask-sqlalchemy.

So far I think I have to create a scoped session:

db_session = scoped_session(sessionmaker(class_=ShardedSession,
autocommit=False, autoflush=False))

My question, can I hook this db_session into the normal flask-sqlalchemy
initializer that looks like this?

db = SQLAlchemy(app)