Fixed #30595 -- Added error message when no objects found to sql* management commands.

This commit is contained in:
Hasan Ramezani 2019-06-30 01:35:48 +02:00 committed by Mariusz Felisiak
parent c2f381ef17
commit 5d03f2bc01
9 changed files with 38 additions and 1 deletions

View File

@ -19,4 +19,7 @@ class Command(BaseCommand):
) )
def handle(self, **options): def handle(self, **options):
return '\n'.join(sql_flush(self.style, connections[options['database']], only_django=True)) sql_statements = sql_flush(self.style, connections[options['database']], only_django=True)
if not sql_statements and options['verbosity'] >= 1:
self.stderr.write('No tables found.')
return '\n'.join(sql_statements)

View File

@ -63,4 +63,6 @@ class Command(BaseCommand):
# for it # for it
plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])] plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])]
sql_statements = executor.collect_sql(plan) sql_statements = executor.collect_sql(plan)
if not sql_statements and options['verbosity'] >= 1:
self.stderr.write('No operations found.')
return '\n'.join(sql_statements) return '\n'.join(sql_statements)

View File

@ -20,4 +20,6 @@ class Command(AppCommand):
connection = connections[options['database']] connection = connections[options['database']]
models = app_config.get_models(include_auto_created=True) models = app_config.get_models(include_auto_created=True)
statements = connection.ops.sequence_reset_sql(self.style, models) statements = connection.ops.sequence_reset_sql(self.style, models)
if not statements and options['verbosity'] >= 1:
self.stderr.write('No sequences found.')
return '\n'.join(statements) return '\n'.join(statements)

View File

View File

View File

@ -0,0 +1,18 @@
import io
from django.core.management import call_command
from django.test import TestCase
class CoreCommandsNoOutputTests(TestCase):
available_apps = ['empty_models']
def test_sqlflush_no_tables(self):
err = io.StringIO()
call_command('sqlflush', stderr=err)
self.assertEqual(err.getvalue(), 'No tables found.\n')
def test_sqlsequencereset_no_sequences(self):
err = io.StringIO()
call_command('sqlsequencereset', 'empty_models', stderr=err)
self.assertEqual(err.getvalue(), 'No sequences found.\n')

View File

@ -645,6 +645,12 @@ class MigrateTests(MigrationTestBase):
self.assertNotIn(start_transaction_sql.lower(), queries) self.assertNotIn(start_transaction_sql.lower(), queries)
self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries) self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries)
@override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations_no_operations'})
def test_migrations_no_operations(self):
err = io.StringIO()
call_command('sqlmigrate', 'migrations', '0001_initial', stderr=err)
self.assertEqual(err.getvalue(), 'No operations found.\n')
@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,6 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = []
operations = []