2013-12-05 00:01:31 +08:00
|
|
|
import importlib
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2014-10-07 07:53:21 +08:00
|
|
|
from django.db.models.fields import NOT_PROVIDED
|
2016-12-01 18:38:01 +08:00
|
|
|
from django.utils import datetime_safe, timezone
|
2013-12-05 00:01:31 +08:00
|
|
|
|
2015-08-21 01:45:30 +08:00
|
|
|
from .loader import MigrationLoader
|
2013-12-27 22:36:19 +08:00
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MigrationQuestioner:
|
2013-12-05 00:01:31 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Give the autodetector responses to questions it might have.
|
2013-12-05 00:01:31 +08:00
|
|
|
This base class has a built-in noninteractive mode, but the
|
|
|
|
interactive subclass is what the command-line arguments will use.
|
|
|
|
"""
|
|
|
|
|
2014-05-22 17:42:46 +08:00
|
|
|
def __init__(self, defaults=None, specified_apps=None, dry_run=None):
|
2013-12-05 00:01:31 +08:00
|
|
|
self.defaults = defaults or {}
|
|
|
|
self.specified_apps = specified_apps or set()
|
2014-05-22 17:42:46 +08:00
|
|
|
self.dry_run = dry_run
|
2013-12-05 00:01:31 +08:00
|
|
|
|
|
|
|
def ask_initial(self, app_label):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Should we create an initial migration for the app?"""
|
2013-12-05 00:01:31 +08:00
|
|
|
# If it was specified on the command line, definitely true
|
|
|
|
if app_label in self.specified_apps:
|
|
|
|
return True
|
|
|
|
# Otherwise, we look to see if it has a migrations module
|
|
|
|
# without any Python files in it, apart from __init__.py.
|
|
|
|
# Apps from the new app template will have these; the python
|
|
|
|
# file check will ensure we skip South ones.
|
|
|
|
try:
|
2013-12-24 19:25:17 +08:00
|
|
|
app_config = apps.get_app_config(app_label)
|
2013-12-14 18:11:52 +08:00
|
|
|
except LookupError: # It's a fake app.
|
2013-12-05 00:01:31 +08:00
|
|
|
return self.defaults.get("ask_initial", False)
|
2016-08-27 05:04:12 +08:00
|
|
|
migrations_import_path, _ = MigrationLoader.migrations_module(app_config.label)
|
2016-01-26 03:27:35 +08:00
|
|
|
if migrations_import_path is None:
|
|
|
|
# It's an application with migrations disabled.
|
|
|
|
return self.defaults.get("ask_initial", False)
|
2013-12-05 00:01:31 +08:00
|
|
|
try:
|
|
|
|
migrations_module = importlib.import_module(migrations_import_path)
|
|
|
|
except ImportError:
|
|
|
|
return self.defaults.get("ask_initial", False)
|
|
|
|
else:
|
2014-02-09 22:03:41 +08:00
|
|
|
if hasattr(migrations_module, "__file__"):
|
|
|
|
filenames = os.listdir(os.path.dirname(migrations_module.__file__))
|
|
|
|
elif hasattr(migrations_module, "__path__"):
|
|
|
|
if len(migrations_module.__path__) > 1:
|
|
|
|
return False
|
|
|
|
filenames = os.listdir(list(migrations_module.__path__)[0])
|
2013-12-05 00:01:31 +08:00
|
|
|
return not any(x.endswith(".py") for x in filenames if x != "__init__.py")
|
|
|
|
|
|
|
|
def ask_not_null_addition(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Adding a NOT NULL field to a model."""
|
2013-12-05 00:01:31 +08:00
|
|
|
# None means quit
|
|
|
|
return None
|
|
|
|
|
2014-10-07 07:53:21 +08:00
|
|
|
def ask_not_null_alteration(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Changing a NULL field to NOT NULL."""
|
2014-10-07 07:53:21 +08:00
|
|
|
# None means quit
|
|
|
|
return None
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
def ask_rename(self, model_name, old_name, new_name, field_instance):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Was this field really renamed?"""
|
2013-12-05 00:01:31 +08:00
|
|
|
return self.defaults.get("ask_rename", False)
|
|
|
|
|
2014-03-10 08:38:24 +08:00
|
|
|
def ask_rename_model(self, old_model_state, new_model_state):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Was this model really renamed?"""
|
2014-03-10 08:38:24 +08:00
|
|
|
return self.defaults.get("ask_rename_model", False)
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
def ask_merge(self, app_label):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Do you really want to merge these migrations?"""
|
2013-12-05 00:01:31 +08:00
|
|
|
return self.defaults.get("ask_merge", False)
|
|
|
|
|
2016-04-02 21:49:32 +08:00
|
|
|
def ask_auto_now_add_addition(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Adding an auto_now_add field to a model."""
|
2016-04-02 21:49:32 +08:00
|
|
|
# None means quit
|
|
|
|
return None
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
|
|
|
|
class InteractiveMigrationQuestioner(MigrationQuestioner):
|
|
|
|
|
|
|
|
def _boolean_input(self, question, default=None):
|
|
|
|
result = input("%s " % question)
|
|
|
|
if not result and default is not None:
|
|
|
|
return default
|
|
|
|
while len(result) < 1 or result[0].lower() not in "yn":
|
|
|
|
result = input("Please answer yes or no: ")
|
|
|
|
return result[0].lower() == "y"
|
|
|
|
|
|
|
|
def _choice_input(self, question, choices):
|
|
|
|
print(question)
|
|
|
|
for i, choice in enumerate(choices):
|
|
|
|
print(" %s) %s" % (i + 1, choice))
|
|
|
|
result = input("Select an option: ")
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
value = int(result)
|
|
|
|
if 0 < value <= len(choices):
|
|
|
|
return value
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
result = input("Please select a valid option: ")
|
|
|
|
|
2016-04-02 21:49:32 +08:00
|
|
|
def _ask_default(self, default=''):
|
|
|
|
"""
|
|
|
|
Prompt for a default value.
|
|
|
|
|
|
|
|
The ``default`` argument allows providing a custom default value (as a
|
|
|
|
string) which will be shown to the user and used as the return value
|
|
|
|
if the user doesn't provide any other input.
|
|
|
|
"""
|
2014-10-07 07:53:21 +08:00
|
|
|
print("Please enter the default value now, as valid Python")
|
2016-04-02 21:49:32 +08:00
|
|
|
if default:
|
|
|
|
print(
|
|
|
|
"You can accept the default '{}' by pressing 'Enter' or you "
|
|
|
|
"can provide another value.".format(default)
|
|
|
|
)
|
2016-04-02 22:59:04 +08:00
|
|
|
print("The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now")
|
2016-04-02 21:49:32 +08:00
|
|
|
print("Type 'exit' to exit this prompt")
|
2014-10-07 07:53:21 +08:00
|
|
|
while True:
|
2016-04-02 21:49:32 +08:00
|
|
|
if default:
|
|
|
|
prompt = "[default: {}] >>> ".format(default)
|
|
|
|
else:
|
|
|
|
prompt = ">>> "
|
2016-12-01 18:38:01 +08:00
|
|
|
code = input(prompt)
|
2016-04-02 21:49:32 +08:00
|
|
|
if not code and default:
|
|
|
|
code = default
|
2014-10-07 07:53:21 +08:00
|
|
|
if not code:
|
|
|
|
print("Please enter some code, or 'exit' (with no quotes) to exit.")
|
|
|
|
elif code == "exit":
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return eval(code, {}, {"datetime": datetime_safe, "timezone": timezone})
|
|
|
|
except (SyntaxError, NameError) as e:
|
|
|
|
print("Invalid input: %s" % e)
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
def ask_not_null_addition(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Adding a NOT NULL field to a model."""
|
2014-05-22 17:42:46 +08:00
|
|
|
if not self.dry_run:
|
|
|
|
choice = self._choice_input(
|
2014-10-07 07:53:21 +08:00
|
|
|
"You are trying to add a non-nullable field '%s' to %s without a default; "
|
|
|
|
"we can't do that (the database needs something to populate existing rows).\n"
|
|
|
|
"Please select a fix:" % (field_name, model_name),
|
|
|
|
[
|
2016-04-03 21:34:15 +08:00
|
|
|
("Provide a one-off default now (will be set on all existing "
|
|
|
|
"rows with a null value for this column)"),
|
2014-10-07 07:53:21 +08:00
|
|
|
"Quit, and let me add a default in models.py",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if choice == 2:
|
|
|
|
sys.exit(3)
|
|
|
|
else:
|
|
|
|
return self._ask_default()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def ask_not_null_alteration(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Changing a NULL field to NOT NULL."""
|
2014-10-07 07:53:21 +08:00
|
|
|
if not self.dry_run:
|
|
|
|
choice = self._choice_input(
|
|
|
|
"You are trying to change the nullable field '%s' on %s to non-nullable "
|
|
|
|
"without a default; we can't do that (the database needs something to "
|
|
|
|
"populate existing rows).\n"
|
|
|
|
"Please select a fix:" % (field_name, model_name),
|
2014-05-22 17:42:46 +08:00
|
|
|
[
|
2016-04-03 21:34:15 +08:00
|
|
|
("Provide a one-off default now (will be set on all existing "
|
|
|
|
"rows with a null value for this column)"),
|
2014-10-07 07:53:21 +08:00
|
|
|
("Ignore for now, and let me handle existing rows with NULL myself "
|
2015-09-23 23:29:05 +08:00
|
|
|
"(e.g. because you added a RunPython or RunSQL operation to handle "
|
|
|
|
"NULL values in a previous data migration)"),
|
2014-05-22 17:42:46 +08:00
|
|
|
"Quit, and let me add a default in models.py",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if choice == 2:
|
2014-10-07 07:53:21 +08:00
|
|
|
return NOT_PROVIDED
|
|
|
|
elif choice == 3:
|
2014-05-22 17:42:46 +08:00
|
|
|
sys.exit(3)
|
|
|
|
else:
|
2014-10-07 07:53:21 +08:00
|
|
|
return self._ask_default()
|
2014-05-22 17:42:46 +08:00
|
|
|
return None
|
2013-12-05 00:01:31 +08:00
|
|
|
|
|
|
|
def ask_rename(self, model_name, old_name, new_name, field_instance):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Was this field really renamed?"""
|
2014-10-25 12:42:44 +08:00
|
|
|
msg = "Did you rename %s.%s to %s.%s (a %s)? [y/N]"
|
|
|
|
return self._boolean_input(msg % (model_name, old_name, model_name, new_name,
|
|
|
|
field_instance.__class__.__name__), False)
|
2013-12-05 00:01:31 +08:00
|
|
|
|
2014-03-10 08:38:24 +08:00
|
|
|
def ask_rename_model(self, old_model_state, new_model_state):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Was this model really renamed?"""
|
2014-10-25 12:42:44 +08:00
|
|
|
msg = "Did you rename the %s.%s model to %s? [y/N]"
|
|
|
|
return self._boolean_input(msg % (old_model_state.app_label, old_model_state.name,
|
|
|
|
new_model_state.name), False)
|
2014-03-10 08:38:24 +08:00
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
def ask_merge(self, app_label):
|
|
|
|
return self._boolean_input(
|
|
|
|
"\nMerging will only work if the operations printed above do not conflict\n" +
|
|
|
|
"with each other (working on different fields or models)\n" +
|
|
|
|
"Do you want to merge these migration branches? [y/N]",
|
|
|
|
False,
|
|
|
|
)
|
2015-03-07 20:53:13 +08:00
|
|
|
|
2016-04-02 21:49:32 +08:00
|
|
|
def ask_auto_now_add_addition(self, field_name, model_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Adding an auto_now_add field to a model."""
|
2016-04-02 21:49:32 +08:00
|
|
|
if not self.dry_run:
|
|
|
|
choice = self._choice_input(
|
|
|
|
"You are trying to add the field '{}' with 'auto_now_add=True' "
|
|
|
|
"to {} without a default; the database needs something to "
|
|
|
|
"populate existing rows.\n".format(field_name, model_name),
|
|
|
|
[
|
|
|
|
"Provide a one-off default now (will be set on all "
|
|
|
|
"existing rows)",
|
|
|
|
"Quit, and let me add a default in models.py",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if choice == 2:
|
|
|
|
sys.exit(3)
|
|
|
|
else:
|
|
|
|
return self._ask_default(default='timezone.now')
|
|
|
|
return None
|
|
|
|
|
2015-03-07 20:53:13 +08:00
|
|
|
|
|
|
|
class NonInteractiveMigrationQuestioner(MigrationQuestioner):
|
|
|
|
|
|
|
|
def ask_not_null_addition(self, field_name, model_name):
|
|
|
|
# We can't ask the user, so act like the user aborted.
|
|
|
|
sys.exit(3)
|
|
|
|
|
|
|
|
def ask_not_null_alteration(self, field_name, model_name):
|
|
|
|
# We can't ask the user, so set as not provided.
|
|
|
|
return NOT_PROVIDED
|
2016-04-02 21:49:32 +08:00
|
|
|
|
|
|
|
def ask_auto_now_add_addition(self, field_name, model_name):
|
|
|
|
# We can't ask the user, so act like the user aborted.
|
|
|
|
sys.exit(3)
|