agendas: convert week day in db to iso numbering (#79168)
gitea/chrono/pipeline/head This commit looks good Details

This commit is contained in:
Valentin Deniaud 2023-06-28 17:03:18 +02:00
parent 900300dd05
commit 06af90608f
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,41 @@
# Generated by Django 3.2.18 on 2023-06-28 10:47
from django.db import migrations, models
from django.db.models import F, Func, OuterRef, Subquery
class ArraySubquery(Subquery):
template = 'ARRAY(%(subquery)s)'
def convert_week_days(apps, schema_editor):
Event = apps.get_model('agendas', 'Event')
SharedCustodyRule = apps.get_model('agendas', 'SharedCustodyRule') # TODO
events_with_days = (
Event.objects.filter(pk=OuterRef('pk'))
.annotate(week_day=Func(F('recurrence_days'), function='unnest', output_field=models.IntegerField()))
.annotate(new_week_day=F('week_day') + 1)
.values('new_week_day')
)
Event.objects.filter(recurrence_days__isnull=False).update(
recurrence_days=ArraySubquery(events_with_days)
)
rules_with_days = (
SharedCustodyRule.objects.filter(pk=OuterRef('pk'))
.annotate(week_day=Func(F('days'), function='unnest', output_field=models.IntegerField()))
.annotate(new_week_day=F('week_day') + 1)
.values('new_week_day')
)
SharedCustodyRule.objects.update(days=ArraySubquery(rules_with_days))
class Migration(migrations.Migration):
dependencies = [
('agendas', '0156_update_dow_index'),
]
operations = [
migrations.RunPython(convert_week_days, migrations.RunPython.noop),
]

View File

@ -342,3 +342,51 @@ def test_translate_holidays_exceptions(transactional_db):
assert not desk.timeperiodexception_set.filter(label='New year').exists()
assert desk.timeperiodexception_set.filter(label='Toussaint').count() == 1
assert desk.timeperiodexception_set.filter(label='Jour de lAn').count() == 1
def test_migration_convert_week_days(transactional_db):
app = 'agendas'
migrate_from = [(app, '0156_update_dow_index')]
migrate_to = [(app, '0157_convert_week_days')]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(migrate_from).apps
executor.migrate(migrate_from)
Agenda = old_apps.get_model(app, 'Agenda')
Event = old_apps.get_model(app, 'Event')
SharedCustodyRule = old_apps.get_model(app, 'SharedCustodyRule')
SharedCustodyAgenda = old_apps.get_model(app, 'SharedCustodyAgenda')
Person = old_apps.get_model(app, 'Person')
agenda = Agenda.objects.create(label='Foo', kind='events')
Event.objects.create(recurrence_days=None, start_datetime=now(), places=1, agenda=agenda, slug='none')
Event.objects.create(recurrence_days=[], start_datetime=now(), places=1, agenda=agenda, slug='empty')
Event.objects.create(recurrence_days=[3], start_datetime=now(), places=1, agenda=agenda, slug='[3]')
Event.objects.create(recurrence_days=[0, 6], start_datetime=now(), places=1, agenda=agenda, slug='[0, 6]')
Event.objects.create(
recurrence_days=[0, 1, 2, 3, 4, 5, 6], start_datetime=now(), places=1, agenda=agenda, slug='all'
)
father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe')
mother = Person.objects.create(user_external_id='mother_id', first_name='Jane', last_name='Doe')
child = Person.objects.create(user_external_id='xxx', first_name='James', last_name='Doe')
agenda = SharedCustodyAgenda.objects.create(
first_guardian=father, second_guardian=mother, child=child, date_start=now()
)
SharedCustodyRule.objects.create(days=[0, 4, 6], weeks='', guardian=father, agenda=agenda)
executor = MigrationExecutor(connection)
executor.migrate(migrate_to)
executor.loader.build_graph()
apps = executor.loader.project_state(migrate_to).apps
Event = apps.get_model(app, 'Event')
assert Event.objects.get(slug='none').recurrence_days is None
assert Event.objects.get(slug='empty').recurrence_days == []
assert Event.objects.get(slug='[3]').recurrence_days == [4]
assert Event.objects.get(slug='[0, 6]').recurrence_days == [1, 7]
assert Event.objects.get(slug='all').recurrence_days == [1, 2, 3, 4, 5, 6, 7]
assert SharedCustodyRule.objects.get().days == [1, 5, 7]