Fixed #23379 -- Corrected a referencing issue in sql_create.
Thanks to Trac alias flakfizer for the report.
This commit is contained in:
parent
2db1ed1033
commit
1e404180c1
|
@ -36,7 +36,7 @@ def sql_create(app_config, 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 = app_config.get_models(include_auto_created=True)
|
app_models = list(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)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.core.management.color import no_style
|
from django.core.management.color import no_style
|
||||||
from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
|
from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
|
||||||
|
@ -19,11 +21,25 @@ class SQLCommandsTestCase(TestCase):
|
||||||
def test_sql_create(self):
|
def test_sql_create(self):
|
||||||
app_config = apps.get_app_config('commands_sql')
|
app_config = apps.get_app_config('commands_sql')
|
||||||
output = sql_create(app_config, 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')]
|
|
||||||
self.assertEqual(len(create_tables), 3)
|
tables = set()
|
||||||
# Lower so that Oracle's upper case tbl names wont break
|
create_table_re = re.compile(r'^create table .(?P<table>[\w_]+).*', re.IGNORECASE)
|
||||||
sql = create_tables[-1].lower()
|
reference_re = re.compile(r'.* references .(?P<table>[\w_]+).*', re.IGNORECASE)
|
||||||
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
for statement in output:
|
||||||
|
create_table = create_table_re.match(statement)
|
||||||
|
if create_table:
|
||||||
|
tables.add(create_table.group('table'))
|
||||||
|
continue
|
||||||
|
reference = reference_re.match(statement)
|
||||||
|
if reference:
|
||||||
|
table = reference.group('table')
|
||||||
|
self.assertIn(
|
||||||
|
table, tables, "The table %s is referenced before its creation." % table
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(tables, {
|
||||||
|
'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments'
|
||||||
|
})
|
||||||
|
|
||||||
def test_sql_delete(self):
|
def test_sql_delete(self):
|
||||||
app_config = apps.get_app_config('commands_sql')
|
app_config = apps.get_app_config('commands_sql')
|
||||||
|
|
Loading…
Reference in New Issue