Migration 0035_preference_model_unique still broken
Exception:
Running migrations:
Applying core.0035_preference_model_unique...Traceback (most recent call last):
File "/srv/aleksis/envs/production/bin/aleksis-admin", line 8, in <module>
sys.exit(aleksis_cmd())
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/aleksis/core/__main__.py", line 14, in aleksis_cmd
execute_from_command_line(sys.argv)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/aleksis/core/migrations/0035_preference_model_unique.py", line 35, in _migrate_preference_models_to_unique
if BasePreferenceModel.get_value(obj) == BasePreferenceModel.preference(obj).default or found:
File "/srv/aleksis/envs/production/lib/python3.9/site-packages/dynamic_preferences/models.py", line 59, in get_value
return self.preference.serializer.deserialize(self.raw_value)
AttributeError: 'PersonPreferenceModel' object has no attribute 'preference'
That's the way it works:
# Generated by Django 3.2.11 on 2022-01-27 18:52
from email.headerregistry import Group
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sites', '0002_alter_domain_unique'),
('core', '0034_invite_permission'),
]
def _migrate_preference_models_to_unique(apps, schema_editor):
from aleksis.core.models import GroupPreferenceModel, PersonPreferenceModel, SitePreferenceModel
models = [GroupPreferenceModel, PersonPreferenceModel, SitePreferenceModel]
for model in models:
duplicates = {}
db_alias = schema_editor.connection.alias
for obj in model.objects.using(db_alias).all():
key = f"{obj.instance.pk}__{obj.section}__{obj.name}"
duplicates.setdefault(key, [])
duplicates[key].append(obj)
for key, objs in duplicates.items():
if len(objs) > 1:
found = False
for obj in objs:
if obj.value == obj.preference.default or found:
obj.delete()
else:
found = True
operations = [
migrations.RunPython(_migrate_preference_models_to_unique),
migrations.AlterUniqueTogether(
name='grouppreferencemodel',
unique_together={('instance', 'section', 'name')},
),
migrations.AlterUniqueTogether(
name='personpreferencemodel',
unique_together={('instance', 'section', 'name')},
),
migrations.AlterUniqueTogether(
name='sitepreferencemodel',
unique_together={('instance', 'section', 'name')},
),
]