This class updates the database schema by managing applications with and without migrations. It provides functionality to apply, unapply, or fake migrations, and can synchronize the state of specific applications or the entire project. Key features include the ability to plan migration actions, detect conflicts in the migration graph, and prune nonexistent migrations from the database record.
Attributes
| Attribute | Type | Description |
|---|
| autodetector | class = MigrationAutodetector | The class used to detect differences between the current project state and the migration graph. |
| help | string = "Updates database schema. Manages both apps with migrations and those without." | Updates database schema. Manages both apps with migrations and those without. |
Constructor
Signature
Methods
add_arguments()
@classmethod
def add_arguments(
parser: [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)
)
Defines the command-line arguments for the migrate command, including app labels, migration names, and flags for faking or planning.
Parameters
| Name | Type | Description |
|---|
| parser | [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser) | The argument parser instance used to register command-line options and positional arguments. |
get_check_kwargs()
@classmethod
def get_check_kwargs(
options: dict
) - > dict
Builds the keyword arguments for the system check framework, ensuring only the targeted database is validated.
Parameters
| Name | Type | Description |
|---|
| options | dict | The parsed command-line options containing the database alias. |
Returns
| Type | Description |
|---|
dict | A dictionary containing system check configuration, specifically limiting the check to the selected database. |
handle()
@classmethod
def handle(
*args: tuple,
**options: dict
)
Executes the migration logic, managing database connections, detecting conflicts, and applying or unapplying migrations based on the provided options.
Parameters
| Name | Type | Description |
|---|
| *args | tuple | Variable length argument list. |
| **options | dict | The command-line options that control behavior such as the target app, database, and whether to run in interactive mode. |
migration_progress_callback()
@classmethod
def migration_progress_callback(
action: str,
migration: [Migration](../../../../db/migrations/migration/migration.md?sid=django_db_migrations_migration_migration),
fake: bool
)
Logs the progress of migration operations to the console, including start and success messages for applying, unapplying, and rendering states.
Parameters
| Name | Type | Description |
|---|
| action | str | The current stage of the migration process (e.g., 'apply_start', 'render_success'). |
| migration | [Migration](../../../../db/migrations/migration/migration.md?sid=django_db_migrations_migration_migration) | The migration object currently being processed. |
| fake | bool | Indicates if the migration is being marked as applied without executing its SQL. |
sync_apps()
@classmethod
def sync_apps(
connection: [BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper),
app_labels: list
)
Run the old syncdb-style operation on a list of app_labels.
Parameters
| Name | Type | Description |
|---|
| connection | [BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper) | The database connection object used to inspect existing tables and create new ones. |
| app_labels | list | A list of application labels that do not have migrations and require manual table creation. |
describe_operation()
@classmethod
def describe_operation(
operation: [Operation](../../../../db/migrations/operations/base/operation.md?sid=django_db_migrations_operations_base_operation),
backwards: bool
) - > tuple
Return a string that describes a migration operation for --plan.
Parameters
| Name | Type | Description |
|---|
| operation | [Operation](../../../../db/migrations/operations/base/operation.md?sid=django_db_migrations_operations_base_operation) | The migration operation instance to be described. |
| backwards | bool | Whether the operation is being applied in reverse (unapplied). |
Returns
| Type | Description |
|---|
tuple | A tuple containing the descriptive string of the operation and a boolean indicating if the operation is irreversible. |