Command
This class provides a command-line interface for printing the SQL statements associated with a specific migration. It supports generating SQL for both applying and unapplying migrations across different database aliases. The class handles migration resolution, validates application labels, and automatically wraps output in transaction blocks if supported by the database.
Attributes
| Attribute | Type | Description |
|---|---|---|
| help | string = "Prints the SQL statements for the named migration." | Prints the SQL statements for the named migration. |
| output_transaction | boolean = true | Determines whether to wrap the generated SQL in BEGIN and COMMIT statements for atomic migrations on supported databases. |
Methods
add_arguments()
@classmethod
def add_arguments(
parser: ArgumentParser
)
Configures the command-line argument parser with required migration identifiers and optional database or direction flags.
Parameters
| Name | Type | Description |
|---|---|---|
| parser | ArgumentParser | The argument parser instance used to define the command's CLI interface and help text. |
execute()
@classmethod
def execute(
*args: tuple,
**options: dict
) - > str
Executes the command logic while ensuring that transaction keywords in the output remain colorless to match the SQL body.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | tuple | Variable length argument list passed to the base execution logic. |
| **options | dict | Named command-line options including database selection and migration targets. |
Returns
| Type | Description |
|---|---|
str | The generated SQL statements as a single string. |
handle()
@classmethod
def handle(
*args: tuple,
**options: dict
) - > str
Performs the core logic of resolving the specified migration and generating its corresponding SQL for the target database.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | tuple | Positional arguments passed from the command line. |
| **options | dict | Configuration options containing 'app_label', 'migration_name', 'database', and 'backwards' flag. |
Returns
| Type | Description |
|---|---|
str | A newline-joined string of SQL statements required to apply or unapply the migration. |