#NEWLINE# def downgrade( engine , desired_version ) : #NEWLINE# #INDENT# 'Downgrades the assets db at the given engine to the desired version.\n\n    Parameters\n    ----------\n    engine : Engine\n        An SQLAlchemy engine to the assets database.\n    desired_version : int\n        The desired resulting version for the assets database.\n    ' #NEWLINE# conn = engine . connect ( ) #NEWLINE# metadata = sa . MetaData ( conn ) #NEWLINE# metadata . reflect ( bind = engine ) #NEWLINE# version_info_table = metadata . tables [ 'version_info' ] #NEWLINE# starting_version = sa . select ( ( version_info_table . c . version , ) ) . scalar ( ) #NEWLINE# if ( starting_version < desired_version ) : #NEWLINE# #INDENT# raise AssetDBImpossibleDowngrade ( db_version = starting_version , desired_version = desired_version ) #NEWLINE# #UNINDENT# if ( starting_version == desired_version ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# ctx = MigrationContext . configure ( conn ) #NEWLINE# op = Operations ( ctx ) #NEWLINE# downgrade_keys = range ( desired_version , starting_version ) [ : : ( - 1 ) ] #NEWLINE# _pragma_foreign_keys ( conn , False ) #NEWLINE# for downgrade_key in downgrade_keys : #NEWLINE# #INDENT# _downgrade_methods [ downgrade_key ] ( op , version_info_table ) #NEWLINE# #UNINDENT# _pragma_foreign_keys ( conn , True )