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...
This commit is contained in:
Eli Ribble 2016-07-05 16:35:03 -06:00
parent c26dba6d2e
commit 500787fbd6
1 changed files with 14 additions and 5 deletions

View File

@ -1,13 +1,22 @@
import logging
import uuid import uuid
import chryso.connection import chryso.connection
import sqlalchemy import sqlalchemy
import sqlalchemy.sql.functions
import vanth.platform.ofxsource import vanth.platform.ofxsource
import vanth.tables import vanth.tables
LOGGER = logging.getLogger(__name__)
def _select(): 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([ return sqlalchemy.select([
vanth.tables.OFXAccount.c.account_id, vanth.tables.OFXAccount.c.account_id,
vanth.tables.OFXAccount.c.name, vanth.tables.OFXAccount.c.name,
@ -18,11 +27,11 @@ def _select():
vanth.tables.OFXAccount.c.uuid, vanth.tables.OFXAccount.c.uuid,
vanth.tables.OFXSource.c.name.label('source.name'), vanth.tables.OFXSource.c.name.label('source.name'),
vanth.tables.OFXSource.c.uuid.label('source.uuid'), vanth.tables.OFXSource.c.uuid.label('source.uuid'),
vanth.tables.OFXUpdate.c.created, subselect,
]).where( ]).select_from(
vanth.tables.OFXAccount.c.source == vanth.tables.OFXSource.c.uuid subselect
).where( ).where(
vanth.tables.OFXAccount.c.uuid == vanth.tables.OFXUpdate.c.ofxaccount vanth.tables.OFXAccount.c.uuid == subselect.c.ofxaccount
) )
def _execute_and_convert(query): def _execute_and_convert(query):
@ -31,7 +40,7 @@ def _execute_and_convert(query):
return [{ return [{
'account_id' : result[vanth.tables.OFXAccount.c.account_id], 'account_id' : result[vanth.tables.OFXAccount.c.account_id],
'name' : result[vanth.tables.OFXAccount.c.name], '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], 'password' : result[vanth.tables.OFXAccount.c.password],
'source' : { 'source' : {
'name' : result[vanth.tables.OFXSource.c.name.label('source.name')], 'name' : result[vanth.tables.OFXSource.c.name.label('source.name')],