# Generated by Django 5.0.8 on 2024-08-26 15:07

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


class Migration(migrations.Migration):

    dependencies = [
        ("intecomm_reports", "0012_remove_vlsummary2_site_alter_diagnoses_managers_and_more"),
    ]

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "with visits as (\n                select subject_identifier, report_datetime, visit_code, visit_code_sequence\n                from intecomm_subject_subjectvisit\n                group by subject_identifier, report_datetime, visit_code, visit_code_sequence\n                order by subject_identifier, report_datetime, visit_code, visit_code_sequence\n            )\n     select *, uuid() as id, now() as `created`, 'intecomm_reports.subjectstransferred_view' as `report_model` from (SELECT prn.subject_identifier, prn.site_id, consent_datetime AS consented, v.visit_code, v.report_datetime AS last_visit, prn.report_datetime AS transferred, DATEDIFF(prn.report_datetime, consent_datetime) / 30 AS months, eof.offstudy_datetime AS offstudy, eof.report_datetime AS last_seen FROM intecomm_prn_subjecttransfer AS prn LEFT JOIN edc_registration_registeredsubject AS rs ON prn.subject_identifier = rs.subject_identifier LEFT JOIN intecomm_prn_endofstudy AS eof ON prn.subject_identifier = eof.subject_identifier LEFT JOIN (SELECT DISTINCT subject_identifier, LAST_VALUE(report_datetime) OVER w AS report_datetime, LAST_VALUE(visit_code) OVER w AS visit_code FROM visits WINDOW w AS (PARTITION BY `subject_identifier`)) AS v ON prn.subject_identifier = v.subject_identifier ORDER BY DATEDIFF(prn.report_datetime, consent_datetime) / 30 DESC, subject_identifier) as A ORDER BY site_id, subject_identifier",
                "subjects_transferred_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "subjects_transferred_view", engine="django.db.backends.mysql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "with visits as (\n                select subject_identifier, report_datetime, visit_code, visit_code_sequence\n                from intecomm_subject_subjectvisit\n                group by subject_identifier, report_datetime, visit_code, visit_code_sequence\n                order by subject_identifier, report_datetime, visit_code, visit_code_sequence\n            )\n     select *, get_random_uuid() as id, now() as created, 'intecomm_reports.subjectstransferred_view' as report_model from (SELECT prn.subject_identifier, prn.site_id, consent_datetime AS consented, v.visit_code, v.report_datetime AS last_visit, prn.report_datetime AS transferred, CAST(CAST(AGE(CAST(prn.report_datetime AS TIMESTAMP), CAST(consent_datetime AS TIMESTAMP)) AS BIGINT) AS DOUBLE PRECISION) / NULLIF(30, 0) AS months, eof.offstudy_datetime AS offstudy, eof.report_datetime AS last_seen FROM intecomm_prn_subjecttransfer AS prn LEFT JOIN edc_registration_registeredsubject AS rs ON prn.subject_identifier = rs.subject_identifier LEFT JOIN intecomm_prn_endofstudy AS eof ON prn.subject_identifier = eof.subject_identifier LEFT JOIN (SELECT DISTINCT subject_identifier, LAST_VALUE(report_datetime) OVER w AS report_datetime, LAST_VALUE(visit_code) OVER w AS visit_code FROM visits WINDOW w AS (PARTITION BY \"subject_identifier\")) AS v ON prn.subject_identifier = v.subject_identifier ORDER BY CAST(CAST(AGE(CAST(prn.report_datetime AS TIMESTAMP), CAST(consent_datetime AS TIMESTAMP)) AS BIGINT) AS DOUBLE PRECISION) / NULLIF(30, 0) DESC NULLS LAST, subject_identifier NULLS FIRST) as A ORDER BY site_id, subject_identifier",
                "subjects_transferred_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "subjects_transferred_view", engine="django.db.backends.postgresql"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "with visits as (\n                select subject_identifier, report_datetime, visit_code, visit_code_sequence\n                from intecomm_subject_subjectvisit\n                group by subject_identifier, report_datetime, visit_code, visit_code_sequence\n                order by subject_identifier, report_datetime, visit_code, visit_code_sequence\n            )\n     select *, uuid() as id, datetime() as created, 'intecomm_reports.subjectstransferred_view' as report_model from (SELECT prn.subject_identifier, prn.site_id, consent_datetime AS consented, v.visit_code, v.report_datetime AS last_visit, prn.report_datetime AS transferred, CAST(CAST((JULIANDAY(prn.report_datetime) - JULIANDAY(consent_datetime)) AS INTEGER) AS REAL) / 30 AS months, eof.offstudy_datetime AS offstudy, eof.report_datetime AS last_seen FROM intecomm_prn_subjecttransfer AS prn LEFT JOIN edc_registration_registeredsubject AS rs ON prn.subject_identifier = rs.subject_identifier LEFT JOIN intecomm_prn_endofstudy AS eof ON prn.subject_identifier = eof.subject_identifier LEFT JOIN (SELECT DISTINCT subject_identifier, LAST_VALUE(report_datetime) OVER w AS report_datetime, LAST_VALUE(visit_code) OVER w AS visit_code FROM visits WINDOW w AS (PARTITION BY \"subject_identifier\")) AS v ON prn.subject_identifier = v.subject_identifier ORDER BY CAST(CAST((JULIANDAY(prn.report_datetime) - JULIANDAY(consent_datetime)) AS INTEGER) AS REAL) / 30 DESC, subject_identifier) as A ORDER BY site_id, subject_identifier",
                "subjects_transferred_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "subjects_transferred_view", engine="django.db.backends.sqlite3"
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'intecomm_reports.vl_view' as `report_model` from (SELECT B1.* FROM (SELECT hivreview.*, dx.baseline_date, TIMESTAMPDIFF(MONTH, baseline_date, vl_date) AS m FROM (SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN CAST(crf.report_datetime AS DATE) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivinitialreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE NOT vl IS NULL UNION SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN CAST(crf.report_datetime AS DATE) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id) AS hivreview LEFT JOIN intecomm_reports_diagnoses AS dx ON dx.subject_identifier = hivreview.subject_identifier WHERE NOT vl_value IS NULL) AS B1) as A ORDER BY site_id, subject_identifier",
                "intecomm_reports_vl_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "intecomm_reports_vl_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.vl_view' as report_model from (SELECT B1.* FROM (SELECT hivreview.*, dx.baseline_date, TIMESTAMPDIFF(vl_date, baseline_date, MONTH) AS m FROM (SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN CAST(crf.report_datetime AS DATE) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivinitialreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE NOT vl IS NULL UNION SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN CAST(crf.report_datetime AS DATE) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id) AS hivreview LEFT JOIN intecomm_reports_diagnoses AS dx ON dx.subject_identifier = hivreview.subject_identifier WHERE NOT vl_value IS NULL) AS B1) as A ORDER BY site_id, subject_identifier",
                "intecomm_reports_vl_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "intecomm_reports_vl_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.vl_view' as report_model from (SELECT B1.* FROM (SELECT hivreview.*, dx.baseline_date, TIMESTAMPDIFF(vl_date, baseline_date, MONTH) AS m FROM (SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN DATE(crf.report_datetime) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivinitialreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE NOT vl IS NULL UNION SELECT subject_identifier, crf.site_id, vl AS vl_value, CASE WHEN drawn_date IS NULL THEN DATE(crf.report_datetime) ELSE drawn_date END AS vl_date FROM intecomm_subject_hivreview AS crf LEFT JOIN intecomm_subject_subjectvisit AS v ON v.id = crf.subject_visit_id) AS hivreview LEFT JOIN intecomm_reports_diagnoses AS dx ON dx.subject_identifier = hivreview.subject_identifier WHERE NOT vl_value IS NULL) AS B1) as A ORDER BY site_id, subject_identifier",
                "intecomm_reports_vl_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "", "intecomm_reports_vl_view", engine="django.db.backends.sqlite3"
            ),
            atomic=False,
        ),
    ]
