diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-03-21 17:35:26 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-03-21 17:45:51 -0400 |
commit | 2a1afd32b82e103961f305b478d4cc6f6244cd00 (patch) | |
tree | f12cc7ae7622535e3107ad1fba88c48367c7e1f2 /examples/sharding/separate_databases.py | |
parent | 512807f02d7aa6c4074910f1d0fba2187f50ee8f (diff) | |
download | sqlalchemy-2a1afd32b82e103961f305b478d4cc6f6244cd00.tar.gz |
note that horizontal sharding supports multi schema translates
the horizontal sharding API needs some work as it is
still exposing some legacy details, but in any case illustrate
how we can, for the moment, to use multiple schema translate
maps in a single session.
A lot more cleanup is needed in horizontal sharding, see #7837
Change-Id: Ia925e2226ecee9d747a8c4fc1772917f10bc505f
References: #7832
References: #7837
Diffstat (limited to 'examples/sharding/separate_databases.py')
-rw-r--r-- | examples/sharding/separate_databases.py | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/examples/sharding/separate_databases.py b/examples/sharding/separate_databases.py index 95f12fa72..accbfd79b 100644 --- a/examples/sharding/separate_databases.py +++ b/examples/sharding/separate_databases.py @@ -58,7 +58,7 @@ def id_generator(ctx): # in reality, might want to use a separate transaction for this. with db1.connect() as conn: nextid = conn.scalar(ids.select().with_for_update()) - conn.execute(ids.update(values={ids.c.nextid: ids.c.nextid + 1})) + conn.execute(ids.update().values({ids.c.nextid: ids.c.nextid + 1})) return nextid @@ -106,7 +106,7 @@ for db in (db1, db2, db3, db4): # establish initial "id" in db1 with db1.begin() as conn: - conn.execute(ids.insert(), nextid=1) + conn.execute(ids.insert(), {"nextid": 1}) # step 5. define sharding functions. @@ -155,19 +155,19 @@ def id_chooser(query, ident): return ["north_america", "asia", "europe", "south_america"] -def query_chooser(query): - """query chooser. +def execute_chooser(context): + """statement execution chooser. - this also returns a list of shard ids, which can - just be all of them. but here we'll search into the Query in order - to try to narrow down the list of shards to query. + this also returns a list of shard ids, which can just be all of them. but + here we'll search into the execution context in order to try to narrow down + the list of shards to SELECT. """ ids = [] # we'll grab continent names as we find them # and convert to shard ids - for column, operator, value in _get_query_comparisons(query): + for column, operator, value in _get_select_comparisons(context.statement): # "shares_lineage()" returns True if both columns refer to the same # statement column, adjusting for any annotations present. # (an annotation is an internal clone of a Column object @@ -186,8 +186,8 @@ def query_chooser(query): return ids -def _get_query_comparisons(query): - """Search an orm.Query object for binary expressions. +def _get_select_comparisons(statement): + """Search a Select or Query object for binary expressions. Returns expressions which match a Column against one or more literal values as a list of tuples of the form @@ -222,9 +222,9 @@ def _get_query_comparisons(query): # here we will traverse through the query's criterion, searching # for SQL constructs. We will place simple column comparisons # into a list. - if query.whereclause is not None: + if statement.whereclause is not None: visitors.traverse( - query.whereclause, + statement.whereclause, {}, { "bindparam": visit_bindparam, @@ -239,7 +239,7 @@ def _get_query_comparisons(query): Session.configure( shard_chooser=shard_chooser, id_chooser=id_chooser, - query_chooser=query_chooser, + execute_chooser=execute_chooser, ) # save and load objects! |