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,51 +83,79 @@ def get_version():
def get_sql_create(app): def get_sql_create(app):
"Returns a list of the CREATE TABLE SQL statements for the given app." "Returns a list of the CREATE TABLE SQL statements for the given app."
from django.db import get_creation_module, models from django.db import models
data_types = get_creation_module().DATA_TYPES
if not data_types: # singleton representing the default connection
# This must be the "dummy" database backend, which means the user _default = object()
# hasn't set DATABASE_ENGINE.
sys.stderr.write(style.ERROR("Error: Django doesn't know which syntax to use for your SQL statements,\n" +
"because you haven't specified the DATABASE_ENGINE setting.\n" +
"Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'.\n"))
sys.exit(1)
# Get installed models, so we generate REFERENCES right # final output will be divided by comments into sections for each
installed_models = _get_installed_models(_get_table_list()) # named connection, if there are any named connections
connection_output = {}
final_output = []
models_output = set(installed_models)
pending_references = {} pending_references = {}
final_output = []
app_models = models.get_models(app)
app_models = models.get_models(app, creation_order=True)
for klass in app_models: for klass in app_models:
output, references = _get_sql_model_create(klass, models_output) opts = klass._meta
final_output.extend(output) 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.
sys.stderr.write(style.ERROR("Error: Django doesn't know which syntax to use for your SQL statements,\n" +
"because you haven't specified the DATABASE_ENGINE setting.\n" +
"Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'.\n"))
sys.exit(1)
# Get installed models, so we generate REFERENCES right
manager = klass._default_manager
tables = manager.get_table_list()
installed_models = manager.get_installed_models(tables)
models_output = set(installed_models)
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(): for refto, refs in references.items():
try: try:
pending_references[refto].extend(refs) pending_references[refto].extend(refs)
except KeyError: except KeyError:
pending_references[refto] = refs pending_references[refto] = refs
final_output.extend(_get_sql_for_pending_references(klass, pending_references)) if klass in pending_references:
# Keep track of the fact that we've created the table for this model. output.extend(pending_references.pop(klass))
models_output.add(klass)
# Create the many-to-many join tables. # Create the many-to-many join tables.
for klass in app_models: many_many = builder.get_create_many_to_many(klass, style)
final_output.extend(_get_many_to_many_sql_for_model(klass)) 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 # Handle references to tables that are from other apps
# but don't exist physically # but don't exist physically
not_installed_models = set(pending_references.keys()) not_installed_models = set(pending_references.keys())
if not_installed_models: if not_installed_models:
final_output.append('-- The following references should be added but depend on non-existant tables:') final_output.append('-- The following references should be added but depend on non-existant tables:')
for klass in not_installed_models: for klass in not_installed_models:
final_output.extend(['-- ' + sql for sql in final_output.extend(['-- ' + sql
_get_sql_for_pending_references(klass, pending_references)]) for sql in pending_references.pop(klass)])
# convert BoundStatements into strings
final_output = map(str, final_output)
return 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.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)."
get_sql_create.args = APP_ARGS get_sql_create.args = APP_ARGS
@ -572,12 +600,26 @@ def install(app):
_check_for_validation_errors(app) _check_for_validation_errors(app)
try: try:
pending = [] pending = {}
for model in models.get_models(app): for model in models.get_models(app, creation_order=True):
pending.extend(model._default_manager.install(True)) new_pending = model._default_manager.install(True)
if pending: for klass, statements in new_pending.items():
for statement in pending: pending.setdefault(klass, []).extend(statements)
statement.execute() # 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: except Exception, e:
sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons: sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons:
* The database isn't running or isn't configured correctly. * The database isn't running or isn't configured correctly.