Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from django.core.management.commands.makemigrations import Command as BaseCommand # noqa: E571
DEFAULT_MIGRATION_NAME = 'auto'
class Command(BaseCommand):
"""
Overrides default `makemigrations` command so we provide a fixed default
name for migrations. Instead of appending the "auto" + timestamp, we just
append "auto" or what is defined in `DEFAULT_MIGRATION_NAME`.
The reason for this change is to make migration names deterministic, so
conflicts or migration overlaps can be detected and blocked by Git, until
conflicts are resolved.
This is essentially useful for our specific workflow of "develop" branch
being always in a deployable state. Without this change, it would be
possible for two different PRs merge into "develop" without notice, as
migration names are always suffixed by timestamp and will rarely conflict.
Putting "develop" in a non-deployable state, where an action needs to be
taken, to merge the two conflicting migrations.
The code below, basically mocks the `migration_name` property, adding a
default value to it, determined by `DEFAULT_MIGRATION_NAME`.
Django version: 1.8 (branch stable/1.8.x)
File: django/core/management/commands/makemigrations.py
Line: 45
> self.migration_name = options.get('name', None)
The code above will call the `migration_name` setter, that will hold the
custom migration name, if provided. Whenever this property is used across
the same file, it will call the `migration_name` property defined below,
which defaults to `DEFAULT_MIGRATION_NAME` in case `_migration_name` is
not provided.
This code seems to be valid up to branch stable/2.1.x
"""
@property
def migration_name(self):
return getattr(self, '_migration_name', DEFAULT_MIGRATION_NAME)
@migration_name.setter
def migration_name(self, name):
if name:
# Only override the migration name when a custom name is provided
self._migration_name = name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.