]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add example usage for shard_id in context feature [ticket:2031]
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Jan 2011 22:08:57 +0000 (17:08 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Jan 2011 22:08:57 +0000 (17:08 -0500)
examples/sharding/attribute_shard.py

index 9f1157c32b3cbfe07595930587833f77f840d7d8..b36b8a01db5dd4377698bf975707410b949889c1 100644 (file)
@@ -1,7 +1,7 @@
 
 # step 1. imports
 from sqlalchemy import (create_engine, MetaData, Table, Column, Integer,
-    String, ForeignKey, Float, DateTime)
+    String, ForeignKey, Float, DateTime, event)
 from sqlalchemy.orm import sessionmaker, mapper, relationship
 from sqlalchemy.ext.horizontal_shard import ShardedSession
 from sqlalchemy.sql import operators, visitors
@@ -233,6 +233,15 @@ mapper(WeatherLocation, weather_locations, properties={
 
 mapper(Report, weather_reports)
 
+# step 8 (optional), events.  The "shard_id" is placed
+# in the QueryContext where it can be intercepted and associated
+# with objects, if needed.
+
+def add_shard_id(instance, ctx):
+    instance.shard_id = ctx.attributes["shard_id"]
+
+event.listen(WeatherLocation, "load", add_shard_id)
+event.listen(Report, "load", add_shard_id)
 
 # save and load objects!
 
@@ -253,7 +262,11 @@ for c in [tokyo, newyork, toronto, london, dublin, brasilia, quito]:
     sess.add(c)
 sess.commit()
 
-t = sess.query(WeatherLocation).get(tokyo.id)
+tokyo_id = tokyo.id
+
+sess.close()
+
+t = sess.query(WeatherLocation).get(tokyo_id)
 assert t.city == tokyo.city
 assert t.reports[0].temperature == 80.0