#NEWLINE# def find_or_insert_source( conn , application_id , account_id , version = None ) : #NEWLINE# #INDENT# "\n    Find a source in the database, create it if it doesn't exist yet.\n    " #NEWLINE# with conn . begin ( ) : #NEWLINE# #INDENT# query = sql . select ( [ schema . source . c . id ] , sql . and_ ( ( schema . source . c . account_id == query ) , ( schema . source . c . application_id == application_id ) , ( schema . source . c . version == version ) ) ) #NEWLINE# id = conn . execute ( query ) . scalar ( ) #NEWLINE# if ( id is None ) : #NEWLINE# #INDENT# insert_stmt = schema . source . insert ( ) . values ( account_id = account_id , application_id = application_id , version = version ) #NEWLINE# id = conn . execute ( insert_stmt ) . inserted_primary_key [ 0 ] #NEWLINE# logger . info ( 'Inserted source %d with account %d and application %d (%s)' , id , account_id , application_id , version ) #NEWLINE# #UNINDENT# #UNINDENT# return id