| 1 |
from django.core.management.base import BaseCommand |
| 2 |
from django.core.management.sql import sql_flush |
| 3 |
from django.db import DEFAULT_DB_ALIAS, connections |
| 4 |
|
| 5 |
|
| 6 |
class Command(BaseCommand): |
| 7 |
help = ( |
| 8 |
"Returns a list of the SQL statements required to return all tables in " |
| 9 |
"the database to the state they were in just after they were installed." |
| 10 |
) |
| 11 |
|
| 12 |
output_transaction = True |
| 13 |
|
| 14 |
def add_arguments(self, parser): |
| 15 |
super().add_arguments(parser) |
| 16 |
parser.add_argument( |
| 17 |
"--database", |
| 18 |
default=DEFAULT_DB_ALIAS, |
| 19 |
choices=tuple(connections), |
| 20 |
help=( |
| 21 |
'Nominates a database to print the SQL for. Defaults to the "default" ' |
| 22 |
"database." |
| 23 |
), |
| 24 |
) |
| 25 |
|
| 26 |
def handle(self, **options): |
| 27 |
sql_statements = sql_flush(self.style, connections[options["database"]]) |
| 28 |
if not sql_statements and options["verbosity"] >= 1: |
| 29 |
self.stderr.write("No tables found.") |
| 30 |
return "\n".join(sql_statements) |