Blob Blame Raw
"""
Move from MongoDB for storage to filesystem.

This adds the Documentfile table to track documents uploaded.

Revision ID: 29c9fbe9c7f
Revises: 1d0f09ee422
Create Date: 2016-02-18 23:25:13.109485

"""

# revision identifiers, used by Alembic.
revision = '29c9fbe9c7f'
down_revision = '1d0f09ee422'

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


def upgrade():
    op.create_table('documentfile',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('uuid', postgresql.UUID(), nullable=True),
                    sa.Column('filename', sa.String(length=255),
                              nullable=True),
                    sa.Column('mimetype', sa.String(length=255),
                              nullable=True),
                    sa.Column('purpose', sa.String(length=255), nullable=True),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.add_column('assessmentsubmission',
                  sa.Column('document_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'assessmentsubmission', 'documentfile',
                          ['document_id'], ['id'])
    op.drop_column('assessmentsubmission', 'datastore_id')
    op.add_column('resource',
                  sa.Column('creation_date',
                            sqlalchemy_utils.types.arrow.ArrowType(),
                            nullable=False))
    op.add_column('resource',
                  sa.Column('document_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'resource', 'documentfile', ['document_id'],
                          ['id'])
    op.drop_column('resource', 'datastore_id')


def downgrade():
    op.add_column('resource', sa.Column('datastore_id', sa.VARCHAR(length=255),
                                        autoincrement=False, nullable=True))
    op.drop_constraint(None, 'resource', type_='foreignkey')
    op.drop_column('resource', 'document_id')
    op.drop_column('resource', 'creation_date')
    op.add_column('assessmentsubmission',
                  sa.Column('datastore_id', sa.VARCHAR(length=255),
                            autoincrement=False, nullable=True))
    op.drop_constraint(None, 'assessmentsubmission', type_='foreignkey')
    op.drop_column('assessmentsubmission', 'document_id')
    op.drop_table('documentfile')