2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management.base import CommandError
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set # Python 2.3 fallback
|
|
|
|
|
2008-05-31 09:01:17 +08:00
|
|
|
def table_names():
|
2007-08-16 14:06:55 +08:00
|
|
|
"Returns a list of all table names that exist in the database."
|
|
|
|
from django.db import connection, get_introspection_module
|
|
|
|
cursor = connection.cursor()
|
2008-05-31 09:01:17 +08:00
|
|
|
return set(get_introspection_module().get_table_list(cursor))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-05-31 09:01:17 +08:00
|
|
|
def django_table_names(only_existing=False):
|
2007-08-26 03:32:30 +08:00
|
|
|
"""
|
|
|
|
Returns a list of all table names that have associated Django models and
|
|
|
|
are in INSTALLED_APPS.
|
|
|
|
|
|
|
|
If only_existing is True, the resulting list will only include the tables
|
|
|
|
that actually exist in the database.
|
|
|
|
"""
|
|
|
|
from django.db import models
|
2008-05-31 09:01:17 +08:00
|
|
|
tables = set()
|
2007-08-26 03:32:30 +08:00
|
|
|
for app in models.get_apps():
|
|
|
|
for model in models.get_models(app):
|
2008-05-31 09:01:17 +08:00
|
|
|
tables.add(model._meta.db_table)
|
|
|
|
tables.update([f.m2m_db_table() for f in model._meta.local_many_to_many])
|
2007-08-26 03:32:30 +08:00
|
|
|
if only_existing:
|
2008-05-31 09:01:17 +08:00
|
|
|
tables = [t for t in tables if t in table_names()]
|
2007-08-26 03:32:30 +08:00
|
|
|
return tables
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
def installed_models(table_list):
|
|
|
|
"Returns a set of all models that are installed, given a list of existing table names."
|
2007-08-20 10:20:33 +08:00
|
|
|
from django.db import connection, models
|
2007-08-16 14:06:55 +08:00
|
|
|
all_models = []
|
|
|
|
for app in models.get_apps():
|
|
|
|
for model in models.get_models(app):
|
|
|
|
all_models.append(model)
|
2007-08-20 10:20:33 +08:00
|
|
|
if connection.features.uses_case_insensitive_names:
|
2007-08-16 14:06:55 +08:00
|
|
|
converter = lambda x: x.upper()
|
|
|
|
else:
|
|
|
|
converter = lambda x: x
|
|
|
|
return set([m for m in all_models if converter(m._meta.db_table) in map(converter, table_list)])
|
|
|
|
|
|
|
|
def sequence_list():
|
|
|
|
"Returns a list of information about all DB sequences for all models in all apps."
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
apps = models.get_apps()
|
|
|
|
sequence_list = []
|
|
|
|
|
|
|
|
for app in apps:
|
|
|
|
for model in models.get_models(app):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in model._meta.local_fields:
|
2007-08-16 14:06:55 +08:00
|
|
|
if isinstance(f, models.AutoField):
|
|
|
|
sequence_list.append({'table': model._meta.db_table, 'column': f.column})
|
|
|
|
break # Only one AutoField is allowed per model, so don't bother continuing.
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in model._meta.local_many_to_many:
|
2007-08-16 14:06:55 +08:00
|
|
|
sequence_list.append({'table': f.m2m_db_table(), 'column': None})
|
|
|
|
|
|
|
|
return sequence_list
|
|
|
|
|
|
|
|
def sql_create(app, style):
|
|
|
|
"Returns a list of the CREATE TABLE SQL statements for the given app."
|
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
if settings.DATABASE_ENGINE == 'dummy':
|
|
|
|
# This must be the "dummy" database backend, which means the user
|
|
|
|
# hasn't set DATABASE_ENGINE.
|
|
|
|
raise CommandError("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'.")
|
|
|
|
|
|
|
|
# Get installed models, so we generate REFERENCES right.
|
|
|
|
# We trim models from the current app so that the sqlreset command does not
|
|
|
|
# generate invalid SQL (leaving models out of known_models is harmless, so
|
|
|
|
# we can be conservative).
|
|
|
|
app_models = models.get_models(app)
|
|
|
|
final_output = []
|
2008-05-31 09:01:17 +08:00
|
|
|
known_models = set([model for model in installed_models(table_names()) if model not in app_models])
|
2007-08-16 14:06:55 +08:00
|
|
|
pending_references = {}
|
|
|
|
|
|
|
|
for model in app_models:
|
|
|
|
output, references = sql_model_create(model, style, known_models)
|
|
|
|
final_output.extend(output)
|
|
|
|
for refto, refs in references.items():
|
|
|
|
pending_references.setdefault(refto, []).extend(refs)
|
2008-03-11 06:18:50 +08:00
|
|
|
if refto in known_models:
|
|
|
|
final_output.extend(sql_for_pending_references(refto, style, pending_references))
|
2007-08-16 14:06:55 +08:00
|
|
|
final_output.extend(sql_for_pending_references(model, style, pending_references))
|
|
|
|
# Keep track of the fact that we've created the table for this model.
|
|
|
|
known_models.add(model)
|
|
|
|
|
|
|
|
# Create the many-to-many join tables.
|
|
|
|
for model in app_models:
|
|
|
|
final_output.extend(many_to_many_sql_for_model(model, style))
|
|
|
|
|
|
|
|
# Handle references to tables that are from other apps
|
|
|
|
# but don't exist physically.
|
|
|
|
not_installed_models = set(pending_references.keys())
|
|
|
|
if not_installed_models:
|
|
|
|
alter_sql = []
|
|
|
|
for model in not_installed_models:
|
|
|
|
alter_sql.extend(['-- ' + sql for sql in
|
|
|
|
sql_for_pending_references(model, style, pending_references)])
|
|
|
|
if alter_sql:
|
|
|
|
final_output.append('-- The following references should be added but depend on non-existent tables:')
|
|
|
|
final_output.extend(alter_sql)
|
|
|
|
|
|
|
|
return final_output
|
|
|
|
|
|
|
|
def sql_delete(app, style):
|
|
|
|
"Returns a list of the DROP TABLE SQL statements for the given app."
|
2007-08-20 11:13:03 +08:00
|
|
|
from django.db import connection, models, get_introspection_module
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.db.backends.util import truncate_name
|
2007-12-17 14:02:35 +08:00
|
|
|
from django.contrib.contenttypes import generic
|
2007-08-16 14:06:55 +08:00
|
|
|
introspection = get_introspection_module()
|
|
|
|
|
|
|
|
# This should work even if a connection isn't available
|
|
|
|
try:
|
|
|
|
cursor = connection.cursor()
|
|
|
|
except:
|
|
|
|
cursor = None
|
|
|
|
|
|
|
|
# Figure out which tables already exist
|
|
|
|
if cursor:
|
|
|
|
table_names = introspection.get_table_list(cursor)
|
|
|
|
else:
|
|
|
|
table_names = []
|
2007-08-20 10:20:33 +08:00
|
|
|
if connection.features.uses_case_insensitive_names:
|
2007-09-16 01:56:36 +08:00
|
|
|
table_name_converter = lambda x: x.upper()
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
table_name_converter = lambda x: x
|
|
|
|
|
|
|
|
output = []
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Output DROP TABLE statements for standard application tables.
|
|
|
|
to_delete = set()
|
|
|
|
|
|
|
|
references_to_delete = {}
|
|
|
|
app_models = models.get_models(app)
|
|
|
|
for model in app_models:
|
|
|
|
if cursor and table_name_converter(model._meta.db_table) in table_names:
|
|
|
|
# The table exists, so it needs to be dropped
|
|
|
|
opts = model._meta
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in opts.local_fields:
|
2007-08-16 14:06:55 +08:00
|
|
|
if f.rel and f.rel.to not in to_delete:
|
|
|
|
references_to_delete.setdefault(f.rel.to, []).append( (model, f) )
|
|
|
|
|
|
|
|
to_delete.add(model)
|
|
|
|
|
|
|
|
for model in app_models:
|
|
|
|
if cursor and table_name_converter(model._meta.db_table) in table_names:
|
|
|
|
# Drop the table now
|
|
|
|
output.append('%s %s;' % (style.SQL_KEYWORD('DROP TABLE'),
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(model._meta.db_table))))
|
2007-08-20 10:20:33 +08:00
|
|
|
if connection.features.supports_constraints and model in references_to_delete:
|
2007-08-16 14:06:55 +08:00
|
|
|
for rel_class, f in references_to_delete[model]:
|
|
|
|
table = rel_class._meta.db_table
|
|
|
|
col = f.column
|
|
|
|
r_table = model._meta.db_table
|
|
|
|
r_col = model._meta.get_field(f.rel.field_name).column
|
|
|
|
r_name = '%s_refs_%s_%x' % (col, r_col, abs(hash((table, r_table))))
|
|
|
|
output.append('%s %s %s %s;' % \
|
|
|
|
(style.SQL_KEYWORD('ALTER TABLE'),
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(table)),
|
2007-08-20 07:07:34 +08:00
|
|
|
style.SQL_KEYWORD(connection.ops.drop_foreignkey_sql()),
|
2007-08-20 07:53:39 +08:00
|
|
|
style.SQL_FIELD(truncate_name(r_name, connection.ops.max_name_length()))))
|
2007-08-16 14:06:55 +08:00
|
|
|
del references_to_delete[model]
|
2007-08-20 11:08:32 +08:00
|
|
|
if model._meta.has_auto_field:
|
|
|
|
ds = connection.ops.drop_sequence_sql(model._meta.db_table)
|
|
|
|
if ds:
|
|
|
|
output.append(ds)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Output DROP TABLE statements for many-to-many tables.
|
|
|
|
for model in app_models:
|
|
|
|
opts = model._meta
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in opts.local_many_to_many:
|
2008-07-31 15:53:55 +08:00
|
|
|
if not f.creates_table:
|
2007-12-17 14:02:35 +08:00
|
|
|
continue
|
2007-08-16 14:06:55 +08:00
|
|
|
if cursor and table_name_converter(f.m2m_db_table()) in table_names:
|
|
|
|
output.append("%s %s;" % (style.SQL_KEYWORD('DROP TABLE'),
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(f.m2m_db_table()))))
|
2007-08-20 11:08:32 +08:00
|
|
|
ds = connection.ops.drop_sequence_sql("%s_%s" % (model._meta.db_table, f.column))
|
|
|
|
if ds:
|
|
|
|
output.append(ds)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
app_label = app_models[0]._meta.app_label
|
|
|
|
|
|
|
|
# Close database connection explicitly, in case this output is being piped
|
|
|
|
# directly into a database client, to avoid locking issues.
|
|
|
|
if cursor:
|
|
|
|
cursor.close()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
return output[::-1] # Reverse it, to deal with table dependencies.
|
|
|
|
|
|
|
|
def sql_reset(app, style):
|
|
|
|
"Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module."
|
|
|
|
return sql_delete(app, style) + sql_all(app, style)
|
|
|
|
|
2007-08-26 03:32:30 +08:00
|
|
|
def sql_flush(style, only_django=False):
|
|
|
|
"""
|
|
|
|
Returns a list of the SQL statements used to flush the database.
|
|
|
|
|
|
|
|
If only_django is True, then only table names that have associated Django
|
|
|
|
models and are in INSTALLED_APPS will be included.
|
|
|
|
"""
|
2007-08-20 08:15:53 +08:00
|
|
|
from django.db import connection
|
2007-08-26 03:32:30 +08:00
|
|
|
if only_django:
|
2008-05-31 09:01:17 +08:00
|
|
|
tables = django_table_names()
|
2007-08-26 03:32:30 +08:00
|
|
|
else:
|
2008-05-31 09:01:17 +08:00
|
|
|
tables = table_names()
|
2007-08-26 03:32:30 +08:00
|
|
|
statements = connection.ops.sql_flush(style, tables, sequence_list())
|
2007-08-16 14:06:55 +08:00
|
|
|
return statements
|
|
|
|
|
2008-07-29 13:53:44 +08:00
|
|
|
def sql_custom(app, style):
|
2007-08-16 14:06:55 +08:00
|
|
|
"Returns a list of the custom table modifying SQL statements for the given app."
|
|
|
|
from django.db.models import get_models
|
|
|
|
output = []
|
|
|
|
|
|
|
|
app_models = get_models(app)
|
|
|
|
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
|
|
|
|
|
|
|
|
for model in app_models:
|
2008-07-29 13:53:44 +08:00
|
|
|
output.extend(custom_sql_for_model(model, style))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def sql_indexes(app, style):
|
|
|
|
"Returns a list of the CREATE INDEX SQL statements for all models in the given app."
|
|
|
|
from django.db import models
|
|
|
|
output = []
|
|
|
|
for model in models.get_models(app):
|
|
|
|
output.extend(sql_indexes_for_model(model, style))
|
|
|
|
return output
|
|
|
|
|
|
|
|
def sql_all(app, style):
|
|
|
|
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
|
2008-07-29 13:53:44 +08:00
|
|
|
return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
def sql_model_create(model, style, known_models=set()):
|
|
|
|
"""
|
|
|
|
Returns the SQL required to create a single model, as a tuple of:
|
|
|
|
(list_of_sql, pending_references_dict)
|
|
|
|
"""
|
2007-08-20 10:28:40 +08:00
|
|
|
from django.db import connection, models
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
opts = model._meta
|
|
|
|
final_output = []
|
|
|
|
table_output = []
|
|
|
|
pending_references = {}
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
2007-11-04 13:05:24 +08:00
|
|
|
inline_references = connection.features.inline_fk_references
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in opts.local_fields:
|
2007-08-16 14:06:55 +08:00
|
|
|
col_type = f.db_type()
|
|
|
|
tablespace = f.db_tablespace or opts.db_tablespace
|
|
|
|
if col_type is None:
|
|
|
|
# Skip ManyToManyFields, because they're not represented as
|
|
|
|
# database columns in this table.
|
|
|
|
continue
|
|
|
|
# Make the definition (e.g. 'foo VARCHAR(30)') for this field.
|
2007-08-20 09:03:33 +08:00
|
|
|
field_output = [style.SQL_FIELD(qn(f.column)),
|
2007-08-16 14:06:55 +08:00
|
|
|
style.SQL_COLTYPE(col_type)]
|
|
|
|
field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
|
|
|
|
if f.primary_key:
|
|
|
|
field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
|
2008-06-30 12:46:59 +08:00
|
|
|
elif f.unique:
|
|
|
|
field_output.append(style.SQL_KEYWORD('UNIQUE'))
|
|
|
|
if tablespace and connection.features.supports_tablespaces and f.unique:
|
2007-08-16 14:06:55 +08:00
|
|
|
# We must specify the index tablespace inline, because we
|
|
|
|
# won't be generating a CREATE INDEX statement for this field.
|
2007-08-20 08:30:19 +08:00
|
|
|
field_output.append(connection.ops.tablespace_sql(tablespace, inline=True))
|
2007-08-16 14:06:55 +08:00
|
|
|
if f.rel:
|
2007-11-04 13:05:24 +08:00
|
|
|
if inline_references and f.rel.to in known_models:
|
2007-08-16 14:06:55 +08:00
|
|
|
field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(f.rel.to._meta.db_table)) + ' (' + \
|
|
|
|
style.SQL_FIELD(qn(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' +
|
2007-08-20 07:03:38 +08:00
|
|
|
connection.ops.deferrable_sql()
|
2007-08-16 14:06:55 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# We haven't yet created the table to which this field
|
|
|
|
# is related, so save it for later.
|
|
|
|
pr = pending_references.setdefault(f.rel.to, []).append((model, f))
|
|
|
|
table_output.append(' '.join(field_output))
|
|
|
|
if opts.order_with_respect_to:
|
2007-08-20 09:03:33 +08:00
|
|
|
table_output.append(style.SQL_FIELD(qn('_order')) + ' ' + \
|
2007-08-16 14:06:55 +08:00
|
|
|
style.SQL_COLTYPE(models.IntegerField().db_type()) + ' ' + \
|
|
|
|
style.SQL_KEYWORD('NULL'))
|
|
|
|
for field_constraints in opts.unique_together:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
table_output.append(style.SQL_KEYWORD('UNIQUE') + ' (%s)' % \
|
2008-01-05 04:26:13 +08:00
|
|
|
", ".join([style.SQL_FIELD(qn(opts.get_field(f).column)) for f in field_constraints]))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-08-20 09:03:33 +08:00
|
|
|
full_statement = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + style.SQL_TABLE(qn(opts.db_table)) + ' (']
|
2007-08-16 14:06:55 +08:00
|
|
|
for i, line in enumerate(table_output): # Combine and add commas.
|
|
|
|
full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or ''))
|
|
|
|
full_statement.append(')')
|
2007-08-20 10:20:33 +08:00
|
|
|
if opts.db_tablespace and connection.features.supports_tablespaces:
|
2007-08-20 08:30:19 +08:00
|
|
|
full_statement.append(connection.ops.tablespace_sql(opts.db_tablespace))
|
2007-08-16 14:06:55 +08:00
|
|
|
full_statement.append(';')
|
|
|
|
final_output.append('\n'.join(full_statement))
|
|
|
|
|
2007-08-20 06:29:57 +08:00
|
|
|
if opts.has_auto_field:
|
|
|
|
# Add any extra SQL needed to support auto-incrementing primary keys.
|
2007-09-15 02:12:36 +08:00
|
|
|
auto_column = opts.auto_field.db_column or opts.auto_field.name
|
|
|
|
autoinc_sql = connection.ops.autoinc_sql(opts.db_table, auto_column)
|
2007-08-16 14:06:55 +08:00
|
|
|
if autoinc_sql:
|
|
|
|
for stmt in autoinc_sql:
|
|
|
|
final_output.append(stmt)
|
|
|
|
|
|
|
|
return final_output, pending_references
|
|
|
|
|
|
|
|
def sql_for_pending_references(model, style, pending_references):
|
|
|
|
"""
|
|
|
|
Returns any ALTER TABLE statements to add constraints after the fact.
|
|
|
|
"""
|
2007-08-20 10:28:40 +08:00
|
|
|
from django.db import connection
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.db.backends.util import truncate_name
|
|
|
|
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
2007-08-16 14:06:55 +08:00
|
|
|
final_output = []
|
2007-08-20 10:20:33 +08:00
|
|
|
if connection.features.supports_constraints:
|
2007-08-16 14:06:55 +08:00
|
|
|
opts = model._meta
|
|
|
|
if model in pending_references:
|
|
|
|
for rel_class, f in pending_references[model]:
|
|
|
|
rel_opts = rel_class._meta
|
|
|
|
r_table = rel_opts.db_table
|
|
|
|
r_col = f.column
|
|
|
|
table = opts.db_table
|
|
|
|
col = opts.get_field(f.rel.field_name).column
|
|
|
|
# For MySQL, r_name must be unique in the first 64 characters.
|
|
|
|
# So we are careful with character usage here.
|
|
|
|
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;' % \
|
2007-08-20 09:03:33 +08:00
|
|
|
(qn(r_table), truncate_name(r_name, connection.ops.max_name_length()),
|
|
|
|
qn(r_col), qn(table), qn(col),
|
2007-08-20 07:03:38 +08:00
|
|
|
connection.ops.deferrable_sql()))
|
2007-08-16 14:06:55 +08:00
|
|
|
del pending_references[model]
|
|
|
|
return final_output
|
|
|
|
|
|
|
|
def many_to_many_sql_for_model(model, style):
|
2007-08-20 10:28:40 +08:00
|
|
|
from django.db import connection, models
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.contrib.contenttypes import generic
|
2007-11-04 13:05:24 +08:00
|
|
|
from django.db.backends.util import truncate_name
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
opts = model._meta
|
|
|
|
final_output = []
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
2007-11-04 13:05:24 +08:00
|
|
|
inline_references = connection.features.inline_fk_references
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in opts.local_many_to_many:
|
2008-07-29 20:41:08 +08:00
|
|
|
if f.creates_table:
|
2007-08-16 14:06:55 +08:00
|
|
|
tablespace = f.db_tablespace or opts.db_tablespace
|
2008-06-30 12:46:59 +08:00
|
|
|
if tablespace and connection.features.supports_tablespaces:
|
2007-08-20 08:30:19 +08:00
|
|
|
tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace, inline=True)
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
tablespace_sql = ''
|
|
|
|
table_output = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + \
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(f.m2m_db_table())) + ' (']
|
2007-11-04 13:05:24 +08:00
|
|
|
table_output.append(' %s %s %s%s,' %
|
2007-08-20 09:03:33 +08:00
|
|
|
(style.SQL_FIELD(qn('id')),
|
2007-08-16 14:06:55 +08:00
|
|
|
style.SQL_COLTYPE(models.AutoField(primary_key=True).db_type()),
|
|
|
|
style.SQL_KEYWORD('NOT NULL PRIMARY KEY'),
|
|
|
|
tablespace_sql))
|
2007-11-04 13:05:24 +08:00
|
|
|
if inline_references:
|
|
|
|
deferred = []
|
|
|
|
table_output.append(' %s %s %s %s (%s)%s,' %
|
|
|
|
(style.SQL_FIELD(qn(f.m2m_column_name())),
|
|
|
|
style.SQL_COLTYPE(models.ForeignKey(model).db_type()),
|
|
|
|
style.SQL_KEYWORD('NOT NULL REFERENCES'),
|
|
|
|
style.SQL_TABLE(qn(opts.db_table)),
|
|
|
|
style.SQL_FIELD(qn(opts.pk.column)),
|
|
|
|
connection.ops.deferrable_sql()))
|
|
|
|
table_output.append(' %s %s %s %s (%s)%s,' %
|
|
|
|
(style.SQL_FIELD(qn(f.m2m_reverse_name())),
|
|
|
|
style.SQL_COLTYPE(models.ForeignKey(f.rel.to).db_type()),
|
|
|
|
style.SQL_KEYWORD('NOT NULL REFERENCES'),
|
|
|
|
style.SQL_TABLE(qn(f.rel.to._meta.db_table)),
|
|
|
|
style.SQL_FIELD(qn(f.rel.to._meta.pk.column)),
|
|
|
|
connection.ops.deferrable_sql()))
|
|
|
|
else:
|
|
|
|
table_output.append(' %s %s %s,' %
|
|
|
|
(style.SQL_FIELD(qn(f.m2m_column_name())),
|
|
|
|
style.SQL_COLTYPE(models.ForeignKey(model).db_type()),
|
|
|
|
style.SQL_KEYWORD('NOT NULL')))
|
|
|
|
table_output.append(' %s %s %s,' %
|
|
|
|
(style.SQL_FIELD(qn(f.m2m_reverse_name())),
|
|
|
|
style.SQL_COLTYPE(models.ForeignKey(f.rel.to).db_type()),
|
|
|
|
style.SQL_KEYWORD('NOT NULL')))
|
|
|
|
deferred = [
|
|
|
|
(f.m2m_db_table(), f.m2m_column_name(), opts.db_table,
|
|
|
|
opts.pk.column),
|
|
|
|
( f.m2m_db_table(), f.m2m_reverse_name(),
|
|
|
|
f.rel.to._meta.db_table, f.rel.to._meta.pk.column)
|
|
|
|
]
|
|
|
|
table_output.append(' %s (%s, %s)%s' %
|
2007-08-16 14:06:55 +08:00
|
|
|
(style.SQL_KEYWORD('UNIQUE'),
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_FIELD(qn(f.m2m_column_name())),
|
|
|
|
style.SQL_FIELD(qn(f.m2m_reverse_name())),
|
2007-08-16 14:06:55 +08:00
|
|
|
tablespace_sql))
|
|
|
|
table_output.append(')')
|
2007-08-20 10:20:33 +08:00
|
|
|
if opts.db_tablespace and connection.features.supports_tablespaces:
|
2007-08-16 14:06:55 +08:00
|
|
|
# f.db_tablespace is only for indices, so ignore its value here.
|
2007-08-20 08:30:19 +08:00
|
|
|
table_output.append(connection.ops.tablespace_sql(opts.db_tablespace))
|
2007-08-16 14:06:55 +08:00
|
|
|
table_output.append(';')
|
|
|
|
final_output.append('\n'.join(table_output))
|
|
|
|
|
2007-11-04 13:05:24 +08:00
|
|
|
for r_table, r_col, table, col in deferred:
|
|
|
|
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;' %
|
|
|
|
(qn(r_table),
|
|
|
|
truncate_name(r_name, connection.ops.max_name_length()),
|
|
|
|
qn(r_col), qn(table), qn(col),
|
|
|
|
connection.ops.deferrable_sql()))
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# Add any extra SQL needed to support auto-incrementing PKs
|
2007-09-15 02:12:36 +08:00
|
|
|
autoinc_sql = connection.ops.autoinc_sql(f.m2m_db_table(), 'id')
|
2007-08-16 14:06:55 +08:00
|
|
|
if autoinc_sql:
|
|
|
|
for stmt in autoinc_sql:
|
|
|
|
final_output.append(stmt)
|
|
|
|
|
|
|
|
return final_output
|
|
|
|
|
2008-07-29 13:53:44 +08:00
|
|
|
def custom_sql_for_model(model, style):
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
opts = model._meta
|
|
|
|
app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
|
|
|
|
output = []
|
|
|
|
|
2008-07-29 13:53:44 +08:00
|
|
|
# Post-creation SQL should come before any initial SQL data is loaded.
|
|
|
|
# However, this should not be done for fields that are part of a a parent
|
|
|
|
# model (via model inheritance).
|
|
|
|
nm = opts.init_name_map()
|
|
|
|
post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
|
|
|
|
for f in post_sql_fields:
|
|
|
|
output.extend(f.post_create_sql(style, model._meta.db_table))
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# Some backends can't execute more than one SQL statement at a time,
|
|
|
|
# so split into separate statements.
|
|
|
|
statements = re.compile(r";[ \t]*$", re.M)
|
|
|
|
|
|
|
|
# Find custom SQL, if it's available.
|
|
|
|
sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)),
|
|
|
|
os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
|
|
|
|
for sql_file in sql_files:
|
|
|
|
if os.path.exists(sql_file):
|
|
|
|
fp = open(sql_file, 'U')
|
|
|
|
for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
|
|
|
|
# Remove any comments from the file
|
2008-06-08 13:38:13 +08:00
|
|
|
statement = re.sub(ur"--.*([\n\Z]|$)", "", statement)
|
2007-08-16 14:06:55 +08:00
|
|
|
if statement.strip():
|
|
|
|
output.append(statement + u";")
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def sql_indexes_for_model(model, style):
|
|
|
|
"Returns the CREATE INDEX SQL statements for a single model"
|
2007-08-20 10:28:40 +08:00
|
|
|
from django.db import connection
|
2007-08-16 14:06:55 +08:00
|
|
|
output = []
|
|
|
|
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in model._meta.local_fields:
|
2008-06-30 12:46:59 +08:00
|
|
|
if f.db_index and not f.unique:
|
2007-08-16 14:06:55 +08:00
|
|
|
tablespace = f.db_tablespace or model._meta.db_tablespace
|
2007-08-20 10:20:33 +08:00
|
|
|
if tablespace and connection.features.supports_tablespaces:
|
2007-08-20 08:30:19 +08:00
|
|
|
tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace)
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
tablespace_sql = ''
|
|
|
|
output.append(
|
2008-06-30 12:46:59 +08:00
|
|
|
style.SQL_KEYWORD('CREATE INDEX') + ' ' + \
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
|
2007-08-16 14:06:55 +08:00
|
|
|
style.SQL_KEYWORD('ON') + ' ' + \
|
2007-08-20 09:03:33 +08:00
|
|
|
style.SQL_TABLE(qn(model._meta.db_table)) + ' ' + \
|
|
|
|
"(%s)" % style.SQL_FIELD(qn(f.column)) + \
|
2007-08-16 14:06:55 +08:00
|
|
|
"%s;" % tablespace_sql
|
|
|
|
)
|
|
|
|
return output
|
|
|
|
|
|
|
|
def emit_post_sync_signal(created_models, verbosity, interactive):
|
|
|
|
from django.db import models
|
|
|
|
from django.dispatch import dispatcher
|
|
|
|
# Emit the post_sync signal for every application.
|
|
|
|
for app in models.get_apps():
|
|
|
|
app_name = app.__name__.split('.')[-2]
|
|
|
|
if verbosity >= 2:
|
|
|
|
print "Running post-sync handlers for application", app_name
|
2008-08-06 23:32:46 +08:00
|
|
|
models.signals.post_syncdb.send(sender=app, app=app,
|
|
|
|
created_models=created_models, verbosity=verbosity,
|
|
|
|
interactive=interactive)
|