# Generated by Django 5.0.7 on 2024-08-13 12:16

import django_db_views.migration_functions
import django_db_views.operations
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ("meta_reports", "0011_auto_20240813_0156"),
    ]

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as created, 'meta_reports.unattendedthreeinrow' as `report_model` from (\n        select subject_identifier, site_id, appt_datetime, `first_value`, `second_value`, `third_value`,\n            datediff(third_date, first_date) as `interval_days`,\n            datediff(now(), first_date) as `from_now_days`\n        from (\n            select subject_identifier, site_id, appt_datetime,\n            FIRST_VALUE(visit_code) OVER w as `first_value`,\n            NTH_VALUE(visit_code, 2) OVER w as `second_value`,\n            NTH_VALUE(visit_code, 3) OVER w as `third_value`,\n            FIRST_VALUE(appt_datetime) OVER w as `first_date`,\n            NTH_VALUE(appt_datetime, 3) OVER w as `third_date`\n            from edc_appointment_appointment where visit_code_sequence=0 and appt_status='New'\n            and appt_datetime <= now()\n            WINDOW w as (PARTITION BY subject_identifier order by appt_datetime ROWS UNBOUNDED PRECEDING)\n        ) as A\n    where `second_value` is not null and `third_value` is not null\n    ) as B\n    order by site_id, from_now_days desc",
                "unattended_three_in_row_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "unattended_three_in_row_view", engine="django.db.backends.mysql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, gen_random_uuid() as id, now() as created, \n    'meta_reports.unattendedthreeinrow' as report_model \n    from (\n    select subject_identifier, site_id, appt_datetime, first_value, second_value, third_value,\n    EXTRACT(DAY FROM third_date - first_date) as interval_days,\n    EXTRACT(DAY FROM now() - first_date) as from_now_days \n    from (\n    select subject_identifier,site_id,appt_datetime,\n    FIRST_VALUE(visit_code) OVER w as first_value,\n    NTH_VALUE(visit_code, 2) OVER w as second_value,\n    NTH_VALUE(visit_code, 3) OVER w as third_value,\n    FIRST_VALUE(appt_datetime) OVER w as first_date,\n    NTH_VALUE(appt_datetime, 3) OVER w as third_date\n    from edc_appointment_appointment where visit_code_sequence=0 and appt_status='New'\n    and appt_datetime <= now()\n    WINDOW w as (PARTITION BY subject_identifier order by appt_datetime ROWS UNBOUNDED PRECEDING)\n    ) as A\n    where second_value is not null and third_value is not null\n    ) as B\n    order by site_id, from_now_days desc",
                "unattended_three_in_row_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "unattended_three_in_row_view", engine="django.db.backends.postgresql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "SELECT *, lower(\n    hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||\n    substr(hex( randomblob(2)), 2) || '-' ||\n    substr('AB89', 1 + (abs(random()) % 4) , 1)  ||\n    substr(hex(randomblob(2)), 2) || '-' ||\n    hex(randomblob(6))\n  ) as id, datetime() as created,'meta_reports.unattendedthreeinrow' as report_model from (\n        select subject_identifier, site_id, appt_datetime, first_value, second_value, third_value,\n            CAST(JulianDay(third_date) - JulianDay(first_date) AS Integer) as interval_days,\n            CAST(JulianDay(datetime()) - JulianDay(first_date) AS Integer) as from_now_days\n        from (\n            select subject_identifier, site_id, appt_datetime,\n            FIRST_VALUE(visit_code) OVER w as first_value,\n            NTH_VALUE(visit_code, 2) OVER w as second_value,\n            NTH_VALUE(visit_code, 3) OVER w as third_value,\n            FIRST_VALUE(appt_datetime) OVER w as first_date,\n            NTH_VALUE(appt_datetime, 3) OVER w as third_date\n            from edc_appointment_appointment where visit_code_sequence=0 and appt_status='New'\n            and appt_datetime <= datetime()\n            WINDOW w as (PARTITION BY subject_identifier order by appt_datetime ROWS UNBOUNDED PRECEDING)\n        ) as A\n    where second_value is not null and third_value is not null\n    ) as B\n    order by site_id, from_now_days desc",
                "unattended_three_in_row_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "unattended_three_in_row_view", engine="django.db.backends.sqlite3"
            ),
            atomic=False,
        ),
    ]
