MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / setup_session

Method setup_session

test/ext/test_horizontal_shard.py:710–767  ·  view source on GitHub ↗
(cls)

Source from the content-addressed store, hash-verified

708class LegacyAPIShardTest(DistinctEngineShardTest):
709 @classmethod
710 def setup_session(cls):
711 global sharded_session
712 shard_lookup = {
713 "North America": "north_america",
714 "Asia": "asia",
715 "Europe": "europe",
716 "South America": "south_america",
717 }
718
719 def shard_chooser(mapper, instance, clause=None):
720 if isinstance(instance, WeatherLocation):
721 return shard_lookup[instance.continent]
722 else:
723 return shard_chooser(mapper, instance.location)
724
725 def id_chooser(query, primary_key):
726 return ["north_america", "asia", "europe", "south_america"]
727
728 def query_chooser(query):
729 ids = []
730
731 class FindContinent(sql.ClauseVisitor):
732 def visit_binary(self, binary):
733 if binary.left.shares_lineage(
734 weather_locations.c.continent
735 ):
736 if binary.operator == operators.eq:
737 ids.append(shard_lookup[binary.right.value])
738 elif binary.operator == operators.in_op:
739 for value in binary.right.value:
740 ids.append(shard_lookup[value])
741
742 if isinstance(query, Select) and query.whereclause is not None:
743 FindContinent().traverse(query.whereclause)
744 if len(ids) == 0:
745 return ["north_america", "asia", "europe", "south_america"]
746 else:
747 return ids
748
749 sm = sessionmaker(class_=ShardedSession, autoflush=True)
750 sm.configure(
751 shards={
752 "north_america": db1,
753 "asia": db2,
754 "europe": db3,
755 "south_america": db4,
756 },
757 shard_chooser=shard_chooser,
758 id_chooser=id_chooser,
759 query_chooser=query_chooser,
760 )
761
762 def sharded_session():
763 with expect_deprecated(
764 "The ``id_chooser`` parameter is deprecated",
765 "The ``query_chooser`` parameter is deprecated",
766 ):
767 return sm()

Callers

nothing calls this directly

Calls 2

sessionmakerClass · 0.90
configureMethod · 0.45

Tested by

no test coverage detected