summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorVíðir Valberg Guðmundsson <valberg@orn.li>2014-05-29 23:03:10 +0200
committerAndrew Godwin <andrew@aeracode.org>2014-05-29 15:36:30 -0700
commitb6a960cd1df4a191fc2900bcaa76c73f2696fc4d (patch)
tree2c9274417fbb54a8230cf590bf4fd77fcc97b710 /django
parentc5afdc3d73945c8e9d45442dd488f413e67ce2aa (diff)
[1.7.x] Making SQL management commands migration aware.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/sqlsequencereset.py2
-rw-r--r--django/core/management/sql.py23
2 files changed, 25 insertions, 0 deletions
diff --git a/django/core/management/commands/sqlsequencereset.py b/django/core/management/commands/sqlsequencereset.py
index 9d25125efb..4f7ce4492a 100644
--- a/django/core/management/commands/sqlsequencereset.py
+++ b/django/core/management/commands/sqlsequencereset.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
from optparse import make_option
from django.core.management.base import AppCommand
+from django.core.management.sql import check_for_migrations
from django.db import connections, DEFAULT_DB_ALIAS
@@ -22,6 +23,7 @@ class Command(AppCommand):
if app_config.models_module is None:
return
connection = connections[options.get('database')]
+ check_for_migrations(app_config, connection)
models = app_config.get_models(include_auto_created=True)
statements = connection.ops.sequence_reset_sql(self.style, models)
return '\n'.join(statements)
diff --git a/django/core/management/sql.py b/django/core/management/sql.py
index 437f8737c3..02bc2603ce 100644
--- a/django/core/management/sql.py
+++ b/django/core/management/sql.py
@@ -9,12 +9,21 @@ from django.apps import apps
from django.conf import settings
from django.core.management.base import CommandError
from django.db import models, router
+from django.db.migrations.loader import MigrationLoader
from django.utils.deprecation import RemovedInDjango19Warning
+def check_for_migrations(app_config, connection):
+ loader = MigrationLoader(connection)
+ if app_config.label in loader.migrated_apps:
+ raise CommandError("App '%s' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations." % app_config.label)
+
+
def sql_create(app_config, style, connection):
"Returns a list of the CREATE TABLE SQL statements for the given app."
+ check_for_migrations(app_config, connection)
+
if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy':
# This must be the "dummy" database backend, which means the user
# hasn't set ENGINE for the database.
@@ -61,6 +70,8 @@ def sql_create(app_config, style, connection):
def sql_delete(app_config, style, connection):
"Returns a list of the DROP TABLE SQL statements for the given app."
+ check_for_migrations(app_config, connection)
+
# This should work even if a connection isn't available
try:
cursor = connection.cursor()
@@ -122,6 +133,9 @@ def sql_flush(style, connection, only_django=False, reset_sequences=True, allow_
def sql_custom(app_config, style, connection):
"Returns a list of the custom table modifying SQL statements for the given app."
+
+ check_for_migrations(app_config, connection)
+
output = []
app_models = router.get_migratable_models(app_config, connection.alias)
@@ -134,6 +148,9 @@ def sql_custom(app_config, style, connection):
def sql_indexes(app_config, style, connection):
"Returns a list of the CREATE INDEX SQL statements for all models in the given app."
+
+ check_for_migrations(app_config, connection)
+
output = []
for model in router.get_migratable_models(app_config, connection.alias, include_auto_created=True):
output.extend(connection.creation.sql_indexes_for_model(model, style))
@@ -142,6 +159,9 @@ def sql_indexes(app_config, style, connection):
def sql_destroy_indexes(app_config, style, connection):
"Returns a list of the DROP INDEX SQL statements for all models in the given app."
+
+ check_for_migrations(app_config, connection)
+
output = []
for model in router.get_migratable_models(app_config, connection.alias, include_auto_created=True):
output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
@@ -149,6 +169,9 @@ def sql_destroy_indexes(app_config, style, connection):
def sql_all(app_config, style, connection):
+
+ check_for_migrations(app_config, connection)
+
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
return sql_create(app_config, style, connection) + sql_custom(app_config, style, connection) + sql_indexes(app_config, style, connection)