Blob Blame Raw
"""
Add modules that contain lessons and assessments.

Revision ID: 419e2215ef91
Revises: 02c2b594dde0
Create Date: 2017-03-03 00:17:21.733230

"""

# revision identifiers, used by Alembic.
revision = '419e2215ef91'
down_revision = '02c2b594dde0'

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils.types.arrow


def upgrade():
    op.create_table('module',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=255), nullable=True),
                    sa.Column('description', sa.Text(), nullable=True),
                    sa.Column('organisation_id', sa.Integer(), nullable=True),
                    sa.Column('requirements', sa.Text(), nullable=True),
                    sa.Column('deleted', sa.Boolean(), nullable=True),
                    sa.Column('created_on',
                              sqlalchemy_utils.types.arrow.ArrowType(),
                              nullable=True),
                    sa.Column('created_by_id', sa.Integer(), nullable=True),
                    sa.Column('signed_off_on',
                              sqlalchemy_utils.types.arrow.ArrowType(),
                              nullable=True),
                    sa.Column('signed_off_by_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(['created_by_id'], ['user.id'], ),
                    sa.ForeignKeyConstraint(['organisation_id'],
                                            ['organisation.id'], ),
                    sa.ForeignKeyConstraint(['signed_off_by_id'],
                                            ['user.id'], ),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.create_table('course_module_association',
                    sa.Column('course_id', sa.Integer(), nullable=False),
                    sa.Column('module_id', sa.Integer(), nullable=False),
                    sa.Column('order', sa.Integer(), nullable=True),
                    sa.Column('graded', sa.Boolean(), nullable=True),
                    sa.Column('weighting', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
                    sa.ForeignKeyConstraint(['module_id'], ['module.id'], ),
                    sa.PrimaryKeyConstraint('course_id', 'module_id')
                    )
    op.create_table('moduleinstance',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('module_id', sa.Integer(), nullable=True),
                    sa.Column('course_instance_id', sa.Integer(),
                              nullable=True),
                    sa.Column('deleted', sa.Boolean(), nullable=True),
                    sa.ForeignKeyConstraint(['course_instance_id'],
                                            ['courseinstance.id'], ),
                    sa.ForeignKeyConstraint(['module_id'], ['module.id'], ),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.add_column('assessment',
                  sa.Column('module_id', sa.Integer(), nullable=True))
    op.add_column('assessment',
                  sa.Column('pass_mark', sa.Integer(), nullable=True))
    op.drop_constraint('assessment_course_id_fkey', 'assessment',
                       type_='foreignkey')
    op.create_foreign_key(None, 'assessment', 'module', ['module_id'], ['id'])
    op.drop_column('assessment', 'course_id')
    op.drop_column('assessment', 'pass_grade')
    op.add_column('assessmentcriteria',
                  sa.Column('module_id', sa.Integer(), nullable=True))
    op.drop_constraint('assessmentcriteria_course_id_fkey',
                       'assessmentcriteria', type_='foreignkey')
    op.create_foreign_key(None, 'assessmentcriteria', 'module', ['module_id'],
                          ['id'])
    op.drop_column('assessmentcriteria', 'course_id')
    op.add_column('assessmentinstance',
                  sa.Column('module_instance_id', sa.Integer(), nullable=True))
    op.drop_constraint('assessmentinstance_course_instance_id_fkey',
                       'assessmentinstance', type_='foreignkey')
    op.create_foreign_key(None, 'assessmentinstance', 'moduleinstance',
                          ['module_instance_id'], ['id'])
    op.drop_column('assessmentinstance', 'course_instance_id')
    op.add_column('lesson',
                  sa.Column('module_id', sa.Integer(), nullable=True))
    op.drop_constraint('lesson_course_id_fkey', 'lesson', type_='foreignkey')
    op.create_foreign_key(None, 'lesson', 'module', ['module_id'], ['id'])
    op.drop_column('lesson', 'course_id')
    op.add_column('lessoninstance',
                  sa.Column('module_instance_id', sa.Integer(), nullable=True))
    op.drop_constraint('lessoninstance_course_instance_id_fkey',
                       'lessoninstance', type_='foreignkey')
    op.create_foreign_key(None, 'lessoninstance', 'moduleinstance',
                          ['module_instance_id'], ['id'])
    op.drop_column('lessoninstance', 'course_instance_id')


def downgrade():
    op.add_column('lessoninstance',
                  sa.Column('course_instance_id', sa.INTEGER(),
                            autoincrement=False, nullable=True))
    op.drop_constraint(None, 'lessoninstance', type_='foreignkey')
    op.create_foreign_key('lessoninstance_course_instance_id_fkey',
                          'lessoninstance', 'courseinstance',
                          ['course_instance_id'], ['id'])
    op.drop_column('lessoninstance', 'module_instance_id')
    op.add_column('lesson',
                  sa.Column('course_id', sa.INTEGER(), autoincrement=False,
                            nullable=True))
    op.drop_constraint(None, 'lesson', type_='foreignkey')
    op.create_foreign_key('lesson_course_id_fkey', 'lesson', 'course',
                          ['course_id'], ['id'])
    op.drop_column('lesson', 'module_id')
    op.add_column('assessmentinstance',
                  sa.Column('course_instance_id', sa.INTEGER(),
                            autoincrement=False, nullable=True))
    op.drop_constraint(None, 'assessmentinstance', type_='foreignkey')
    op.create_foreign_key('assessmentinstance_course_instance_id_fkey',
                          'assessmentinstance', 'courseinstance',
                          ['course_instance_id'], ['id'])
    op.drop_column('assessmentinstance', 'module_instance_id')
    op.add_column('assessmentcriteria',
                  sa.Column('course_id', sa.INTEGER(), autoincrement=False,
                            nullable=True))
    op.drop_constraint(None, 'assessmentcriteria', type_='foreignkey')
    op.create_foreign_key('assessmentcriteria_course_id_fkey',
                          'assessmentcriteria', 'course', ['course_id'],
                          ['id'])
    op.drop_column('assessmentcriteria', 'module_id')
    op.add_column('assessment', sa.Column('pass_grade', sa.VARCHAR(length=255),
                                          autoincrement=False, nullable=True))
    op.add_column('assessment',
                  sa.Column('course_id', sa.INTEGER(), autoincrement=False,
                            nullable=True))
    op.drop_constraint(None, 'assessment', type_='foreignkey')
    op.create_foreign_key('assessment_course_id_fkey', 'assessment', 'course',
                          ['course_id'], ['id'])
    op.drop_column('assessment', 'pass_mark')
    op.drop_column('assessment', 'module_id')
    op.drop_table('moduleinstance')
    op.drop_table('course_module_association')
    op.drop_table('module')