mirror of https://github.com/django/django.git
Changed sql_* to use an app config instead of a models module.
This commit is contained in:
parent
38ce41103f
commit
1d4bcb86ea
|
@ -22,5 +22,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_create(app_config.models_module, self.style, connection)
|
statements = sql_create(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -22,5 +22,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_all(app_config.models_module, self.style, connection)
|
statements = sql_all(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -22,5 +22,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_delete(app_config.models_module, self.style, connection)
|
statements = sql_delete(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -22,5 +22,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_custom(app_config.models_module, self.style, connection)
|
statements = sql_custom(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -23,5 +23,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_destroy_indexes(app_config.models_module, self.style, connection)
|
statements = sql_destroy_indexes(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -23,5 +23,5 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
statements = sql_indexes(app_config.models_module, self.style, connection)
|
statements = sql_indexes(app_config, self.style, connection)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -11,7 +11,7 @@ from django.core.management.base import CommandError
|
||||||
from django.db import models, router
|
from django.db import models, router
|
||||||
|
|
||||||
|
|
||||||
def sql_create(app, style, connection):
|
def sql_create(app_config, style, connection):
|
||||||
"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."
|
||||||
|
|
||||||
if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy':
|
if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy':
|
||||||
|
@ -25,13 +25,13 @@ def sql_create(app, style, connection):
|
||||||
# We trim models from the current app so that the sqlreset command does not
|
# 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
|
# generate invalid SQL (leaving models out of known_models is harmless, so
|
||||||
# we can be conservative).
|
# we can be conservative).
|
||||||
app_models = apps.get_models(app, include_auto_created=True)
|
app_models = app_config.get_models(include_auto_created=True)
|
||||||
final_output = []
|
final_output = []
|
||||||
tables = connection.introspection.table_names()
|
tables = connection.introspection.table_names()
|
||||||
known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models)
|
known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models)
|
||||||
pending_references = {}
|
pending_references = {}
|
||||||
|
|
||||||
for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
|
for model in router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True):
|
||||||
output, references = connection.creation.sql_create_model(model, style, known_models)
|
output, references = connection.creation.sql_create_model(model, style, known_models)
|
||||||
final_output.extend(output)
|
final_output.extend(output)
|
||||||
for refto, refs in references.items():
|
for refto, refs in references.items():
|
||||||
|
@ -57,7 +57,7 @@ def sql_create(app, style, connection):
|
||||||
return final_output
|
return final_output
|
||||||
|
|
||||||
|
|
||||||
def sql_delete(app, style, connection):
|
def sql_delete(app_config, style, connection):
|
||||||
"Returns a list of the DROP TABLE SQL statements for the given app."
|
"Returns a list of the DROP TABLE SQL statements for the given app."
|
||||||
|
|
||||||
# This should work even if a connection isn't available
|
# This should work even if a connection isn't available
|
||||||
|
@ -78,7 +78,7 @@ def sql_delete(app, style, connection):
|
||||||
to_delete = set()
|
to_delete = set()
|
||||||
|
|
||||||
references_to_delete = {}
|
references_to_delete = {}
|
||||||
app_models = router.get_migratable_models(app, connection.alias, include_auto_created=True)
|
app_models = router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True)
|
||||||
for model in app_models:
|
for model in app_models:
|
||||||
if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names:
|
if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names:
|
||||||
# The table exists, so it needs to be dropped
|
# The table exists, so it needs to be dropped
|
||||||
|
@ -118,11 +118,11 @@ def sql_flush(style, connection, only_django=False, reset_sequences=True, allow_
|
||||||
return statements
|
return statements
|
||||||
|
|
||||||
|
|
||||||
def sql_custom(app, style, connection):
|
def sql_custom(app_config, style, connection):
|
||||||
"Returns a list of the custom table modifying SQL statements for the given app."
|
"Returns a list of the custom table modifying SQL statements for the given app."
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
app_models = router.get_migratable_models(app, connection.alias)
|
app_models = router.get_migratable_models(app_config.models_module, connection.alias)
|
||||||
|
|
||||||
for model in app_models:
|
for model in app_models:
|
||||||
output.extend(custom_sql_for_model(model, style, connection))
|
output.extend(custom_sql_for_model(model, style, connection))
|
||||||
|
@ -130,25 +130,25 @@ def sql_custom(app, style, connection):
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def sql_indexes(app, 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."
|
"Returns a list of the CREATE INDEX SQL statements for all models in the given app."
|
||||||
output = []
|
output = []
|
||||||
for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
|
for model in router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True):
|
||||||
output.extend(connection.creation.sql_indexes_for_model(model, style))
|
output.extend(connection.creation.sql_indexes_for_model(model, style))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def sql_destroy_indexes(app, 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."
|
"Returns a list of the DROP INDEX SQL statements for all models in the given app."
|
||||||
output = []
|
output = []
|
||||||
for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
|
for model in router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True):
|
||||||
output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
|
output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def sql_all(app, style, connection):
|
def sql_all(app_config, style, connection):
|
||||||
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
|
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
|
||||||
return sql_create(app, style, connection) + sql_custom(app, style, connection) + sql_indexes(app, style, connection)
|
return sql_create(app_config, style, connection) + sql_custom(app_config, style, connection) + sql_indexes(app_config, style, connection)
|
||||||
|
|
||||||
|
|
||||||
def _split_statements(content):
|
def _split_statements(content):
|
||||||
|
|
|
@ -17,8 +17,8 @@ class SQLCommandsTestCase(TestCase):
|
||||||
return len([o for o in output if o.startswith(cmd)])
|
return len([o for o in output if o.startswith(cmd)])
|
||||||
|
|
||||||
def test_sql_create(self):
|
def test_sql_create(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
create_tables = [o for o in output if o.startswith('CREATE TABLE')]
|
create_tables = [o for o in output if o.startswith('CREATE TABLE')]
|
||||||
self.assertEqual(len(create_tables), 3)
|
self.assertEqual(len(create_tables), 3)
|
||||||
# Lower so that Oracle's upper case tbl names wont break
|
# Lower so that Oracle's upper case tbl names wont break
|
||||||
|
@ -26,8 +26,8 @@ class SQLCommandsTestCase(TestCase):
|
||||||
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
||||||
|
|
||||||
def test_sql_delete(self):
|
def test_sql_delete(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
drop_tables = [o for o in output if o.startswith('DROP TABLE')]
|
drop_tables = [o for o in output if o.startswith('DROP TABLE')]
|
||||||
self.assertEqual(len(drop_tables), 3)
|
self.assertEqual(len(drop_tables), 3)
|
||||||
# Lower so that Oracle's upper case tbl names wont break
|
# Lower so that Oracle's upper case tbl names wont break
|
||||||
|
@ -35,20 +35,20 @@ class SQLCommandsTestCase(TestCase):
|
||||||
six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
|
six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
|
||||||
|
|
||||||
def test_sql_indexes(self):
|
def test_sql_indexes(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
# PostgreSQL creates one additional index for CharField
|
# PostgreSQL creates one additional index for CharField
|
||||||
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
||||||
|
|
||||||
def test_sql_destroy_indexes(self):
|
def test_sql_destroy_indexes(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_destroy_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_destroy_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
# PostgreSQL creates one additional index for CharField
|
# PostgreSQL creates one additional index for CharField
|
||||||
self.assertIn(self.count_ddl(output, 'DROP INDEX'), [3, 4])
|
self.assertIn(self.count_ddl(output, 'DROP INDEX'), [3, 4])
|
||||||
|
|
||||||
def test_sql_all(self):
|
def test_sql_all(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_all(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_all(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
|
|
||||||
self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
|
self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
|
||||||
# PostgreSQL creates one additional index for CharField
|
# PostgreSQL creates one additional index for CharField
|
||||||
|
@ -69,8 +69,8 @@ class SQLCommandsRouterTestCase(TestCase):
|
||||||
router.routers = self._old_routers
|
router.routers = self._old_routers
|
||||||
|
|
||||||
def test_router_honored(self):
|
def test_router_honored(self):
|
||||||
app = apps.get_app_config('commands_sql').models_module
|
app_config = apps.get_app_config('commands_sql')
|
||||||
for sql_command in (sql_all, sql_create, sql_delete, sql_indexes, sql_destroy_indexes):
|
for sql_command in (sql_all, sql_create, sql_delete, sql_indexes, sql_destroy_indexes):
|
||||||
output = sql_command(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_command(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
self.assertEqual(len(output), 0,
|
self.assertEqual(len(output), 0,
|
||||||
"%s command is not honoring routers" % sql_command.__name__)
|
"%s command is not honoring routers" % sql_command.__name__)
|
||||||
|
|
Loading…
Reference in New Issue