mirror of
https://github.com/jazzband/django-ddp.git
synced 2026-03-16 22:40:24 +00:00
Added helper for migrations involving AleaIdField to populate fields from ObjectMapping table.
This commit is contained in:
parent
b32e5aa547
commit
489175d9a9
1 changed files with 48 additions and 0 deletions
|
|
@ -1,4 +1,7 @@
|
|||
import functools
|
||||
from django.db import migrations
|
||||
from django.db.migrations.operations.base import Operation
|
||||
from dddp.models import AleaIdField, get_meteor_id
|
||||
|
||||
|
||||
class TruncateOperation(Operation):
|
||||
|
|
@ -35,3 +38,48 @@ class TruncateOperation(Operation):
|
|||
def describe(self):
|
||||
"""Describe what the operation does in console output."""
|
||||
return "Truncate tables"
|
||||
|
||||
|
||||
def set_default_forwards(app_name, operation, apps, schema_editor):
|
||||
"""Set default value for AleaIdField."""
|
||||
model = apps.get_model(app_name, operation.model_name)
|
||||
for obj_pk in model.objects.values_list('pk', flat=True):
|
||||
model.objects.filter(pk=obj_pk).update(**{
|
||||
operation.name: get_meteor_id(model, obj_pk),
|
||||
})
|
||||
|
||||
|
||||
def set_default_reverse(app_name, operation, apps, schema_editor):
|
||||
"""Unset default value for AleaIdField."""
|
||||
model = apps.get_model(app_name, operation.model_name)
|
||||
for obj_pk in model.objects.values_list('pk', flat=True):
|
||||
get_meteor_id(model, obj_pk)
|
||||
|
||||
|
||||
class DefaultAleaIdOperations(object):
|
||||
|
||||
def __init__(self, app_name):
|
||||
self.app_name = app_name
|
||||
|
||||
def __add__(self, operations):
|
||||
default_operations = []
|
||||
for operation in operations:
|
||||
if not isinstance(operation, migrations.AlterField):
|
||||
continue
|
||||
if not isinstance(operation.field, AleaIdField):
|
||||
continue
|
||||
if operation.name != 'aid':
|
||||
continue
|
||||
if operation.field.null:
|
||||
continue
|
||||
default_operations.append(
|
||||
migrations.RunPython(
|
||||
code=functools.partial(
|
||||
set_default_forwards, self.app_name, operation,
|
||||
),
|
||||
reverse_code=functools.partial(
|
||||
set_default_reverse, self.app_name, operation,
|
||||
),
|
||||
)
|
||||
)
|
||||
return default_operations + operations
|
||||
|
|
|
|||
Loading…
Reference in a new issue