2007-08-16 22:34:01 +08:00
|
|
|
from django.core.management.base import NoArgsCommand
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management.color import no_style
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2007-09-10 05:57:59 +08:00
|
|
|
from optparse import make_option
|
2007-08-16 23:14:21 +08:00
|
|
|
import sys
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set # Python 2.3 fallback
|
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
class Command(NoArgsCommand):
|
2007-09-10 05:57:59 +08:00
|
|
|
option_list = NoArgsCommand.option_list + (
|
|
|
|
make_option('--noinput', action='store_false', dest='interactive', default=True,
|
|
|
|
help='Tells Django to NOT prompt the user for input of any kind.'),
|
|
|
|
)
|
2007-08-16 14:06:55 +08:00
|
|
|
help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
|
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
def handle_noargs(self, **options):
|
2007-08-20 10:28:40 +08:00
|
|
|
from django.db import connection, transaction, models
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.conf import settings
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
|
2007-08-17 03:34:25 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
verbosity = int(options.get('verbosity', 1))
|
|
|
|
interactive = options.get('interactive')
|
2008-06-19 21:24:39 +08:00
|
|
|
show_traceback = options.get('traceback', False)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
self.style = no_style()
|
|
|
|
|
|
|
|
# Import the 'management' module within each installed app, to register
|
|
|
|
# dispatcher events.
|
|
|
|
for app_name in settings.INSTALLED_APPS:
|
|
|
|
try:
|
2009-03-19 00:55:59 +08:00
|
|
|
import_module('.management', app_name)
|
2007-12-02 23:17:14 +08:00
|
|
|
except ImportError, exc:
|
2008-06-12 12:22:02 +08:00
|
|
|
# This is slightly hackish. We want to ignore ImportErrors
|
|
|
|
# if the "management" module itself is missing -- but we don't
|
|
|
|
# want to ignore the exception if the management module exists
|
|
|
|
# but raises an ImportError for some reason. The only way we
|
|
|
|
# can do this is to check the text of the exception. Note that
|
|
|
|
# we're a bit broad in how we check the text, because different
|
2008-10-02 20:57:13 +08:00
|
|
|
# Python implementations may not use the same text.
|
2008-07-11 23:08:09 +08:00
|
|
|
# CPython uses the text "No module named management"
|
|
|
|
# PyPy uses "No module named myproject.myapp.management"
|
2008-06-12 12:22:02 +08:00
|
|
|
msg = exc.args[0]
|
2008-07-11 23:08:09 +08:00
|
|
|
if not msg.startswith('No module named') or 'management' not in msg:
|
2007-12-02 23:17:14 +08:00
|
|
|
raise
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
# Get a list of already installed *models* so that references work right.
|
2008-08-11 20:11:25 +08:00
|
|
|
tables = connection.introspection.table_names()
|
|
|
|
seen_models = connection.introspection.installed_models(tables)
|
2007-08-16 14:06:55 +08:00
|
|
|
created_models = set()
|
|
|
|
pending_references = {}
|
|
|
|
|
|
|
|
# Create the tables for each model
|
|
|
|
for app in models.get_apps():
|
|
|
|
app_name = app.__name__.split('.')[-2]
|
|
|
|
model_list = models.get_models(app)
|
|
|
|
for model in model_list:
|
|
|
|
# Create the model's database table, if it doesn't already exist.
|
|
|
|
if verbosity >= 2:
|
|
|
|
print "Processing %s.%s model" % (app_name, model._meta.object_name)
|
2008-08-11 20:11:25 +08:00
|
|
|
if connection.introspection.table_name_converter(model._meta.db_table) in tables:
|
2007-08-16 14:06:55 +08:00
|
|
|
continue
|
2008-08-11 20:11:25 +08:00
|
|
|
sql, references = connection.creation.sql_create_model(model, self.style, seen_models)
|
2007-08-16 14:06:55 +08:00
|
|
|
seen_models.add(model)
|
|
|
|
created_models.add(model)
|
|
|
|
for refto, refs in references.items():
|
|
|
|
pending_references.setdefault(refto, []).extend(refs)
|
2008-03-11 06:18:50 +08:00
|
|
|
if refto in seen_models:
|
2008-08-11 20:11:25 +08:00
|
|
|
sql.extend(connection.creation.sql_for_pending_references(refto, self.style, pending_references))
|
|
|
|
sql.extend(connection.creation.sql_for_pending_references(model, self.style, pending_references))
|
2009-03-09 11:35:02 +08:00
|
|
|
if verbosity >= 1 and sql:
|
2007-08-16 14:06:55 +08:00
|
|
|
print "Creating table %s" % model._meta.db_table
|
|
|
|
for statement in sql:
|
|
|
|
cursor.execute(statement)
|
2008-08-11 20:11:25 +08:00
|
|
|
tables.append(connection.introspection.table_name_converter(model._meta.db_table))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Create the m2m tables. This must be done after all tables have been created
|
|
|
|
# to ensure that all referred tables will exist.
|
|
|
|
for app in models.get_apps():
|
|
|
|
app_name = app.__name__.split('.')[-2]
|
|
|
|
model_list = models.get_models(app)
|
|
|
|
for model in model_list:
|
|
|
|
if model in created_models:
|
2008-08-11 20:11:25 +08:00
|
|
|
sql = connection.creation.sql_for_many_to_many(model, self.style)
|
2007-08-16 14:06:55 +08:00
|
|
|
if sql:
|
|
|
|
if verbosity >= 2:
|
|
|
|
print "Creating many-to-many tables for %s.%s model" % (app_name, model._meta.object_name)
|
|
|
|
for statement in sql:
|
|
|
|
cursor.execute(statement)
|
|
|
|
|
|
|
|
transaction.commit_unless_managed()
|
|
|
|
|
|
|
|
# Send the post_syncdb signal, so individual apps can do whatever they need
|
|
|
|
# to do at this point.
|
|
|
|
emit_post_sync_signal(created_models, verbosity, interactive)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2008-07-11 22:06:59 +08:00
|
|
|
# The connection may have been closed by a syncdb handler.
|
|
|
|
cursor = connection.cursor()
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# Install custom SQL for the app (but only if this
|
|
|
|
# is a model we've just created)
|
|
|
|
for app in models.get_apps():
|
|
|
|
app_name = app.__name__.split('.')[-2]
|
|
|
|
for model in models.get_models(app):
|
|
|
|
if model in created_models:
|
2008-07-29 13:53:44 +08:00
|
|
|
custom_sql = custom_sql_for_model(model, self.style)
|
2007-08-16 14:06:55 +08:00
|
|
|
if custom_sql:
|
|
|
|
if verbosity >= 1:
|
|
|
|
print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
|
|
|
|
try:
|
|
|
|
for sql in custom_sql:
|
|
|
|
cursor.execute(sql)
|
|
|
|
except Exception, e:
|
2008-06-19 21:24:39 +08:00
|
|
|
sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \
|
2007-08-16 14:06:55 +08:00
|
|
|
(app_name, model._meta.object_name, e))
|
2008-06-19 21:24:39 +08:00
|
|
|
if show_traceback:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2007-08-16 14:06:55 +08:00
|
|
|
transaction.rollback_unless_managed()
|
|
|
|
else:
|
|
|
|
transaction.commit_unless_managed()
|
2008-06-19 21:24:39 +08:00
|
|
|
else:
|
|
|
|
if verbosity >= 2:
|
|
|
|
print "No custom SQL for %s.%s model" % (app_name, model._meta.object_name)
|
2007-08-16 14:06:55 +08:00
|
|
|
# Install SQL indicies for all newly created models
|
|
|
|
for app in models.get_apps():
|
|
|
|
app_name = app.__name__.split('.')[-2]
|
|
|
|
for model in models.get_models(app):
|
|
|
|
if model in created_models:
|
2008-08-11 20:11:25 +08:00
|
|
|
index_sql = connection.creation.sql_indexes_for_model(model, self.style)
|
2007-08-16 14:06:55 +08:00
|
|
|
if index_sql:
|
|
|
|
if verbosity >= 1:
|
|
|
|
print "Installing index for %s.%s model" % (app_name, model._meta.object_name)
|
|
|
|
try:
|
|
|
|
for sql in index_sql:
|
|
|
|
cursor.execute(sql)
|
|
|
|
except Exception, e:
|
2008-07-11 22:06:59 +08:00
|
|
|
sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \
|
2007-08-16 14:06:55 +08:00
|
|
|
(app_name, model._meta.object_name, e))
|
|
|
|
transaction.rollback_unless_managed()
|
|
|
|
else:
|
|
|
|
transaction.commit_unless_managed()
|
|
|
|
|
2007-08-18 06:56:36 +08:00
|
|
|
# Install the 'initial_data' fixture, using format discovery
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management import call_command
|
2007-08-18 06:56:36 +08:00
|
|
|
call_command('loaddata', 'initial_data', verbosity=verbosity)
|