<s> import os <EOL> import re <EOL> from django . conf import settings <EOL> from django . core . management . base import CommandError <EOL> from django . db import models <EOL> from django . db . models import get_models <EOL> def sql_create ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> if connection . settings_dict [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> raise CommandError ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> app_models = models . get_models ( app , include_auto_created = True ) <EOL> final_output = [ ] <EOL> tables = connection . introspection . table_names ( ) <EOL> known_models = set ( [ model for model in connection . introspection . installed_models ( tables ) if model not in app_models ] ) <EOL> pending_references = { } <EOL> for model in app_models : <EOL> output , references = connection . creation . sql_create_model ( model , style , known_models ) <EOL> final_output . extend ( output ) <EOL> for refto , refs in references . items ( ) : <EOL> pending_references . setdefault ( refto , [ ] ) . extend ( refs ) <EOL> if refto in known_models : <EOL> final_output . extend ( connection . creation . sql_for_pending_references ( refto , style , pending_references ) ) <EOL> final_output . extend ( connection . creation . sql_for_pending_references ( model , style , pending_references ) ) <EOL> known_models . add ( model ) <EOL> not_installed_models = set ( pending_references . keys ( ) ) <EOL> if not_installed_models : <EOL> alter_sql = [ ] <EOL> for model in not_installed_models : <EOL> alter_sql . extend ( [ '<STR_LIT>' + sql for sql in <EOL> connection . creation . sql_for_pending_references ( model , style , pending_references ) ] ) <EOL> if alter_sql : <EOL> final_output . append ( '<STR_LIT>' ) <EOL> final_output . extend ( alter_sql ) <EOL> return final_output <EOL> def sql_delete ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> cursor = connection . cursor ( ) <EOL> except : <EOL> cursor = None <EOL> if cursor : <EOL> table_names = connection . introspection . get_table_list ( cursor ) <EOL> else : <EOL> table_names = [ ] <EOL> output = [ ] <EOL> to_delete = set ( ) <EOL> references_to_delete = { } <EOL> app_models = models . get_models ( app , include_auto_created = True ) <EOL> for model in app_models : <EOL> if cursor and connection . introspection . table_name_converter ( model . _meta . db_table ) in table_names : <EOL> opts = model . _meta <EOL> for f in opts . local_fields : <EOL> if f . rel and f . rel . to not in to_delete : <EOL> references_to_delete . setdefault ( f . rel . to , [ ] ) . append ( ( model , f ) ) <EOL> to_delete . add ( model ) <EOL> for model in app_models : <EOL> if connection . introspection . table_name_converter ( model . _meta . db_table ) in table_names : <EOL> output . extend ( connection . creation . sql_destroy_model ( model , references_to_delete , style ) ) <EOL> if cursor : <EOL> cursor . close ( ) <EOL> connection . close ( ) <EOL> return output [ : : - <NUM_LIT:1> ] <EOL> def sql_reset ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> import warnings <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> DeprecationWarning <EOL> ) <EOL> return sql_delete ( app , style , connection ) + sql_all ( app , style , connection ) <EOL> def sql_flush ( style , connection , only_django = False ) : <EOL> """<STR_LIT>""" <EOL> if only_django : <EOL> tables = connection . introspection . django_table_names ( only_existing = True ) <EOL> else : <EOL> tables = connection . introspection . table_names ( ) <EOL> statements = connection . ops . sql_flush ( <EOL> style , tables , connection . introspection . sequence_list ( ) <EOL> ) <EOL> return statements <EOL> def sql_custom ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> output = [ ] <EOL> app_models = get_models ( app ) <EOL> for model in app_models : <EOL> output . extend ( custom_sql_for_model ( model , style , connection ) ) <EOL> return output <EOL> def sql_indexes ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> output = [ ] <EOL> for model in models . get_models ( app ) : <EOL> output . extend ( connection . creation . sql_indexes_for_model ( model , style ) ) <EOL> return output <EOL> def sql_all ( app , style , connection ) : <EOL> "<STR_LIT>" <EOL> return sql_create ( app , style , connection ) + sql_custom ( app , style , connection ) + sql_indexes ( app , style , connection ) <EOL> def custom_sql_for_model ( model , style , connection ) : <EOL> opts = model . _meta <EOL> app_dir = os . path . normpath ( os . path . join ( os . path . dirname ( models . get_app ( model . _meta . app_label ) . __file__ ) , '<STR_LIT>' ) ) <EOL> output = [ ] <EOL> if opts . managed : <EOL> post_sql_fields = [ f for f in opts . local_fields if hasattr ( f , '<STR_LIT>' ) ] <EOL> for f in post_sql_fields : <EOL> output . extend ( f . post_create_sql ( style , model . _meta . db_table ) ) <EOL> statements = re . compile ( r"<STR_LIT>" , re . M ) <EOL> backend_name = connection . settings_dict [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> sql_files = [ os . path . join ( app_dir , "<STR_LIT>" % ( opts . object_name . lower ( ) , backend_name ) ) , <EOL> os . path . join ( app_dir , "<STR_LIT>" % opts . object_name . lower ( ) ) ] <EOL> for sql_file in sql_files : <EOL> if os . path . exists ( sql_file ) : <EOL> fp = open ( sql_file , '<STR_LIT>' ) <EOL> for statement in statements . split ( fp . read ( ) . decode ( settings . FILE_CHARSET ) ) : <EOL> statement = re . sub ( ur "<STR_LIT>" , "<STR_LIT>" , statement ) <EOL> if statement . strip ( ) : <EOL> output . append ( statement + u"<STR_LIT:;>" ) <EOL> fp . close ( ) <EOL> return output <EOL> def emit_post_sync_signal ( created_models , verbosity , interactive , db ) : <EOL> for app in models . get_apps ( ) : <EOL> app_name = app . __name__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:2> ] <EOL> if verbosity >= <NUM_LIT:2> : <EOL> print "<STR_LIT>" , app_name <EOL> models . signals . post_syncdb . send ( sender = app , app = app , <EOL> created_models = created_models , verbosity = verbosity , <EOL> interactive = interactive , db = db ) </s>
