Fixed #3822 -- Changed get_indexes_for_model() in django.core.management to quote index names. Thanks, Robin Breathe

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4835 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-03-27 22:35:22 +00:00
parent e2937ef6a0
commit 302d183a2f
1 changed files with 26 additions and 26 deletions

View File

@ -186,7 +186,7 @@ def _get_sql_model_create(model, known_models=set()):
if f.rel.to in known_models: if f.rel.to in known_models:
field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \ field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \ style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \
style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' + style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' +
backend.get_deferrable_sql() backend.get_deferrable_sql()
) )
else: else:
@ -232,7 +232,7 @@ def _get_sql_for_pending_references(model, pending_references):
r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table)))) r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \ final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
(backend.quote_name(r_table), r_name, (backend.quote_name(r_table), r_name,
backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col), backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col),
backend.get_deferrable_sql())) backend.get_deferrable_sql()))
del pending_references[model] del pending_references[model]
return final_output return final_output
@ -456,7 +456,7 @@ def get_sql_indexes_for_model(model):
unique = f.unique and 'UNIQUE ' or '' unique = f.unique and 'UNIQUE ' or ''
output.append( output.append(
style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \ style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
style.SQL_TABLE('%s_%s' % (model._meta.db_table, f.column)) + ' ' + \ style.SQL_TABLE(backend.quote_name('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
style.SQL_KEYWORD('ON') + ' ' + \ style.SQL_KEYWORD('ON') + ' ' + \
style.SQL_TABLE(backend.quote_name(model._meta.db_table)) + ' ' + \ style.SQL_TABLE(backend.quote_name(model._meta.db_table)) + ' ' + \
"(%s);" % style.SQL_FIELD(backend.quote_name(f.column)) "(%s);" % style.SQL_FIELD(backend.quote_name(f.column))
@ -554,7 +554,7 @@ def syncdb(verbosity=1, interactive=True):
# to do at this point. # to do at this point.
_emit_post_sync_signal(created_models, verbosity, interactive) _emit_post_sync_signal(created_models, verbosity, interactive)
# Install custom SQL for the app (but only if this # Install custom SQL for the app (but only if this
# is a model we've just created) # is a model we've just created)
for app in models.get_apps(): for app in models.get_apps():
for model in models.get_models(app): for model in models.get_models(app):
@ -696,7 +696,7 @@ def flush(verbosity=1, interactive=True):
from django.conf import settings from django.conf import settings
from django.db import connection, transaction, models from django.db import connection, transaction, models
from django.dispatch import dispatcher from django.dispatch import dispatcher
disable_termcolors() disable_termcolors()
# First, try validating the models. # First, try validating the models.
@ -709,7 +709,7 @@ def flush(verbosity=1, interactive=True):
__import__(app_name + '.management', {}, {}, ['']) __import__(app_name + '.management', {}, {}, [''])
except ImportError: except ImportError:
pass pass
sql_list = get_sql_flush() sql_list = get_sql_flush()
if interactive: if interactive:
@ -743,10 +743,10 @@ The full error: """ % settings.DATABASE_NAME + style.ERROR_OUTPUT(str(e)) + '\n'
# applications to respond as if the database had been # applications to respond as if the database had been
# sync'd from scratch. # sync'd from scratch.
_emit_post_sync_signal(models.get_models(), verbosity, interactive) _emit_post_sync_signal(models.get_models(), verbosity, interactive)
# Reinstall the initial_data fixture # Reinstall the initial_data fixture
load_data(['initial_data'], verbosity=verbosity) load_data(['initial_data'], verbosity=verbosity)
else: else:
print "Flush cancelled." print "Flush cancelled."
flush.help_doc = "Executes ``sqlflush`` on the current database." flush.help_doc = "Executes ``sqlflush`` on the current database."
@ -1325,7 +1325,7 @@ def test(app_labels, verbosity=1):
failures = test_runner(app_list, verbosity) failures = test_runner(app_list, verbosity)
if failures: if failures:
sys.exit(failures) sys.exit(failures)
test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified' test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified'
test.args = '[--verbosity] ' + APP_ARGS test.args = '[--verbosity] ' + APP_ARGS
@ -1336,23 +1336,23 @@ def load_data(fixture_labels, verbosity=1):
from django.db import connection, transaction from django.db import connection, transaction
from django.conf import settings from django.conf import settings
import sys import sys
# Keep a count of the installed objects and fixtures # Keep a count of the installed objects and fixtures
count = [0,0] count = [0,0]
humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
# Get a cursor (even though we don't need one yet). This has # Get a cursor (even though we don't need one yet). This has
# the side effect of initializing the test database (if # the side effect of initializing the test database (if
# it isn't already initialized). # it isn't already initialized).
cursor = connection.cursor() cursor = connection.cursor()
# Start transaction management. All fixtures are installed in a # Start transaction management. All fixtures are installed in a
# single transaction to ensure that all references are resolved. # single transaction to ensure that all references are resolved.
transaction.commit_unless_managed() transaction.commit_unless_managed()
transaction.enter_transaction_management() transaction.enter_transaction_management()
transaction.managed(True) transaction.managed(True)
app_fixtures = [os.path.join(os.path.dirname(app.__file__),'fixtures') for app in get_apps()] app_fixtures = [os.path.join(os.path.dirname(app.__file__),'fixtures') for app in get_apps()]
for fixture_label in fixture_labels: for fixture_label in fixture_labels:
parts = fixture_label.split('.') parts = fixture_label.split('.')
@ -1365,13 +1365,13 @@ def load_data(fixture_labels, verbosity=1):
formats = [format] formats = [format]
else: else:
formats = [] formats = []
if verbosity > 0: if verbosity > 0:
if formats: if formats:
print "Loading '%s' fixtures..." % fixture_name print "Loading '%s' fixtures..." % fixture_name
else: else:
print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format) print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format)
for fixture_dir in app_fixtures + list(settings.FIXTURE_DIRS) + ['']: for fixture_dir in app_fixtures + list(settings.FIXTURE_DIRS) + ['']:
if verbosity > 1: if verbosity > 1:
print "Checking %s for fixtures..." % humanize(fixture_dir) print "Checking %s for fixtures..." % humanize(fixture_dir)
@ -1387,7 +1387,7 @@ def load_data(fixture_labels, verbosity=1):
fixture = open(full_path, 'r') fixture = open(full_path, 'r')
if label_found: if label_found:
fixture.close() fixture.close()
print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
(fixture_name, humanize(fixture_dir))) (fixture_name, humanize(fixture_dir)))
transaction.rollback() transaction.rollback()
transaction.leave_transaction_management() transaction.leave_transaction_management()
@ -1406,7 +1406,7 @@ def load_data(fixture_labels, verbosity=1):
except Exception, e: except Exception, e:
fixture.close() fixture.close()
sys.stderr.write( sys.stderr.write(
style.ERROR("Problem installing fixture '%s': %s\n" % style.ERROR("Problem installing fixture '%s': %s\n" %
(full_path, str(e)))) (full_path, str(e))))
transaction.rollback() transaction.rollback()
transaction.leave_transaction_management() transaction.leave_transaction_management()
@ -1424,27 +1424,27 @@ def load_data(fixture_labels, verbosity=1):
print "Installed %d object(s) from %d fixture(s)" % tuple(count) print "Installed %d object(s) from %d fixture(s)" % tuple(count)
transaction.commit() transaction.commit()
transaction.leave_transaction_management() transaction.leave_transaction_management()
load_data.help_doc = 'Installs the named fixture(s) in the database' load_data.help_doc = 'Installs the named fixture(s) in the database'
load_data.args = "[--verbosity] fixture, fixture, ..." load_data.args = "[--verbosity] fixture, fixture, ..."
def dump_data(app_labels, format='json', indent=None): def dump_data(app_labels, format='json', indent=None):
"Output the current contents of the database as a fixture of the given format" "Output the current contents of the database as a fixture of the given format"
from django.db.models import get_app, get_apps, get_models from django.db.models import get_app, get_apps, get_models
from django.core import serializers from django.core import serializers
if len(app_labels) == 0: if len(app_labels) == 0:
app_list = get_apps() app_list = get_apps()
else: else:
app_list = [get_app(app_label) for app_label in app_labels] app_list = [get_app(app_label) for app_label in app_labels]
# Check that the serialization format exists; this is a shortcut to # Check that the serialization format exists; this is a shortcut to
# avoid collating all the objects and _then_ failing. # avoid collating all the objects and _then_ failing.
try: try:
serializers.get_serializer(format) serializers.get_serializer(format)
except KeyError: except KeyError:
sys.stderr.write(style.ERROR("Unknown serialization format: %s\n" % format)) sys.stderr.write(style.ERROR("Unknown serialization format: %s\n" % format))
objects = [] objects = []
for app in app_list: for app in app_list:
for model in get_models(app): for model in get_models(app):
@ -1539,7 +1539,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
parser.add_option('--noreload', action='store_false', dest='use_reloader', default=True, parser.add_option('--noreload', action='store_false', dest='use_reloader', default=True,
help='Tells Django to NOT use the auto-reloader when running the development server.') help='Tells Django to NOT use the auto-reloader when running the development server.')
parser.add_option('--format', default='json', dest='format', parser.add_option('--format', default='json', dest='format',
help='Specifies the output serialization format for fixtures') help='Specifies the output serialization format for fixtures')
parser.add_option('--indent', default=None, dest='indent', parser.add_option('--indent', default=None, dest='indent',
type='int', help='Specifies the indent level to use when pretty-printing output') type='int', help='Specifies the indent level to use when pretty-printing output')
parser.add_option('--verbosity', action='store', dest='verbosity', default='1', parser.add_option('--verbosity', action='store', dest='verbosity', default='1',