Ticket #2225: sql_patch.diff
File sql_patch.diff, 12.3 KB (added by , 15 years ago) |
---|
-
django/core/management/commands/sqlall.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s)." 5 5 6 6 output_transaction = True 7 7 8 def handle_app(self, app , **options):8 def handle_app(self, apps, **options): 9 9 from django.core.management.sql import sql_all 10 return u'\n'.join(sql_all(app , self.style)).encode('utf-8')10 return u'\n'.join(sql_all(apps, self.style)).encode('utf-8') -
django/core/management/commands/sqlcustom.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the custom table modifying SQL statements for the given app name(s)." 5 5 6 6 output_transaction = True 7 7 8 def handle_app(self, app , **options):8 def handle_app(self, apps, **options): 9 9 from django.core.management.sql import sql_custom 10 return u'\n'.join(sql_custom(app , self.style)).encode('utf-8')10 return u'\n'.join(sql_custom(apps, self.style)).encode('utf-8') -
django/core/management/commands/sql.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the CREATE TABLE SQL statements for the given app name(s)." 5 5 6 6 output_transaction = True 7 7 8 def handle_app(self, app , **options):8 def handle_app(self, apps, **options): 9 9 from django.core.management.sql import sql_create 10 return u'\n'.join(sql_create(app , self.style)).encode('utf-8')10 return u'\n'.join(sql_create(apps, self.style)).encode('utf-8') -
django/core/management/commands/sqlreset.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." 5 5 6 6 output_transaction = True 7 7 8 def handle_app(self, app , **options):8 def handle_app(self, apps, **options): 9 9 from django.core.management.sql import sql_reset 10 return u'\n'.join(sql_reset(app , self.style)).encode('utf-8')10 return u'\n'.join(sql_reset(apps, self.style)).encode('utf-8') -
django/core/management/commands/sqlclear.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the DROP TABLE SQL statements for the given app name(s)." 5 5 6 6 output_transaction = True 7 7 8 def handle_app(self, app , **options):8 def handle_app(self, apps, **options): 9 9 from django.core.management.sql import sql_delete 10 return u'\n'.join(sql_delete(app , self.style)).encode('utf-8')10 return u'\n'.join(sql_delete(apps, self.style)).encode('utf-8') -
django/core/management/commands/sqlindexes.py
1 from django.core.management.base import AppCommand1 from django.core.management.base import CrossAppCommand 2 2 3 class Command( AppCommand):3 class Command(CrossAppCommand): 4 4 help = "Prints the CREATE INDEX SQL statements for the given model module name(s)." 5 5 6 6 output_transaction = True -
django/core/management/commands/reset.py
1 from django.core.management.base import AppCommand, CommandError1 from django.core.management.base import CrossAppCommand, CommandError 2 2 from django.core.management.color import no_style 3 3 from optparse import make_option 4 4 5 class Command( AppCommand):6 option_list = AppCommand.option_list + (5 class Command(CrossAppCommand): 6 option_list = CrossAppCommand.option_list + ( 7 7 make_option('--noinput', action='store_false', dest='interactive', default=True, 8 8 help='Tells Django to NOT prompt the user for input of any kind.'), 9 9 ) … … 12 12 13 13 output_transaction = True 14 14 15 def handle_app(self, app , **options):15 def handle_app(self, apps, **options): 16 16 from django.db import connection, transaction 17 17 from django.conf import settings 18 18 from django.core.management.sql import sql_reset 19 20 for app in apps: 21 app_name = app.__name__.split('.')[-2] 19 22 20 app_name = app.__name__.split('.')[-2]21 22 23 self.style = no_style() 23 24 24 sql_list = sql_reset(app , self.style)25 sql_list = sql_reset(apps, self.style) 25 26 26 27 if options.get('interactive'): 27 28 confirm = raw_input(""" -
django/core/management/base.py
297 297 """ 298 298 raise NotImplementedError() 299 299 300 class CrossAppCommand(BaseCommand): 301 """ 302 Modified version of AppCommand which allows for multiple applications to 303 see each other models. 304 """ 305 306 args = '<appname appname ...>' 307 308 def handle(self, *app_labels, **options): 309 from django.db import models 310 from django.core.management.sql import sql_create 311 if not app_labels: 312 raise CommandError('Enter at least one appname.') 313 try: 314 app_list = [models.get_app(app_label) for app_label in app_labels] 315 except (ImproperlyConfigured, ImportError), e: 316 raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e) 317 return self.handle_app(app_list, **options) 318 319 def handle_app(self, apps, **options): 320 """ 321 Perform the command's actions for ``apps``, which will be the 322 Python module corresponding to an application name given on 323 the command line. 324 """ 325 raise NotImplementedError() 326 300 327 class LabelCommand(BaseCommand): 301 328 """ 302 329 A management command which takes one or more arbitrary arguments -
django/core/management/sql.py
7 7 except NameError: 8 8 from sets import Set as set # Python 2.3 fallback 9 9 10 def sql_create(app , style):10 def sql_create(apps, style): 11 11 "Returns a list of the CREATE TABLE SQL statements for the given app." 12 12 from django.db import connection, models 13 13 from django.conf import settings … … 23 23 # We trim models from the current app so that the sqlreset command does not 24 24 # generate invalid SQL (leaving models out of known_models is harmless, so 25 25 # we can be conservative). 26 app_models = models.get_models(app, include_auto_created=True) 26 if isinstance(apps, list): 27 app_models=[] 28 for app in apps: 29 for mod in models.get_models(app, include_auto_created=True): 30 app_models.append(mod) 31 else: 32 app_models = models.get_models(apps, include_auto_created=True) 27 33 final_output = [] 28 34 tables = connection.introspection.table_names() 29 35 known_models = set([model for model in connection.introspection.installed_models(tables) if model not in app_models]) … … 54 60 55 61 return final_output 56 62 57 def sql_delete(app , style):63 def sql_delete(apps, style): 58 64 "Returns a list of the DROP TABLE SQL statements for the given app." 59 65 from django.db import connection, models 60 66 from django.db.backends.util import truncate_name … … 76 82 77 83 # Output DROP TABLE statements for standard application tables. 78 84 to_delete = set() 79 85 if not isinstance(apps, list): 86 apps=[apps] 80 87 references_to_delete = {} 81 app_models = models.get_models(app, include_auto_created=True) 82 for model in app_models: 83 if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: 84 # The table exists, so it needs to be dropped 85 opts = model._meta 86 for f in opts.local_fields: 87 if f.rel and f.rel.to not in to_delete: 88 references_to_delete.setdefault(f.rel.to, []).append( (model, f) ) 88 for app in apps: 89 app_models = models.get_models(app, include_auto_created=True) 90 for model in app_models: 91 if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: 92 # The table exists, so it needs to be dropped 93 opts = model._meta 94 for f in opts.local_fields: 95 if f.rel and f.rel.to not in to_delete: 96 references_to_delete.setdefault(f.rel.to, []).append( (model, f) ) 89 97 90 to_delete.add(model)98 to_delete.add(model) 91 99 92 for model in app_models:93 if connection.introspection.table_name_converter(model._meta.db_table) in table_names:94 output.extend(connection.creation.sql_destroy_model(model, references_to_delete, style))100 for model in app_models: 101 if connection.introspection.table_name_converter(model._meta.db_table) in table_names: 102 output.extend(connection.creation.sql_destroy_model(model, references_to_delete, style)) 95 103 96 104 # Close database connection explicitly, in case this output is being piped 97 105 # directly into a database client, to avoid locking issues. … … 120 128 statements = connection.ops.sql_flush(style, tables, connection.introspection.sequence_list()) 121 129 return statements 122 130 123 def sql_custom(app , style):131 def sql_custom(apps, style): 124 132 "Returns a list of the custom table modifying SQL statements for the given app." 125 133 from django.db.models import get_models 126 134 output = [] 127 128 app_models = get_models(app)129 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))130 131 for model in app_models:132 output.extend(custom_sql_for_model(model, style))133 135 app_models = [] 136 if not isinstance(apps, list): 137 apps=[apps] 138 for app in apps: 139 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 140 for model in get_models(app): 141 output.extend(custom_sql_for_model(model, style)) 134 142 return output 135 143 136 def sql_indexes(app , style):144 def sql_indexes(apps, style): 137 145 "Returns a list of the CREATE INDEX SQL statements for all models in the given app." 138 146 from django.db import connection, models 139 147 output = [] 140 for model in models.get_models(app): 141 output.extend(connection.creation.sql_indexes_for_model(model, style)) 148 if not isinstance(apps, list): 149 apps=[apps] 150 for app in apps: 151 for model in models.get_models(app): 152 output.extend(connection.creation.sql_indexes_for_model(model, style)) 142 153 return output 143 154 144 155 def sql_all(app, style):