# Generated by Django 5.0.8 on 2024-08-26 16:55

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


class Migration(migrations.Migration):

    dependencies = [
        ("intecomm_reports", "0014_eos"),
    ]

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'intecomm_reports.eos_view' as `report_model` from (SELECT subject_identifier, visit_datetime, timepoint, visit_code, visit_code_sequence, offstudy_reason, offstudy_datetime, site_id, TIMESTAMPDIFF(MONTH, visit_datetime, NOW()) AS m, schedule_status FROM (SELECT b1.subject_identifier, b1.visit_datetime, b1.timepoint, appt.visit_code, appt.visit_code_sequence, appt.site_id, reasons.name AS offstudy_reason, offstudy_datetime, baseline_datetime, sched.schedule_status FROM (SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint >= 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id UNION SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint < 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id) AS b1 LEFT JOIN edc_appointment_appointment AS appt ON (b1.subject_identifier = appt.subject_identifier AND b1.timepoint = appt.timepoint) LEFT JOIN intecomm_lists_offstudyreasons AS reasons ON offstudy_reason_id = reasons.id LEFT JOIN edc_visit_schedule_subjectschedulehistory AS sched ON b1.subject_identifier = sched.subject_identifier) AS b2) as A ORDER BY site_id, subject_identifier",
                "eos_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "eos_view", engine="django.db.backends.mysql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'intecomm_reports.eos_view' as report_model from (SELECT subject_identifier, visit_datetime, timepoint, visit_code, visit_code_sequence, offstudy_reason, offstudy_datetime, site_id, TIMESTAMPDIFF(NOW(), visit_datetime, MONTH) AS m, schedule_status FROM (SELECT b1.subject_identifier, b1.visit_datetime, b1.timepoint, appt.visit_code, appt.visit_code_sequence, appt.site_id, reasons.name AS offstudy_reason, offstudy_datetime, baseline_datetime, sched.schedule_status FROM (SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint >= 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id UNION SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint < 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id) AS b1 LEFT JOIN edc_appointment_appointment AS appt ON (b1.subject_identifier = appt.subject_identifier AND b1.timepoint = appt.timepoint) LEFT JOIN intecomm_lists_offstudyreasons AS reasons ON offstudy_reason_id = reasons.id LEFT JOIN edc_visit_schedule_subjectschedulehistory AS sched ON b1.subject_identifier = sched.subject_identifier) AS b2) as A ORDER BY site_id, subject_identifier",
                "eos_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "eos_view", engine="django.db.backends.postgresql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'intecomm_reports.eos_view' as report_model from (SELECT subject_identifier, visit_datetime, timepoint, visit_code, visit_code_sequence, offstudy_reason, offstudy_datetime, site_id, TIMESTAMPDIFF(NOW(), visit_datetime, MONTH) AS m, schedule_status FROM (SELECT b1.subject_identifier, b1.visit_datetime, b1.timepoint, appt.visit_code, appt.visit_code_sequence, appt.site_id, reasons.name AS offstudy_reason, offstudy_datetime, baseline_datetime, sched.schedule_status FROM (SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint >= 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id UNION SELECT v.subject_identifier, MAX(v.report_datetime) AS visit_datetime, MIN(v.report_datetime) AS baseline_datetime, MAX(appt.timepoint) AS timepoint, eos.offstudy_reason_id, MAX(eos.offstudy_datetime) AS offstudy_datetime FROM intecomm_screening_patientlog AS plog LEFT JOIN intecomm_subject_subjectvisit AS v ON v.subject_identifier = plog.subject_identifier LEFT JOIN edc_appointment_appointment AS appt ON v.appointment_id = appt.id LEFT JOIN intecomm_prn_endofstudy AS eos ON eos.subject_identifier = plog.subject_identifier WHERE NOT plog.group_identifier IS NULL AND timepoint < 12.0 AND v.visit_code_sequence = 0 GROUP BY v.subject_identifier, eos.offstudy_reason_id) AS b1 LEFT JOIN edc_appointment_appointment AS appt ON (b1.subject_identifier = appt.subject_identifier AND b1.timepoint = appt.timepoint) LEFT JOIN intecomm_lists_offstudyreasons AS reasons ON offstudy_reason_id = reasons.id LEFT JOIN edc_visit_schedule_subjectschedulehistory AS sched ON b1.subject_identifier = sched.subject_identifier) AS b2) as A ORDER BY site_id, subject_identifier",
                "eos_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "eos_view", engine="django.db.backends.sqlite3"
            ),
            atomic=False,
        ),
    ]
