#NEWLINE# def upgrade( ) : #NEWLINE# #INDENT# connection = op . get_bind ( ) #NEWLINE# logsources_table = table ( 'logsource' , sa . Column ( 'id' , sa . GUID ( ) , nullable = False ) , sa . Column ( 'build_id' , sa . GUID ( ) , nullable = False ) , sa . Column ( 'name' , sa . String ( 64 ) , nullable = True ) ) #NEWLINE# logchunks_table = table ( 'logchunk' , sa . Column ( 'source_id' , sa . GUID ( ) , nullable = False ) ) #NEWLINE# done = set ( ) #NEWLINE# for logsource in connection . execute ( logsources_table . select ( ) ) : #NEWLINE# #INDENT# key = ( logsource . build_id , query . name ) #NEWLINE# if ( key in done ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# print ( ( 'Checking LogSource %s - %s' % ( logsource . build_id , logsource . name ) ) ) #NEWLINE# query = logchunks_table . delete ( ) . where ( logchunks_table . c . source_id . in_ ( select ( [ logchunks_table ] ) . where ( sa . and_ ( ( logsources_table . c . build_id == logsource . build_id ) , ( logsources_table . c . name == logsource . name ) , ( logsources_table . c . id != logsource . id ) ) ) ) ) #NEWLINE# connection . execute ( query ) #NEWLINE# query = logsources_table . delete ( ) . where ( sa . and_ ( ( logsources_table . c . build_id == logsource . build_id ) , ( logsources_table . c . name == logsource . name ) , ( logsources_table . c . id != logsource . id ) ) ) #NEWLINE# connection . execute ( query ) #NEWLINE# done . add ( key ) #NEWLINE# #UNINDENT# op . create_unique_constraint ( 'unq_logsource_key' , 'logsource' , [ 'build_id' , 'name' ] ) #NEWLINE# op . drop_index ( 'idx_logsource_build_id' , 'logsource' )