From 500787fbd60bc09287c14f0c06fc8f1eb70d96fb Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 5 Jul 2016 16:35:03 -0600 Subject: [PATCH] Properly get latest update for ofxaccount I had done it lazily before and was getting erroneous results when querying as soon as I had more than one OFXUpdate for an account. Now I properly do a subselect. Come to think of it, I could also use a limit, I think... --- vanth/platform/ofxaccount.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/vanth/platform/ofxaccount.py b/vanth/platform/ofxaccount.py index cefac0d..145c4b4 100644 --- a/vanth/platform/ofxaccount.py +++ b/vanth/platform/ofxaccount.py @@ -1,13 +1,22 @@ +import logging import uuid import chryso.connection import sqlalchemy +import sqlalchemy.sql.functions import vanth.platform.ofxsource import vanth.tables +LOGGER = logging.getLogger(__name__) def _select(): + subselect = sqlalchemy.select([ + vanth.tables.OFXUpdate.c.ofxaccount, + sqlalchemy.sql.functions.max(vanth.tables.OFXUpdate.c.created), + ]).group_by( + vanth.tables.OFXUpdate.c.ofxaccount + ).alias('ofxupdates') return sqlalchemy.select([ vanth.tables.OFXAccount.c.account_id, vanth.tables.OFXAccount.c.name, @@ -18,11 +27,11 @@ def _select(): vanth.tables.OFXAccount.c.uuid, vanth.tables.OFXSource.c.name.label('source.name'), vanth.tables.OFXSource.c.uuid.label('source.uuid'), - vanth.tables.OFXUpdate.c.created, - ]).where( - vanth.tables.OFXAccount.c.source == vanth.tables.OFXSource.c.uuid + subselect, + ]).select_from( + subselect ).where( - vanth.tables.OFXAccount.c.uuid == vanth.tables.OFXUpdate.c.ofxaccount + vanth.tables.OFXAccount.c.uuid == subselect.c.ofxaccount ) def _execute_and_convert(query): @@ -31,7 +40,7 @@ def _execute_and_convert(query): return [{ 'account_id' : result[vanth.tables.OFXAccount.c.account_id], 'name' : result[vanth.tables.OFXAccount.c.name], - 'last_updated' : result[vanth.tables.OFXUpdate.c.created], + 'last_updated' : result['max_1'], 'password' : result[vanth.tables.OFXAccount.c.password], 'source' : { 'name' : result[vanth.tables.OFXSource.c.name.label('source.name')],