Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemy warnings #214

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 11 additions & 29 deletions inbox/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from inbox.models.session import session_scope_by_shard_id


def contact_subquery(db_session, namespace_id, email_address, field):
def contact_query(db_session, namespace_id, email_address, field):
return (
db_session.query(Message.thread_id)
.join(MessageContactAssociation)
Expand All @@ -32,7 +32,6 @@ def contact_subquery(db_session, namespace_id, email_address, field):
Contact.namespace_id == namespace_id,
MessageContactAssociation.field == field,
)
.subquery()
)


Expand Down Expand Up @@ -89,19 +88,19 @@ def threads(
query = query.filter(*filters)

if from_addr is not None:
from_query = contact_subquery(db_session, namespace_id, from_addr, "from_addr")
from_query = contact_query(db_session, namespace_id, from_addr, "from_addr")
query = query.filter(Thread.id.in_(from_query))

if to_addr is not None:
to_query = contact_subquery(db_session, namespace_id, to_addr, "to_addr")
to_query = contact_query(db_session, namespace_id, to_addr, "to_addr")
query = query.filter(Thread.id.in_(to_query))

if cc_addr is not None:
cc_query = contact_subquery(db_session, namespace_id, cc_addr, "cc_addr")
cc_query = contact_query(db_session, namespace_id, cc_addr, "cc_addr")
query = query.filter(Thread.id.in_(cc_query))

if bcc_addr is not None:
bcc_query = contact_subquery(db_session, namespace_id, bcc_addr, "bcc_addr")
bcc_query = contact_query(db_session, namespace_id, bcc_addr, "bcc_addr")
query = query.filter(Thread.id.in_(bcc_query))

if any_email is not None:
Expand All @@ -113,7 +112,6 @@ def threads(
Contact.email_address.in_(any_email),
Contact.namespace_id == namespace_id,
)
.subquery()
)
query = query.filter(Thread.id.in_(any_contact_query))

Expand All @@ -129,7 +127,6 @@ def threads(
.join(Part)
.join(Block)
.filter(Block.filename == filename, Block.namespace_id == namespace_id)
.subquery()
)
query = query.filter(Thread.id.in_(files_query))

Expand All @@ -146,24 +143,19 @@ def threads(
.join(Message.messagecategories)
.join(MessageCategory.category)
.filter(Category.namespace_id == namespace_id, or_(*category_filters))
.subquery()
)
query = query.filter(Thread.id.in_(category_query))

if unread is not None:
read = not unread
unread_query = (
db_session.query(Message.thread_id)
.filter(Message.namespace_id == namespace_id, Message.is_read == read)
.subquery()
unread_query = db_session.query(Message.thread_id).filter(
Message.namespace_id == namespace_id, Message.is_read == read
)
query = query.filter(Thread.id.in_(unread_query))

if starred is not None:
starred_query = (
db_session.query(Message.thread_id)
.filter(Message.namespace_id == namespace_id, Message.is_starred == starred)
.subquery()
starred_query = db_session.query(Message.thread_id).filter(
Message.namespace_id == namespace_id, Message.is_starred == starred
)
query = query.filter(Thread.id.in_(starred_query))

Expand Down Expand Up @@ -344,7 +336,6 @@ def messages_or_drafts(
Contact.email_address == to_addr,
Contact.namespace_id == bindparam("namespace_id"),
)
.subquery()
)
query = query.filter(Message.id.in_(to_query))

Expand All @@ -357,7 +348,6 @@ def messages_or_drafts(
Contact.email_address == from_addr,
Contact.namespace_id == bindparam("namespace_id"),
)
.subquery()
)
query = query.filter(Message.id.in_(from_query))

Expand All @@ -370,7 +360,6 @@ def messages_or_drafts(
Contact.email_address == cc_addr,
Contact.namespace_id == bindparam("namespace_id"),
)
.subquery()
)
query = query.filter(Message.id.in_(cc_query))

Expand All @@ -383,7 +372,6 @@ def messages_or_drafts(
Contact.email_address == bcc_addr,
Contact.namespace_id == bindparam("namespace_id"),
)
.subquery()
)
query = query.filter(Message.id.in_(bcc_query))

Expand All @@ -395,7 +383,6 @@ def messages_or_drafts(
Contact.email_address.in_(any_email),
Contact.namespace_id == bindparam("namespace_id"),
)
.subquery()
)
query = query.filter(Message.id.in_(any_email_query))

Expand Down Expand Up @@ -583,7 +570,7 @@ def recurring_events(
if ends_before:
# start < end, so event start < ends_before
before_criteria.append(RecurringEvent.start < ends_before)
recur_query = recur_query.filter(and_(*before_criteria))
recur_query = recur_query.filter(*before_criteria)
after_criteria = []
if starts_after:
after_criteria.append(
Expand All @@ -594,7 +581,7 @@ def recurring_events(
or_(RecurringEvent.until > ends_after, RecurringEvent.until.is_(None))
)

recur_query = recur_query.filter(and_(*after_criteria))
recur_query = recur_query.filter(*after_criteria)

recur_instances = []

Expand Down Expand Up @@ -694,7 +681,6 @@ def events(
Contact.namespace_id == namespace_id,
EventContactAssociation.field == "title",
)
.subquery()
)
event_criteria.append(Event.id.in_(title_email_query))

Expand All @@ -707,7 +693,6 @@ def events(
Contact.namespace_id == namespace_id,
EventContactAssociation.field == "description",
)
.subquery()
)
event_criteria.append(Event.id.in_(description_email_query))

Expand All @@ -720,7 +705,6 @@ def events(
Contact.namespace_id == namespace_id,
EventContactAssociation.field == "owner",
)
.subquery()
)
event_criteria.append(Event.id.in_(owner_email_query))

Expand All @@ -733,7 +717,6 @@ def events(
Contact.namespace_id == namespace_id,
EventContactAssociation.field == "participant",
)
.subquery()
)
event_criteria.append(Event.id.in_(participant_email_query))

Expand All @@ -744,7 +727,6 @@ def events(
.filter(
Contact.email_address == any_email, Contact.namespace_id == namespace_id
)
.subquery()
)
event_criteria.append(Event.id.in_(any_email_query))

Expand Down
4 changes: 2 additions & 2 deletions inbox/models/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def id_chooser(query, ident):
return [str(engine_manager.shard_key_for_id(ident))]


def query_chooser(query):
def execute_chooser(query):
return [str(k) for k in engine_manager.engines]


Expand All @@ -221,7 +221,7 @@ def global_session_scope():
session = ShardedSession(
shard_chooser=shard_chooser,
id_chooser=id_chooser,
query_chooser=query_chooser,
execute_chooser=execute_chooser,
shards=shards,
)
# STOPSHIP(emfree): need instrumentation and proper exception handling
Expand Down
2 changes: 1 addition & 1 deletion inbox/transactions/delta_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_transaction_cursor_near_timestamp(namespace_id, timestamp, db_session):
.order_by(desc(Transaction.created_at))
.filter(Transaction.created_at < dt, Transaction.namespace_id == namespace_id)
.limit(1)
.subquery()
.scalar_subquery()
)
latest_transaction = (
db_session.query(Transaction)
Expand Down