| 1 |
import os |
| 2 |
import re |
| 3 |
from importlib import import_module |
| 4 |
|
| 5 |
from django import get_version |
| 6 |
from django.apps import apps |
| 7 |
|
| 8 |
# SettingsReference imported for backwards compatibility in Django 2.2. |
| 9 |
from django.conf import SettingsReference # NOQA |
| 10 |
from django.db import migrations |
| 11 |
from django.db.migrations.loader import MigrationLoader |
| 12 |
from django.db.migrations.serializer import Serializer, serializer_factory |
| 13 |
from django.utils.inspect import get_func_args |
| 14 |
from django.utils.module_loading import module_dir |
| 15 |
from django.utils.timezone import now |
| 16 |
|
| 17 |
|
| 18 |
class OperationWriter: |
| 19 |
def __init__(self, operation, indentation=2): |
| 20 |
self.operation = operation |
| 21 |
self.buff = [] |
| 22 |
self.indentation = indentation |
| 23 |
|
| 24 |
def serialize(self): |
| 25 |
def _write(_arg_name, _arg_value): |
| 26 |
if _arg_name in self.operation.serialization_expand_args and isinstance( |
| 27 |
_arg_value, (list, tuple, dict) |
| 28 |
): |
| 29 |
if isinstance(_arg_value, dict): |
| 30 |
self.feed("%s={" % _arg_name) |
| 31 |
self.indent() |
| 32 |
for key, value in _arg_value.items(): |
| 33 |
key_string, key_imports = MigrationWriter.serialize(key) |
| 34 |
arg_string, arg_imports = MigrationWriter.serialize(value) |
| 35 |
args = arg_string.splitlines() |
| 36 |
if len(args) > 1: |
| 37 |
self.feed("%s: %s" % (key_string, args[0])) |
| 38 |
for arg in args[1:-1]: |
| 39 |
self.feed(arg) |
| 40 |
self.feed("%s," % args[-1]) |
| 41 |
else: |
| 42 |
self.feed("%s: %s," % (key_string, arg_string)) |
| 43 |
imports.update(key_imports) |
| 44 |
imports.update(arg_imports) |
| 45 |
self.unindent() |
| 46 |
self.feed("},") |
| 47 |
else: |
| 48 |
self.feed("%s=[" % _arg_name) |
| 49 |
self.indent() |
| 50 |
for item in _arg_value: |
| 51 |
arg_string, arg_imports = MigrationWriter.serialize(item) |
| 52 |
args = arg_string.splitlines() |
| 53 |
if len(args) > 1: |
| 54 |
for arg in args[:-1]: |
| 55 |
self.feed(arg) |
| 56 |
self.feed("%s," % args[-1]) |
| 57 |
else: |
| 58 |
self.feed("%s," % arg_string) |
| 59 |
imports.update(arg_imports) |
| 60 |
self.unindent() |
| 61 |
self.feed("],") |
| 62 |
else: |
| 63 |
arg_string, arg_imports = MigrationWriter.serialize(_arg_value) |
| 64 |
args = arg_string.splitlines() |
| 65 |
if len(args) > 1: |
| 66 |
self.feed("%s=%s" % (_arg_name, args[0])) |
| 67 |
for arg in args[1:-1]: |
| 68 |
self.feed(arg) |
| 69 |
self.feed("%s," % args[-1]) |
| 70 |
else: |
| 71 |
self.feed("%s=%s," % (_arg_name, arg_string)) |
| 72 |
imports.update(arg_imports) |
| 73 |
|
| 74 |
imports = set() |
| 75 |
name, args, kwargs = self.operation.deconstruct() |
| 76 |
operation_args = get_func_args(self.operation.__init__) |
| 77 |
|
| 78 |
# See if this operation is in django.db.migrations. If it is, |
| 79 |
# We can just use the fact we already have that imported, |
| 80 |
# otherwise, we need to add an import for the operation class. |
| 81 |
if getattr(migrations, name, None) == self.operation.__class__: |
| 82 |
self.feed("migrations.%s(" % name) |
| 83 |
else: |
| 84 |
imports.add("import %s" % (self.operation.__class__.__module__)) |
| 85 |
self.feed("%s.%s(" % (self.operation.__class__.__module__, name)) |
| 86 |
|
| 87 |
self.indent() |
| 88 |
|
| 89 |
for i, arg in enumerate(args): |
| 90 |
arg_value = arg |
| 91 |
arg_name = operation_args[i] |
| 92 |
_write(arg_name, arg_value) |
| 93 |
|
| 94 |
i = len(args) |
| 95 |
# Only iterate over remaining arguments |
| 96 |
for arg_name in operation_args[i:]: |
| 97 |
if arg_name in kwargs: # Don't sort to maintain signature order |
| 98 |
arg_value = kwargs[arg_name] |
| 99 |
_write(arg_name, arg_value) |
| 100 |
|
| 101 |
self.unindent() |
| 102 |
self.feed("),") |
| 103 |
return self.render(), imports |
| 104 |
|
| 105 |
def indent(self): |
| 106 |
self.indentation += 1 |
| 107 |
|
| 108 |
def unindent(self): |
| 109 |
self.indentation -= 1 |
| 110 |
|
| 111 |
def feed(self, line): |
| 112 |
self.buff.append(" " * (self.indentation * 4) + line) |
| 113 |
|
| 114 |
def render(self): |
| 115 |
return "\n".join(self.buff) |
| 116 |
|
| 117 |
|
| 118 |
class MigrationWriter: |
| 119 |
""" |
| 120 |
Take a Migration instance and is able to produce the contents |
| 121 |
of the migration file from it. |
| 122 |
""" |
| 123 |
|
| 124 |
def __init__(self, migration, include_header=True): |
| 125 |
self.migration = migration |
| 126 |
self.include_header = include_header |
| 127 |
self.needs_manual_porting = False |
| 128 |
|
| 129 |
def as_string(self): |
| 130 |
"""Return a string of the file contents.""" |
| 131 |
items = { |
| 132 |
"replaces_str": "", |
| 133 |
"initial_str": "", |
| 134 |
} |
| 135 |
|
| 136 |
imports = set() |
| 137 |
|
| 138 |
# Deconstruct operations |
| 139 |
operations = [] |
| 140 |
for operation in self.migration.operations: |
| 141 |
operation_string, operation_imports = OperationWriter(operation).serialize() |
| 142 |
imports.update(operation_imports) |
| 143 |
operations.append(operation_string) |
| 144 |
items["operations"] = "\n".join(operations) + "\n" if operations else "" |
| 145 |
|
| 146 |
# Format dependencies and write out swappable dependencies right |
| 147 |
dependencies = [] |
| 148 |
for dependency in self.migration.dependencies: |
| 149 |
if dependency[0] == "__setting__": |
| 150 |
dependencies.append( |
| 151 |
" migrations.swappable_dependency(settings.%s)," |
| 152 |
% dependency[1] |
| 153 |
) |
| 154 |
imports.add("from django.conf import settings") |
| 155 |
else: |
| 156 |
dependencies.append(" %s," % self.serialize(dependency)[0]) |
| 157 |
items["dependencies"] = ( |
| 158 |
"\n".join(sorted(dependencies)) + "\n" if dependencies else "" |
| 159 |
) |
| 160 |
|
| 161 |
# Format imports nicely, swapping imports of functions from migration files |
| 162 |
# for comments |
| 163 |
migration_imports = set() |
| 164 |
for line in list(imports): |
| 165 |
if re.match(r"^import (.*)\.\d+[^\s]*$", line): |
| 166 |
migration_imports.add(line.split("import")[1].strip()) |
| 167 |
imports.remove(line) |
| 168 |
self.needs_manual_porting = True |
| 169 |
|
| 170 |
# django.db.migrations is always used, but models import may not be. |
| 171 |
# If models import exists, merge it with migrations import. |
| 172 |
if "from django.db import models" in imports: |
| 173 |
imports.discard("from django.db import models") |
| 174 |
imports.add("from django.db import migrations, models") |
| 175 |
else: |
| 176 |
imports.add("from django.db import migrations") |
| 177 |
|
| 178 |
# Sort imports by the package / module to be imported (the part after |
| 179 |
# "from" in "from ... import ..." or after "import" in "import ..."). |
| 180 |
# First group the "import" statements, then "from ... import ...". |
| 181 |
sorted_imports = sorted( |
| 182 |
imports, key=lambda i: (i.split()[0] == "from", i.split()[1]) |
| 183 |
) |
| 184 |
items["imports"] = "\n".join(sorted_imports) + "\n" if imports else "" |
| 185 |
if migration_imports: |
| 186 |
items["imports"] += ( |
| 187 |
"\n\n# Functions from the following migrations need manual " |
| 188 |
"copying.\n# Move them and any dependencies into this file, " |
| 189 |
"then update the\n# RunPython operations to refer to the local " |
| 190 |
"versions:\n# %s" |
| 191 |
) % "\n# ".join(sorted(migration_imports)) |
| 192 |
# If there's a replaces, make a string for it |
| 193 |
if self.migration.replaces: |
| 194 |
items["replaces_str"] = ( |
| 195 |
"\n replaces = %s\n" % self.serialize(self.migration.replaces)[0] |
| 196 |
) |
| 197 |
# Hinting that goes into comment |
| 198 |
if self.include_header: |
| 199 |
items["migration_header"] = MIGRATION_HEADER_TEMPLATE % { |
| 200 |
"version": get_version(), |
| 201 |
"timestamp": now().strftime("%Y-%m-%d %H:%M"), |
| 202 |
} |
| 203 |
else: |
| 204 |
items["migration_header"] = "" |
| 205 |
|
| 206 |
if self.migration.initial: |
| 207 |
items["initial_str"] = "\n initial = True\n" |
| 208 |
|
| 209 |
return MIGRATION_TEMPLATE % items |
| 210 |
|
| 211 |
@property |
| 212 |
def basedir(self): |
| 213 |
migrations_package_name, _ = MigrationLoader.migrations_module( |
| 214 |
self.migration.app_label |
| 215 |
) |
| 216 |
|
| 217 |
if migrations_package_name is None: |
| 218 |
raise ValueError( |
| 219 |
"Django can't create migrations for app '%s' because " |
| 220 |
"migrations have been disabled via the MIGRATION_MODULES " |
| 221 |
"setting." % self.migration.app_label |
| 222 |
) |
| 223 |
|
| 224 |
# See if we can import the migrations module directly |
| 225 |
try: |
| 226 |
migrations_module = import_module(migrations_package_name) |
| 227 |
except ImportError: |
| 228 |
pass |
| 229 |
else: |
| 230 |
try: |
| 231 |
return module_dir(migrations_module) |
| 232 |
except ValueError: |
| 233 |
pass |
| 234 |
|
| 235 |
# Alright, see if it's a direct submodule of the app |
| 236 |
app_config = apps.get_app_config(self.migration.app_label) |
| 237 |
( |
| 238 |
maybe_app_name, |
| 239 |
_, |
| 240 |
migrations_package_basename, |
| 241 |
) = migrations_package_name.rpartition(".") |
| 242 |
if app_config.name == maybe_app_name: |
| 243 |
return os.path.join(app_config.path, migrations_package_basename) |
| 244 |
|
| 245 |
# In case of using MIGRATION_MODULES setting and the custom package |
| 246 |
# doesn't exist, create one, starting from an existing package |
| 247 |
existing_dirs, missing_dirs = migrations_package_name.split("."), [] |
| 248 |
while existing_dirs: |
| 249 |
missing_dirs.insert(0, existing_dirs.pop(-1)) |
| 250 |
try: |
| 251 |
base_module = import_module(".".join(existing_dirs)) |
| 252 |
except (ImportError, ValueError): |
| 253 |
continue |
| 254 |
else: |
| 255 |
try: |
| 256 |
base_dir = module_dir(base_module) |
| 257 |
except ValueError: |
| 258 |
continue |
| 259 |
else: |
| 260 |
break |
| 261 |
else: |
| 262 |
raise ValueError( |
| 263 |
"Could not locate an appropriate location to create " |
| 264 |
"migrations package %s. Make sure the toplevel " |
| 265 |
"package exists and can be imported." % migrations_package_name |
| 266 |
) |
| 267 |
|
| 268 |
final_dir = os.path.join(base_dir, *missing_dirs) |
| 269 |
os.makedirs(final_dir, exist_ok=True) |
| 270 |
for missing_dir in missing_dirs: |
| 271 |
base_dir = os.path.join(base_dir, missing_dir) |
| 272 |
with open(os.path.join(base_dir, "__init__.py"), "w"): |
| 273 |
pass |
| 274 |
|
| 275 |
return final_dir |
| 276 |
|
| 277 |
@property |
| 278 |
def filename(self): |
| 279 |
return "%s.py" % self.migration.name |
| 280 |
|
| 281 |
@property |
| 282 |
def path(self): |
| 283 |
return os.path.join(self.basedir, self.filename) |
| 284 |
|
| 285 |
@classmethod |
| 286 |
def serialize(cls, value): |
| 287 |
return serializer_factory(value).serialize() |
| 288 |
|
| 289 |
@classmethod |
| 290 |
def register_serializer(cls, type_, serializer): |
| 291 |
Serializer.register(type_, serializer) |
| 292 |
|
| 293 |
@classmethod |
| 294 |
def unregister_serializer(cls, type_): |
| 295 |
Serializer.unregister(type_) |
| 296 |
|
| 297 |
|
| 298 |
MIGRATION_HEADER_TEMPLATE = """\ |
| 299 |
# Generated by Django %(version)s on %(timestamp)s |
| 300 |
|
| 301 |
""" |
| 302 |
|
| 303 |
|
| 304 |
MIGRATION_TEMPLATE = """\ |
| 305 |
%(migration_header)s%(imports)s |
| 306 |
|
| 307 |
class Migration(migrations.Migration): |
| 308 |
%(replaces_str)s%(initial_str)s |
| 309 |
dependencies = [ |
| 310 |
%(dependencies)s\ |
| 311 |
] |
| 312 |
|
| 313 |
operations = [ |
| 314 |
%(operations)s\ |
| 315 |
] |
| 316 |
""" |