<s> from optparse import make_option <EOL> from django . core . cache . backends . db import BaseDatabaseCache <EOL> from django . core . management . base import LabelCommand <EOL> from django . db import connections , router , transaction , models , DEFAULT_DB_ALIAS <EOL> from django . db . utils import DatabaseError <EOL> class Command ( LabelCommand ) : <EOL> help = "<STR_LIT>" <EOL> args = "<STR_LIT>" <EOL> label = '<STR_LIT>' <EOL> option_list = LabelCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = DEFAULT_DB_ALIAS , help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> requires_model_validation = False <EOL> def handle_label ( self , tablename , ** options ) : <EOL> db = options . get ( '<STR_LIT>' ) <EOL> cache = BaseDatabaseCache ( tablename , { } ) <EOL> if not router . allow_syncdb ( db , cache . cache_model_class ) : <EOL> return <EOL> connection = connections [ db ] <EOL> fields = ( <EOL> models . CharField ( name = '<STR_LIT>' , max_length = <NUM_LIT:255> , unique = True , primary_key = True ) , <EOL> models . TextField ( name = '<STR_LIT:value>' ) , <EOL> models . DateTimeField ( name = '<STR_LIT>' , db_index = True ) , <EOL> ) <EOL> table_output = [ ] <EOL> index_output = [ ] <EOL> qn = connection . ops . quote_name <EOL> for f in fields : <EOL> field_output = [ qn ( f . name ) , f . db_type ( connection = connection ) ] <EOL> field_output . append ( "<STR_LIT>" % ( not f . null and "<STR_LIT>" or "<STR_LIT>" ) ) <EOL> if f . primary_key : <EOL> field_output . append ( "<STR_LIT>" ) <EOL> elif f . unique : <EOL> field_output . append ( "<STR_LIT>" ) <EOL> if f . db_index : <EOL> unique = f . unique and "<STR_LIT>" or "<STR_LIT>" <EOL> index_output . append ( "<STR_LIT>" % ( unique , qn ( '<STR_LIT>' % ( tablename , f . name ) ) , qn ( tablename ) , <EOL> qn ( f . name ) ) ) <EOL> table_output . append ( "<STR_LIT:U+0020>" . join ( field_output ) ) <EOL> full_statement = [ "<STR_LIT>" % qn ( tablename ) ] <EOL> for i , line in enumerate ( table_output ) : <EOL> full_statement . append ( '<STR_LIT>' % ( line , i < len ( table_output ) - <NUM_LIT:1> and '<STR_LIT:U+002C>' or '<STR_LIT>' ) ) <EOL> full_statement . append ( '<STR_LIT>' ) <EOL> curs = connection . cursor ( ) <EOL> try : <EOL> curs . execute ( "<STR_LIT:\n>" . join ( full_statement ) ) <EOL> except DatabaseError , e : <EOL> self . stderr . write ( <EOL> self . style . ERROR ( "<STR_LIT>" % <EOL> ( tablename , e ) ) ) <EOL> transaction . rollback_unless_managed ( using = db ) <EOL> else : <EOL> for statement in index_output : <EOL> curs . execute ( statement ) <EOL> transaction . commit_unless_managed ( using = db ) </s>
