Skip to main content

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

AttributeTypeDescription
helpstring = "Prints the SQL statements for the named migration."Prints the SQL statements for the named migration.
output_transactionboolean = trueDetermines 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

NameTypeDescription
parserArgumentParserThe 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

NameTypeDescription
*argstupleVariable length argument list passed to the base execution logic.
**optionsdictNamed command-line options including database selection and migration targets.

Returns

TypeDescription
strThe 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

NameTypeDescription
*argstuplePositional arguments passed from the command line.
**optionsdictConfiguration options containing 'app_label', 'migration_name', 'database', and 'backwards' flag.

Returns

TypeDescription
strA newline-joined string of SQL statements required to apply or unapply the migration.