<s> """<STR_LIT>""" <EOL> revision = '<STR_LIT>' <EOL> down_revision = '<STR_LIT>' <EOL> import logging <EOL> import sys <EOL> import time <EOL> import sqlalchemy as sa <EOL> from alembic import op <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . addHandler ( logging . StreamHandler ( sys . stdout ) ) <EOL> log . setLevel ( logging . INFO ) <EOL> def upgrade ( ) : <EOL> from depot . manager import DepotManager <EOL> from depot . fields . upload import UploadedFile <EOL> from sqlalchemy import bindparam , Unicode , Column <EOL> from kotti import DBSession , metadata <EOL> files = sa . Table ( '<STR_LIT>' , metadata ) <EOL> files . c . data . type = sa . LargeBinary ( ) <EOL> dn = DepotManager . get_default ( ) <EOL> _saved = [ ] <EOL> def process ( thing ) : <EOL> id , data , filename , mimetype = thing <EOL> uploaded_file = UploadedFile ( { '<STR_LIT>' : dn , '<STR_LIT>' : [ ] } ) <EOL> uploaded_file . _thaw ( ) <EOL> uploaded_file . process_content ( <EOL> data , filename = filename , content_type = mimetype ) <EOL> _saved . append ( { '<STR_LIT>' : id , '<STR_LIT:data>' : uploaded_file . encode ( ) } ) <EOL> log . info ( "<STR_LIT>" . format ( id ) ) <EOL> query = DBSession . query ( <EOL> files . c . id , files . c . data , files . c . filename , files . c . mimetype <EOL> ) . order_by ( files . c . id ) . yield_per ( <NUM_LIT:10> ) <EOL> window_size = <NUM_LIT:10> <EOL> window_idx = <NUM_LIT:0> <EOL> log . info ( "<STR_LIT>" ) <EOL> now = time . time ( ) <EOL> while True : <EOL> start , stop = window_size * window_idx , window_size * ( window_idx + <NUM_LIT:1> ) <EOL> things = query . slice ( start , stop ) . all ( ) <EOL> if things is None : <EOL> break <EOL> for thing in things : <EOL> process ( thing ) <EOL> if len ( things ) < window_size : <EOL> break <EOL> window_idx += <NUM_LIT:1> <EOL> log . info ( "<STR_LIT>" ) <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT:data>' ) <EOL> op . add_column ( '<STR_LIT>' , Column ( '<STR_LIT:data>' , Unicode ( <NUM_LIT> ) ) ) <EOL> files . c . data . type = Unicode ( <NUM_LIT> ) <EOL> update = files . update ( ) . where ( files . c . id == bindparam ( '<STR_LIT>' ) ) . values ( { files . c . data : bindparam ( '<STR_LIT:data>' ) } ) <EOL> def chunks ( l , n ) : <EOL> for i in range ( <NUM_LIT:0> , len ( l ) , n ) : <EOL> yield l [ i : i + n ] <EOL> for cdata in chunks ( _saved , <NUM_LIT:10> ) : <EOL> DBSession . execute ( update , cdata ) <EOL> log . info ( "<STR_LIT>" . format ( <EOL> int ( time . time ( ) - now ) ) ) <EOL> def downgrade ( ) : <EOL> pass </s>
