| 1 |
from django.core.management.base import AppCommand |
| 2 |
from django.db import DEFAULT_DB_ALIAS, connections |
| 3 |
|
| 4 |
|
| 5 |
class Command(AppCommand): |
| 6 |
help = ( |
| 7 |
"Prints the SQL statements for resetting sequences for the given app name(s)." |
| 8 |
) |
| 9 |
|
| 10 |
output_transaction = True |
| 11 |
|
| 12 |
def add_arguments(self, parser): |
| 13 |
super().add_arguments(parser) |
| 14 |
parser.add_argument( |
| 15 |
"--database", |
| 16 |
default=DEFAULT_DB_ALIAS, |
| 17 |
choices=tuple(connections), |
| 18 |
help=( |
| 19 |
'Nominates a database to print the SQL for. Defaults to the "default" ' |
| 20 |
"database." |
| 21 |
), |
| 22 |
) |
| 23 |
|
| 24 |
def handle_app_config(self, app_config, **options): |
| 25 |
if app_config.models_module is None: |
| 26 |
return |
| 27 |
connection = connections[options["database"]] |
| 28 |
models = app_config.get_models(include_auto_created=True) |
| 29 |
statements = connection.ops.sequence_reset_sql(self.style, models) |
| 30 |
if not statements and options["verbosity"] >= 1: |
| 31 |
self.stderr.write("No sequences found.") |
| 32 |
return "\n".join(statements) |