# Generated by Django 5.1.2 on 2024-10-31 03:02

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


class Migration(migrations.Migration):

    dependencies = [
        ("intecomm_reports", "0027_delete_onstudymissinglabvalues_and_more"),
    ]

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'intecomm_reports.vl' 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(
                "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",
            ),
            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' 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(
                "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",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'intecomm_reports.vl' 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(
                "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",
            ),
            atomic=False,
        ),
    ]
