Fixed #33509 -- Added "(no-op)" to sqlmigrate output for operations without SQL statement.

This commit is contained in:
Adam Johnson 2022-02-10 20:16:33 +00:00 committed by Mariusz Felisiak
parent f15f7d395c
commit 6f453cd298
4 changed files with 63 additions and 11 deletions

View File

@ -103,15 +103,14 @@ class Migration:
# there instead # there instead
if collect_sql: if collect_sql:
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
if not operation.reduces_to_sql:
schema_editor.collected_sql.append(
"-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS "
"SQL:"
)
schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("-- %s" % operation.describe())
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
if not operation.reduces_to_sql: if not operation.reduces_to_sql:
schema_editor.collected_sql.append(
"-- THIS OPERATION CANNOT BE WRITTEN AS SQL"
)
continue continue
collected_sql_before = len(schema_editor.collected_sql)
# Save the state before the operation has run # Save the state before the operation has run
old_state = project_state.clone() old_state = project_state.clone()
operation.state_forwards(self.app_label, project_state) operation.state_forwards(self.app_label, project_state)
@ -131,6 +130,8 @@ class Migration:
operation.database_forwards( operation.database_forwards(
self.app_label, schema_editor, old_state, project_state self.app_label, schema_editor, old_state, project_state
) )
if collect_sql and collected_sql_before == len(schema_editor.collected_sql):
schema_editor.collected_sql.append("-- (no-op)")
return project_state return project_state
def unapply(self, project_state, schema_editor, collect_sql=False): def unapply(self, project_state, schema_editor, collect_sql=False):
@ -167,15 +168,14 @@ class Migration:
for operation, to_state, from_state in to_run: for operation, to_state, from_state in to_run:
if collect_sql: if collect_sql:
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
if not operation.reduces_to_sql:
schema_editor.collected_sql.append(
"-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS "
"SQL:"
)
schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("-- %s" % operation.describe())
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
if not operation.reduces_to_sql: if not operation.reduces_to_sql:
schema_editor.collected_sql.append(
"-- THIS OPERATION CANNOT BE WRITTEN AS SQL"
)
continue continue
collected_sql_before = len(schema_editor.collected_sql)
atomic_operation = operation.atomic or ( atomic_operation = operation.atomic or (
self.atomic and operation.atomic is not False self.atomic and operation.atomic is not False
) )
@ -191,6 +191,8 @@ class Migration:
operation.database_backwards( operation.database_backwards(
self.app_label, schema_editor, from_state, to_state self.app_label, schema_editor, from_state, to_state
) )
if collect_sql and collected_sql_before == len(schema_editor.collected_sql):
schema_editor.collected_sql.append("-- (no-op)")
return project_state return project_state
def suggest_name(self): def suggest_name(self):

View File

@ -1021,11 +1021,51 @@ class MigrateTests(MigrationTestBase):
@override_settings( @override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"} MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"}
) )
def test_migrations_no_operations(self): def test_sqlmigrate_no_operations(self):
err = io.StringIO() err = io.StringIO()
call_command("sqlmigrate", "migrations", "0001_initial", stderr=err) call_command("sqlmigrate", "migrations", "0001_initial", stderr=err)
self.assertEqual(err.getvalue(), "No operations found.\n") self.assertEqual(err.getvalue(), "No operations found.\n")
@override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_noop"}
)
def test_sqlmigrate_noop(self):
out = io.StringIO()
call_command("sqlmigrate", "migrations", "0001", stdout=out)
lines = out.getvalue().splitlines()
if connection.features.can_rollback_ddl:
lines = lines[1:-1]
self.assertEqual(
lines,
[
"--",
"-- Raw SQL operation",
"--",
"-- (no-op)",
],
)
@override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_manual_porting"}
)
def test_sqlmigrate_unrepresentable(self):
out = io.StringIO()
call_command("sqlmigrate", "migrations", "0002", stdout=out)
lines = out.getvalue().splitlines()
if connection.features.can_rollback_ddl:
lines = lines[1:-1]
self.assertEqual(
lines,
[
"--",
"-- Raw Python operation",
"--",
"-- THIS OPERATION CANNOT BE WRITTEN AS SQL",
],
)
@override_settings( @override_settings(
INSTALLED_APPS=[ INSTALLED_APPS=[
"migrations.migrations_test_apps.migrated_app", "migrations.migrations_test_apps.migrated_app",

View File

@ -0,0 +1,10 @@
from django.db import migrations
class Migration(migrations.Migration):
initial = True
operations = [
migrations.RunSQL(sql="", reverse_sql=""),
]