<s> from south . db import generic <EOL> class DatabaseOperations ( generic . DatabaseOperations ) : <EOL> """<STR_LIT>""" <EOL> backend_name = "<STR_LIT>" <EOL> supports_foreign_keys = False <EOL> has_check_constraints = False <EOL> has_booleans = False <EOL> def add_column ( self , table_name , name , field , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if ( not field . null and <EOL> ( not field . has_default ( ) or field . get_default ( ) is None ) and <EOL> not field . empty_strings_allowed ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> field . set_attributes_from_name ( name ) <EOL> field_default = None <EOL> if not getattr ( field , '<STR_LIT>' , False ) : <EOL> default = field . get_default ( ) <EOL> if default is not None : <EOL> field_default = "<STR_LIT>" % field . get_db_prep_save ( default , connection = self . _get_connection ( ) ) <EOL> field . _suppress_default = True <EOL> self . _remake_table ( table_name , added = { <EOL> field . column : ( self . _column_sql_for_create ( table_name , name , field , False ) , field_default ) <EOL> } ) <EOL> def _get_full_table_description ( self , connection , cursor , table_name ) : <EOL> cursor . execute ( '<STR_LIT>' % connection . ops . quote_name ( table_name ) ) <EOL> return [ { '<STR_LIT:name>' : field [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:type>' : field [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : not field [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : field [ <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : field [ <NUM_LIT:5> ] <EOL> } for field in cursor . fetchall ( ) ] <EOL> @ generic . invalidate_table_constraints <EOL> def _remake_table ( self , table_name , added = { } , renames = { } , deleted = [ ] , altered = { } , primary_key_override = None , uniques_deleted = [ ] ) : <EOL> """<STR_LIT>""" <EOL> if self . dry_run : <EOL> return <EOL> temp_name = "<STR_LIT>" + table_name <EOL> definitions = { } <EOL> cursor = self . _get_connection ( ) . cursor ( ) <EOL> indexes = self . _get_connection ( ) . introspection . get_indexes ( cursor , table_name ) <EOL> multi_indexes = self . _get_multi_indexes ( table_name ) <EOL> for column_info in self . _get_full_table_description ( self . _get_connection ( ) , cursor , table_name ) : <EOL> name = column_info [ '<STR_LIT:name>' ] <EOL> if name in deleted : <EOL> continue <EOL> type = column_info [ '<STR_LIT:type>' ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if ( primary_key_override and primary_key_override == name ) or ( not primary_key_override and name in indexes and <EOL> indexes [ name ] [ '<STR_LIT:primary_key>' ] ) : <EOL> type += "<STR_LIT>" <EOL> elif not column_info [ '<STR_LIT>' ] : <EOL> type += "<STR_LIT>" <EOL> if ( name in indexes and indexes [ name ] [ '<STR_LIT>' ] and <EOL> name not in uniques_deleted ) : <EOL> type += "<STR_LIT>" <EOL> if column_info [ '<STR_LIT>' ] is not None : <EOL> type += "<STR_LIT>" + column_info [ '<STR_LIT>' ] <EOL> if name in renames : <EOL> name = renames [ name ] <EOL> definitions [ name ] = type <EOL> for name , type in altered . items ( ) : <EOL> if ( primary_key_override and primary_key_override == name ) or ( not primary_key_override and name in indexes and <EOL> indexes [ name ] [ '<STR_LIT:primary_key>' ] ) : <EOL> type += "<STR_LIT>" <EOL> if ( name in indexes and indexes [ name ] [ '<STR_LIT>' ] and <EOL> name not in uniques_deleted ) : <EOL> type += "<STR_LIT>" <EOL> definitions [ name ] = type <EOL> for name , ( type , _ ) in added . items ( ) : <EOL> if ( primary_key_override and primary_key_override == name ) : <EOL> type += "<STR_LIT>" <EOL> definitions [ name ] = type <EOL> self . execute ( "<STR_LIT>" % ( <EOL> self . quote_name ( temp_name ) , <EOL> "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( self . quote_name ( cname ) , ctype ) for cname , ctype in definitions . items ( ) ] ) , <EOL> ) ) <EOL> self . _copy_data ( table_name , temp_name , renames , added ) <EOL> self . delete_table ( table_name ) <EOL> self . rename_table ( temp_name , table_name ) <EOL> self . _make_multi_indexes ( table_name , multi_indexes , renames = renames , deleted = deleted , uniques_deleted = uniques_deleted ) <EOL> def _copy_data ( self , src , dst , field_renames = { } , added = { } ) : <EOL> "<STR_LIT>" <EOL> cursor = self . _get_connection ( ) . cursor ( ) <EOL> src_fields = [ column_info [ <NUM_LIT:0> ] for column_info in self . _get_connection ( ) . introspection . get_table_description ( cursor , src ) ] <EOL> dst_fields = [ column_info [ <NUM_LIT:0> ] for column_info in self . _get_connection ( ) . introspection . get_table_description ( cursor , dst ) ] <EOL> src_fields_new = [ ] <EOL> dst_fields_new = [ ] <EOL> for field in src_fields : <EOL> if field in field_renames : <EOL> dst_fields_new . append ( self . quote_name ( field_renames [ field ] ) ) <EOL> elif field in dst_fields : <EOL> dst_fields_new . append ( self . quote_name ( field ) ) <EOL> else : <EOL> continue <EOL> src_fields_new . append ( self . quote_name ( field ) ) <EOL> for field , ( _ , default ) in added . items ( ) : <EOL> if default is not None : <EOL> field = self . quote_name ( field ) <EOL> src_fields_new . append ( "<STR_LIT>" % ( default , field ) ) <EOL> dst_fields_new . append ( field ) <EOL> self . execute ( "<STR_LIT>" % ( <EOL> self . quote_name ( dst ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( dst_fields_new ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( src_fields_new ) , <EOL> self . quote_name ( src ) , <EOL> ) ) <EOL> def _create_unique ( self , table_name , columns ) : <EOL> self . execute ( "<STR_LIT>" % ( <EOL> self . quote_name ( '<STR_LIT>' % ( table_name , '<STR_LIT>' . join ( columns ) ) ) , <EOL> self . quote_name ( table_name ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( self . quote_name ( c ) for c in columns ) , <EOL> ) ) <EOL> def _get_multi_indexes ( self , table_name ) : <EOL> indexes = [ ] <EOL> cursor = self . _get_connection ( ) . cursor ( ) <EOL> cursor . execute ( '<STR_LIT>' % self . quote_name ( table_name ) ) <EOL> for index , unique in [ ( field [ <NUM_LIT:1> ] , field [ <NUM_LIT:2> ] ) for field in cursor . fetchall ( ) ] : <EOL> if not unique : <EOL> continue <EOL> cursor . execute ( '<STR_LIT>' % self . quote_name ( index ) ) <EOL> info = cursor . fetchall ( ) <EOL> if len ( info ) == <NUM_LIT:1> : <EOL> continue <EOL> columns = [ ] <EOL> for field in info : <EOL> columns . append ( field [ <NUM_LIT:2> ] ) <EOL> indexes . append ( columns ) <EOL> return indexes <EOL> def _make_multi_indexes ( self , table_name , indexes , deleted = [ ] , renames = { } , uniques_deleted = [ ] ) : <EOL> for index in indexes : <EOL> columns = [ ] <EOL> for name in index : <EOL> if name in deleted : <EOL> columns = [ ] <EOL> break <EOL> if name in renames : <EOL> name = renames [ name ] <EOL> columns . append ( name ) <EOL> if columns and set ( columns ) != set ( uniques_deleted ) : <EOL> self . _create_unique ( table_name , columns ) <EOL> def _column_sql_for_create ( self , table_name , name , field , explicit_name = True ) : <EOL> "<STR_LIT>" <EOL> field . set_attributes_from_name ( name ) <EOL> if not explicit_name : <EOL> name = field . db_column <EOL> else : <EOL> field . column = name <EOL> sql = self . column_sql ( table_name , name , field , with_name = False , field_prepared = True ) <EOL> if sql : <EOL> sql = sql . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return sql <EOL> def alter_column ( self , table_name , name , field , explicit_name = True , ignore_constraints = False ) : <EOL> """<STR_LIT>""" <EOL> if not field . null and field . has_default ( ) : <EOL> params = { <EOL> "<STR_LIT>" : self . quote_name ( name ) , <EOL> "<STR_LIT>" : self . quote_name ( table_name ) <EOL> } <EOL> self . _update_nulls_to_default ( params , field ) <EOL> field . _suppress_default = True <EOL> self . _remake_table ( table_name , altered = { <EOL> name : self . _column_sql_for_create ( table_name , name , field , explicit_name ) , <EOL> } ) <EOL> def delete_column ( self , table_name , column_name ) : <EOL> """<STR_LIT>""" <EOL> self . _remake_table ( table_name , deleted = [ column_name ] ) <EOL> def rename_column ( self , table_name , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . _remake_table ( table_name , renames = { old : new } ) <EOL> def create_unique ( self , table_name , columns ) : <EOL> """<STR_LIT>""" <EOL> self . _create_unique ( table_name , columns ) <EOL> def delete_unique ( self , table_name , columns ) : <EOL> """<STR_LIT>""" <EOL> self . _remake_table ( table_name , uniques_deleted = columns ) <EOL> def create_primary_key ( self , table_name , columns ) : <EOL> if not isinstance ( columns , ( list , tuple ) ) : <EOL> columns = [ columns ] <EOL> assert len ( columns ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> self . _remake_table ( table_name , primary_key_override = columns [ <NUM_LIT:0> ] ) <EOL> def delete_primary_key ( self , table_name ) : <EOL> self . _remake_table ( table_name , primary_key_override = True ) <EOL> def delete_table ( self , table_name , cascade = True ) : <EOL> generic . DatabaseOperations . delete_table ( self , table_name , False ) </s>
