Repository navigation
Closed
Labels
Milestone
Description
- add bind_arguments to session.get()
- propagate execution_options, bind_arguments get() arguments to _get_impl(), then to _identity_lookup, it's very strange that bind_arguments aren't passed here
- add new horiziontal sharding hook identity_chooser(), pass to this chooser: mapper, exec_options, bind_arguments, primary key, identity_token, lazy_loaded_from, **kw
- deprecate id_chooser the same way query_chooser is deprecated
- expose "identity_token" as a setter/getter on ORMExecuteState, to better help with sharding types of use cases that dont necessarily use horizontal sharding (simplify HS API to use this attribute also?) see example below
- add execution_options to Session constructor, to set up default execution args
update sharding examples to use above new APIs
example of identity_token API
import sqlalchemy as sa
from sqlalchemy import event
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyTable(Base):
__tablename__ = "example"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Text)
engine: sa.engine.Engine = sa.create_engine(
"postgresql://scott:tiger@localhost/test",
echo="debug",
)
with engine.begin() as conn:
Base.metadata.drop_all(
conn.execution_options(schema_translate_map={None: "test_schema"})
)
Base.metadata.drop_all(
conn.execution_options(schema_translate_map={None: "test_schema_2"})
)
Base.metadata.create_all(
conn.execution_options(schema_translate_map={None: "test_schema"})
)
Base.metadata.create_all(
conn.execution_options(schema_translate_map={None: "test_schema_2"})
)
map1 = {None: "test_schema"}
map2 = {None: "test_schema_2"}
with Session(engine.execution_options(schema_translate_map=map1)) as sess:
sess.add(MyTable(name="this is schema one"))
sess.commit()
with Session(engine.execution_options(schema_translate_map=map2)) as sess:
sess.add(MyTable(name="this is schema two"))
sess.commit()
with Session(engine) as sess:
@event.listens_for(sess, "do_orm_execute")
def do_orm_execute(context):
desired_schema = context.execution_options.get("desired_schema", None)
if desired_schema:
context.update_execution_options(
schema_translate_map={None: desired_schema},
# this part is ugly, so ORMExecuteState may be added
# an identity_token attribute that you can set here
_sa_orm_load_options=context.load_options
+ {"_refresh_identity_token": desired_schema},
)
base_qry = sess.query(MyTable).filter(MyTable.id == 1)
obj1 = base_qry.execution_options(desired_schema="test_schema").one()
assert obj1.name == "this is schema one"
obj2 = base_qry.execution_options(desired_schema="test_schema_2").one()
assert obj2.name == "this is schema two"
assert obj1 is not obj2
Activity
note that horizontal sharding supports multi schema translates
note that horizontal sharding supports multi schema translates
sqla-tester commentedon Dec 18, 2022
Mike Bayer has proposed a fix for this issue in the main branch:
re-consume execute options in orm_setup_cursor_result? https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4313
zzzeek commentedon Dec 20, 2022
so I have this in progress but now I'm not sure why.
why ? how is this useful if they aren't also setting "connection callable" that works in the flush process? this issue has no link to whatever discussion it came from.
zzzeek commentedon Dec 20, 2022
OK the use case is #7832. it's read-only, or at least has to be documented as such. we can put that in the example
zzzeek commentedon Dec 21, 2022
by definition, when we call identity_chooser(), we dont have a non-None identity token. that's literally the thing we are trying to determine! So remove this from the new hook
zzzeek commentedon Dec 22, 2022
going to leave this part out. still not ready to do this. "autoflush" being in two places for example seems inconsistent.
reorganize pre_session_exec around do_orm_execute