magic-removal: refactored django.core.management some more: cleaned up duplicate validation code, and made sqlreset remove permissions/content-types correctly.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2479 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
cb0a57fc8b
commit
4456e2c26c
|
@ -200,25 +200,20 @@ def _get_many_to_many_sql_for_model(klass):
|
||||||
|
|
||||||
def get_sql_delete(app):
|
def get_sql_delete(app):
|
||||||
"Returns a list of the DROP TABLE SQL statements for the given app."
|
"Returns a list of the DROP TABLE SQL statements for the given app."
|
||||||
from django.db import backend, connection, models, transaction
|
from django.db import backend, connection, models, get_introspection_module
|
||||||
|
introspection = get_introspection_module()
|
||||||
|
|
||||||
|
# This should work even if a connecton isn't available
|
||||||
try:
|
try:
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
except:
|
except:
|
||||||
cursor = None
|
cursor = None
|
||||||
|
|
||||||
# Determine whether the admin log table exists. It only exists if the
|
# Figure out which tables already exist
|
||||||
# person has installed the admin app.
|
if cursor:
|
||||||
try:
|
table_names = introspection.get_table_list(cursor)
|
||||||
if cursor is not None:
|
|
||||||
# Check whether the table exists.
|
|
||||||
cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name('django_admin_log'))
|
|
||||||
except:
|
|
||||||
# The table doesn't exist, so it doesn't need to be dropped.
|
|
||||||
transaction.rollback_unless_managed()
|
|
||||||
admin_log_exists = False
|
|
||||||
else:
|
else:
|
||||||
admin_log_exists = True
|
table_names = []
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
|
@ -228,14 +223,8 @@ def get_sql_delete(app):
|
||||||
references_to_delete = {}
|
references_to_delete = {}
|
||||||
app_models = models.get_models(app)
|
app_models = models.get_models(app)
|
||||||
for klass in app_models:
|
for klass in app_models:
|
||||||
try:
|
if cursor and klass._meta.db_table in table_names:
|
||||||
if cursor is not None:
|
# The table exists, so it needs to be dropped
|
||||||
# Check whether the table exists.
|
|
||||||
cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name(klass._meta.db_table))
|
|
||||||
except:
|
|
||||||
# The table doesn't exist, so it doesn't need to be dropped.
|
|
||||||
transaction.rollback_unless_managed()
|
|
||||||
else:
|
|
||||||
opts = klass._meta
|
opts = klass._meta
|
||||||
for f in opts.fields:
|
for f in opts.fields:
|
||||||
if f.rel and f.rel.to not in to_delete:
|
if f.rel and f.rel.to not in to_delete:
|
||||||
|
@ -244,14 +233,8 @@ def get_sql_delete(app):
|
||||||
to_delete.add(klass)
|
to_delete.add(klass)
|
||||||
|
|
||||||
for klass in app_models:
|
for klass in app_models:
|
||||||
try:
|
if cursor and klass._meta.db_table in table_names:
|
||||||
if cursor is not None:
|
# Drop the able now
|
||||||
# Check whether the table exists.
|
|
||||||
cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name(klass._meta.db_table))
|
|
||||||
except:
|
|
||||||
# The table doesn't exist, so it doesn't need to be dropped.
|
|
||||||
transaction.rollback_unless_managed()
|
|
||||||
else:
|
|
||||||
output.append("DROP TABLE %s;" % backend.quote_name(klass._meta.db_table))
|
output.append("DROP TABLE %s;" % backend.quote_name(klass._meta.db_table))
|
||||||
if backend.supports_constraints and references_to_delete.has_key(klass):
|
if backend.supports_constraints and references_to_delete.has_key(klass):
|
||||||
for rel_class, f in references_to_delete[klass]:
|
for rel_class, f in references_to_delete[klass]:
|
||||||
|
@ -268,35 +251,37 @@ def get_sql_delete(app):
|
||||||
for klass in app_models:
|
for klass in app_models:
|
||||||
opts = klass._meta
|
opts = klass._meta
|
||||||
for f in opts.many_to_many:
|
for f in opts.many_to_many:
|
||||||
try:
|
if cursor and f.m2m_db_table() in table_names:
|
||||||
if cursor is not None:
|
|
||||||
cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name(f.m2m_db_table()))
|
|
||||||
except:
|
|
||||||
transaction.rollback_unless_managed()
|
|
||||||
else:
|
|
||||||
output.append("DROP TABLE %s;" % backend.quote_name(f.m2m_db_table()))
|
output.append("DROP TABLE %s;" % backend.quote_name(f.m2m_db_table()))
|
||||||
|
|
||||||
app_label = app_models[0]._meta.app_label
|
app_label = app_models[0]._meta.app_label
|
||||||
|
|
||||||
# Delete from django_package, auth_permission, django_content_type.
|
# Delete from django_package, auth_permission, django_content_type.
|
||||||
# TODO: fix this when permissions are activated again
|
if cursor and "django_content_type" in table_names:
|
||||||
# output.append("DELETE FROM %s WHERE %s = '%s';" % \
|
|
||||||
# (backend.quote_name('auth_permission'), backend.quote_name('package'), app_label))
|
# Grab a list of affected content-types
|
||||||
|
cursor.execute("SELECT id FROM django_content_type WHERE app_label = %s", [app_label])
|
||||||
|
affected_content_types = [r[0] for r in cursor.fetchall()]
|
||||||
|
|
||||||
|
# Remember do this this business in reverse order since the returned
|
||||||
|
# values are reversed below
|
||||||
output.append("DELETE FROM %s WHERE %s = '%s';" % \
|
output.append("DELETE FROM %s WHERE %s = '%s';" % \
|
||||||
(backend.quote_name('django_content_type'), backend.quote_name('app_label'), app_label))
|
(backend.quote_name('django_content_type'), backend.quote_name('app_label'), app_label))
|
||||||
|
|
||||||
# Delete from the admin log.
|
if "auth_permission" in table_names:
|
||||||
if cursor is not None:
|
for ctype_id in affected_content_types:
|
||||||
cursor.execute("SELECT %s FROM %s WHERE %s = %%s" % \
|
|
||||||
(backend.quote_name('id'), backend.quote_name('django_content_type'),
|
|
||||||
backend.quote_name('package')), [app_label])
|
|
||||||
if admin_log_exists:
|
|
||||||
for row in cursor.fetchall():
|
|
||||||
output.append("DELETE FROM %s WHERE %s = %s;" % \
|
output.append("DELETE FROM %s WHERE %s = %s;" % \
|
||||||
(backend.quote_name('django_admin_log'), backend.quote_name('content_type_id'), row[0]))
|
(backend.quote_name("auth_permission"), backend.quote_name("content_type_id"), ctype_id))
|
||||||
|
|
||||||
|
# Delete from the admin log.
|
||||||
|
if "django_admin_log" in table_names:
|
||||||
|
for ctype_id in affected_content_types:
|
||||||
|
output.append("DELETE FROM %s WHERE %s = %s;" % \
|
||||||
|
(backend.quote_name("django_admin_log"), backend.quote_name("content_type_id"), ctype_id))
|
||||||
|
|
||||||
# Close database connection explicitly, in case this output is being piped
|
# Close database connection explicitly, in case this output is being piped
|
||||||
# directly into a database client, to avoid locking issues.
|
# directly into a database client, to avoid locking issues.
|
||||||
|
if cursor:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
@ -492,13 +477,7 @@ def reset(app):
|
||||||
app_name = app.__name__.split('.')[-2]
|
app_name = app.__name__.split('.')[-2]
|
||||||
|
|
||||||
# First, try validating the models.
|
# First, try validating the models.
|
||||||
s = StringIO()
|
_check_for_validation_errors(app)
|
||||||
num_errors = get_validation_errors(s, app)
|
|
||||||
if num_errors:
|
|
||||||
sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % app_name)
|
|
||||||
s.seek(0)
|
|
||||||
sys.stderr.write(s.read())
|
|
||||||
sys.exit(1)
|
|
||||||
sql_list = get_sql_reset(app)
|
sql_list = get_sql_reset(app)
|
||||||
|
|
||||||
confirm = raw_input("""
|
confirm = raw_input("""
|
||||||
|
|
Loading…
Reference in New Issue