# Generated by Django 2.1.1 on 2018-09-25 10:05

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [("coursera", "0004_create_course_grades_view")]

    operations = [
        migrations.RunSQL(
            sql=[
                """
                CREATE MATERIALIZED VIEW course_branch_lessons_view
                AS
                SELECT
                MD5(MD5(course_branch_id) || course_lesson_id)::varchar(50) as lesson_id,
                course_branch_id,
                course_lesson_id,
                MD5(MD5(course_branch_id) || course_module_id)::varchar(50) as module_id,
                course_branch_lesson_order,
                course_branch_lesson_name
                FROM
                course_branch_lessons;
                """,
                """
                CREATE UNIQUE INDEX ON course_branch_lessons_view (lesson_id)
                """,
                """
                CREATE INDEX ON course_branch_lessons_view (module_id)
                """,
            ],
            reverse_sql="""
            DROP MATERIALIZED VIEW IF EXISTS course_branch_lessons_view
            """,
        ),
        migrations.RunSQL(
            sql=[
                """
                CREATE MATERIALIZED VIEW course_branch_items_view
                AS
                SELECT
                MD5(MD5(course_branch_id) || course_item_id)::varchar(50) as item_id,
                course_branch_id,
                course_item_id,
                MD5(MD5(course_branch_id) || course_lesson_id)::varchar(50) as lesson_id,
                course_branch_item_order,
                course_item_type_id,
                course_branch_item_name,
                course_branch_item_optional,
                atom_id,
                atom_version_id,
                course_branch_atom_is_frozen
                FROM
                course_branch_items;
                """,
                """
                CREATE UNIQUE INDEX ON course_branch_items_view (item_id)
                """,
                """
                CREATE INDEX ON course_branch_items_view (lesson_id)
                """,
            ],
            reverse_sql="""
            DROP MATERIALIZED VIEW IF EXISTS course_branch_items_view
            """,
        ),
    ]
