1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

[multi-db] Updated django.core.management to use pending dicts

correctly, and refactored get_sql_create() function to use manager and 
creation module builder as appropriate.


git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3292 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-07 21:30:03 +00:00
parent bbc82d0c73
commit c1087076b4

View File

@ -83,9 +83,25 @@ def get_version():
def get_sql_create(app):
"Returns a list of the CREATE TABLE SQL statements for the given app."
from django.db import get_creation_module, models
data_types = get_creation_module().DATA_TYPES
from django.db import models
# singleton representing the default connection
_default = object()
# final output will be divided by comments into sections for each
# named connection, if there are any named connections
connection_output = {}
pending_references = {}
final_output = []
app_models = models.get_models(app, creation_order=True)
for klass in app_models:
opts = klass._meta
connection_name = opts.db_connection or _default
output = connection_output.setdefault(connection_name, [])
info = opts.connection_info
creation = info.get_creation_module()
data_types = creation.DATA_TYPES
if not data_types:
# This must be the "dummy" database backend, which means the user
# hasn't set DATABASE_ENGINE.
@ -95,29 +111,39 @@ def get_sql_create(app):
sys.exit(1)
# Get installed models, so we generate REFERENCES right
installed_models = _get_installed_models(_get_table_list())
final_output = []
manager = klass._default_manager
tables = manager.get_table_list()
installed_models = manager.get_installed_models(tables)
models_output = set(installed_models)
pending_references = {}
app_models = models.get_models(app)
for klass in app_models:
output, references = _get_sql_model_create(klass, models_output)
final_output.extend(output)
builder = creation.builder
builder.models_already_seen.update(models_output)
model_output, references = builder.get_create_table(klass, style)
output.extend(model_output)
for refto, refs in references.items():
try:
pending_references[refto].extend(refs)
except KeyError:
pending_references[refto] = refs
final_output.extend(_get_sql_for_pending_references(klass, pending_references))
# Keep track of the fact that we've created the table for this model.
models_output.add(klass)
if klass in pending_references:
output.extend(pending_references.pop(klass))
# Create the many-to-many join tables.
for klass in app_models:
final_output.extend(_get_many_to_many_sql_for_model(klass))
many_many = builder.get_create_many_to_many(klass, style)
for refklass, statements in many_many.items():
output.extend(statements)
if len(connection_output.keys()) == 1:
# all for the default connection
for statements in connection_output.values():
final_output.extend(statements)
else:
for connection_name, statements in connection_output.items():
if connection_name == _default:
connection_name = '(default)'
final_output.append(' -- The following statements are for connection: %s' % connection_name)
final_output.extend(statements)
final_output.append(' -- END statements for %s\n' %
connection_name)
# Handle references to tables that are from other apps
# but don't exist physically
@ -125,9 +151,11 @@ def get_sql_create(app):
if not_installed_models:
final_output.append('-- The following references should be added but depend on non-existant tables:')
for klass in not_installed_models:
final_output.extend(['-- ' + sql for sql in
_get_sql_for_pending_references(klass, pending_references)])
final_output.extend(['-- ' + sql
for sql in pending_references.pop(klass)])
# convert BoundStatements into strings
final_output = map(str, final_output)
return final_output
get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)."
get_sql_create.args = APP_ARGS
@ -572,12 +600,26 @@ def install(app):
_check_for_validation_errors(app)
try:
pending = []
for model in models.get_models(app):
pending.extend(model._default_manager.install(True))
if pending:
for statement in pending:
pending = {}
for model in models.get_models(app, creation_order=True):
new_pending = model._default_manager.install(True)
for klass, statements in new_pending.items():
pending.setdefault(klass, []).extend(statements)
# execute any pending statements that were waiting for this model
if model in pending:
for statement in pending.pop(model):
statement.execute()
if pending:
for klass, statements in pending.items():
tables = klass._default_manager.get_table_list()
models_installed = klass._default_manager.get_installed_models(tables)
if klass in models_installed:
for statement in statements:
statement.execute()
else:
raise Exception("%s is not installed, but there are "
"pending statements that need it: %s"
% (klass, statements))
except Exception, e:
sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons:
* The database isn't running or isn't configured correctly.