Debian Codegraph @

django/db/migrations/operations/fields.py

raw
1 from django.db.migrations.utils import field_references
2 from django.db.models import NOT_PROVIDED
3 from django.utils.functional import cached_property
4
5 from .base import Operation, OperationCategory
6
7
8 class FieldOperation(Operation):
9 def __init__(self, model_name, name, field=None):
10 self.model_name = model_name
11 self.name = name
12 self.field = field
13
14 @cached_property
15 def model_name_lower(self):
16 return self.model_name.lower()
17
18 @cached_property
19 def name_lower(self):
20 return self.name.lower()
21
22 def is_same_model_operation(self, operation):
23 return self.model_name_lower == operation.model_name_lower
24
25 def is_same_field_operation(self, operation):
26 return (
27 self.is_same_model_operation(operation)
28 and self.name_lower == operation.name_lower
29 )
30
31 def references_model(self, name, app_label):
32 name_lower = name.lower()
33 if name_lower == self.model_name_lower:
34 return True
35 if self.field:
36 return bool(
37 field_references(
38 (app_label, self.model_name_lower),
39 self.field,
40 (app_label, name_lower),
41 )
42 )
43 return False
44
45 def references_field(self, model_name, name, app_label):
46 model_name_lower = model_name.lower()
47 # Check if this operation locally references the field.
48 if model_name_lower == self.model_name_lower:
49 if name == self.name:
50 return True
51 elif (
52 self.field
53 and hasattr(self.field, "from_fields")
54 and name in self.field.from_fields
55 ):
56 return True
57 # Check if this operation remotely references the field.
58 if self.field is None:
59 return False
60 return bool(
61 field_references(
62 (app_label, self.model_name_lower),
63 self.field,
64 (app_label, model_name_lower),
65 name,
66 )
67 )
68
69 def reduce(self, operation, app_label):
70 return super().reduce(operation, app_label) or not operation.references_field(
71 self.model_name, self.name, app_label
72 )
73
74
75 class AddField(FieldOperation):
76 """Add a field to a model."""
77
78 category = OperationCategory.ADDITION
79
80 def __init__(self, model_name, name, field, preserve_default=True):
81 self.preserve_default = preserve_default
82 super().__init__(model_name, name, field)
83
84 def deconstruct(self):
85 kwargs = {
86 "model_name": self.model_name,
87 "name": self.name,
88 "field": self.field,
89 }
90 if self.preserve_default is not True:
91 kwargs["preserve_default"] = self.preserve_default
92 return (self.__class__.__name__, [], kwargs)
93
94 def state_forwards(self, app_label, state):
95 state.add_field(
96 app_label,
97 self.model_name_lower,
98 self.name,
99 self.field,
100 self.preserve_default,
101 )
102
103 def database_forwards(self, app_label, schema_editor, from_state, to_state):
104 to_model = to_state.apps.get_model(app_label, self.model_name)
105 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
106 from_model = from_state.apps.get_model(app_label, self.model_name)
107 field = to_model._meta.get_field(self.name)
108 if not self.preserve_default:
109 field.default = self.field.default
110 schema_editor.add_field(
111 from_model,
112 field,
113 )
114 if not self.preserve_default:
115 field.default = NOT_PROVIDED
116
117 def database_backwards(self, app_label, schema_editor, from_state, to_state):
118 from_model = from_state.apps.get_model(app_label, self.model_name)
119 if self.allow_migrate_model(schema_editor.connection.alias, from_model):
120 schema_editor.remove_field(
121 from_model, from_model._meta.get_field(self.name)
122 )
123
124 def describe(self):
125 return "Add field %s to %s" % (self.name, self.model_name)
126
127 @property
128 def migration_name_fragment(self):
129 return "%s_%s" % (self.model_name_lower, self.name_lower)
130
131 def reduce(self, operation, app_label):
132 if isinstance(operation, FieldOperation) and self.is_same_field_operation(
133 operation
134 ):
135 if isinstance(operation, AlterField):
136 return [
137 AddField(
138 model_name=self.model_name,
139 name=operation.name,
140 field=operation.field,
141 ),
142 ]
143 elif isinstance(operation, RemoveField):
144 return []
145 elif isinstance(operation, RenameField):
146 return [
147 AddField(
148 model_name=self.model_name,
149 name=operation.new_name,
150 field=self.field,
151 ),
152 ]
153 return super().reduce(operation, app_label)
154
155
156 class RemoveField(FieldOperation):
157 """Remove a field from a model."""
158
159 category = OperationCategory.REMOVAL
160
161 def deconstruct(self):
162 kwargs = {
163 "model_name": self.model_name,
164 "name": self.name,
165 }
166 return (self.__class__.__name__, [], kwargs)
167
168 def state_forwards(self, app_label, state):
169 state.remove_field(app_label, self.model_name_lower, self.name)
170
171 def database_forwards(self, app_label, schema_editor, from_state, to_state):
172 from_model = from_state.apps.get_model(app_label, self.model_name)
173 if self.allow_migrate_model(schema_editor.connection.alias, from_model):
174 schema_editor.remove_field(
175 from_model, from_model._meta.get_field(self.name)
176 )
177
178 def database_backwards(self, app_label, schema_editor, from_state, to_state):
179 to_model = to_state.apps.get_model(app_label, self.model_name)
180 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
181 from_model = from_state.apps.get_model(app_label, self.model_name)
182 schema_editor.add_field(from_model, to_model._meta.get_field(self.name))
183
184 def describe(self):
185 return "Remove field %s from %s" % (self.name, self.model_name)
186
187 @property
188 def migration_name_fragment(self):
189 return "remove_%s_%s" % (self.model_name_lower, self.name_lower)
190
191 def reduce(self, operation, app_label):
192 from .models import DeleteModel
193
194 if (
195 isinstance(operation, DeleteModel)
196 and operation.name_lower == self.model_name_lower
197 ):
198 return [operation]
199 return super().reduce(operation, app_label)
200
201
202 class AlterField(FieldOperation):
203 """
204 Alter a field's database column (e.g. null, max_length) to the provided
205 new field.
206 """
207
208 category = OperationCategory.ALTERATION
209
210 def __init__(self, model_name, name, field, preserve_default=True):
211 self.preserve_default = preserve_default
212 super().__init__(model_name, name, field)
213
214 def deconstruct(self):
215 kwargs = {
216 "model_name": self.model_name,
217 "name": self.name,
218 "field": self.field,
219 }
220 if self.preserve_default is not True:
221 kwargs["preserve_default"] = self.preserve_default
222 return (self.__class__.__name__, [], kwargs)
223
224 def state_forwards(self, app_label, state):
225 state.alter_field(
226 app_label,
227 self.model_name_lower,
228 self.name,
229 self.field,
230 self.preserve_default,
231 )
232
233 def database_forwards(self, app_label, schema_editor, from_state, to_state):
234 to_model = to_state.apps.get_model(app_label, self.model_name)
235 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
236 from_model = from_state.apps.get_model(app_label, self.model_name)
237 from_field = from_model._meta.get_field(self.name)
238 to_field = to_model._meta.get_field(self.name)
239 if not self.preserve_default:
240 to_field.default = self.field.default
241 schema_editor.alter_field(from_model, from_field, to_field)
242 if not self.preserve_default:
243 to_field.default = NOT_PROVIDED
244
245 def database_backwards(self, app_label, schema_editor, from_state, to_state):
246 self.database_forwards(app_label, schema_editor, from_state, to_state)
247
248 def describe(self):
249 return "Alter field %s on %s" % (self.name, self.model_name)
250
251 @property
252 def migration_name_fragment(self):
253 return "alter_%s_%s" % (self.model_name_lower, self.name_lower)
254
255 def reduce(self, operation, app_label):
256 if isinstance(
257 operation, (AlterField, RemoveField)
258 ) and self.is_same_field_operation(operation):
259 return [operation]
260 elif (
261 isinstance(operation, RenameField)
262 and self.is_same_field_operation(operation)
263 and self.field.db_column is None
264 ):
265 return [
266 operation,
267 AlterField(
268 model_name=self.model_name,
269 name=operation.new_name,
270 field=self.field,
271 ),
272 ]
273 return super().reduce(operation, app_label)
274
275
276 class RenameField(FieldOperation):
277 """Rename a field on the model. Might affect db_column too."""
278
279 category = OperationCategory.ALTERATION
280
281 def __init__(self, model_name, old_name, new_name):
282 self.old_name = old_name
283 self.new_name = new_name
284 super().__init__(model_name, old_name)
285
286 @cached_property
287 def old_name_lower(self):
288 return self.old_name.lower()
289
290 @cached_property
291 def new_name_lower(self):
292 return self.new_name.lower()
293
294 def deconstruct(self):
295 kwargs = {
296 "model_name": self.model_name,
297 "old_name": self.old_name,
298 "new_name": self.new_name,
299 }
300 return (self.__class__.__name__, [], kwargs)
301
302 def state_forwards(self, app_label, state):
303 state.rename_field(
304 app_label, self.model_name_lower, self.old_name, self.new_name
305 )
306
307 def database_forwards(self, app_label, schema_editor, from_state, to_state):
308 to_model = to_state.apps.get_model(app_label, self.model_name)
309 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
310 from_model = from_state.apps.get_model(app_label, self.model_name)
311 schema_editor.alter_field(
312 from_model,
313 from_model._meta.get_field(self.old_name),
314 to_model._meta.get_field(self.new_name),
315 )
316
317 def database_backwards(self, app_label, schema_editor, from_state, to_state):
318 to_model = to_state.apps.get_model(app_label, self.model_name)
319 if self.allow_migrate_model(schema_editor.connection.alias, to_model):
320 from_model = from_state.apps.get_model(app_label, self.model_name)
321 schema_editor.alter_field(
322 from_model,
323 from_model._meta.get_field(self.new_name),
324 to_model._meta.get_field(self.old_name),
325 )
326
327 def describe(self):
328 return "Rename field %s on %s to %s" % (
329 self.old_name,
330 self.model_name,
331 self.new_name,
332 )
333
334 @property
335 def migration_name_fragment(self):
336 return "rename_%s_%s_%s" % (
337 self.old_name_lower,
338 self.model_name_lower,
339 self.new_name_lower,
340 )
341
342 def references_field(self, model_name, name, app_label):
343 return self.references_model(model_name, app_label) and (
344 name.lower() == self.old_name_lower or name.lower() == self.new_name_lower
345 )
346
347 def reduce(self, operation, app_label):
348 if (
349 isinstance(operation, RenameField)
350 and self.is_same_model_operation(operation)
351 and self.new_name_lower == operation.old_name_lower
352 ):
353 return [
354 RenameField(
355 self.model_name,
356 self.old_name,
357 operation.new_name,
358 ),
359 ]
360 # Skip `FieldOperation.reduce` as we want to run `references_field`
361 # against self.old_name and self.new_name.
362 return super(FieldOperation, self).reduce(operation, app_label) or not (
363 operation.references_field(self.model_name, self.old_name, app_label)
364 or operation.references_field(self.model_name, self.new_name, app_label)
365 )