"""initial autogen revision

Revision ID: 74df8fa5a35f
Revises:
Create Date: 2017-02-06 20:48:47.191133

"""
import datetime
from alembic import op
import sqlalchemy as sa
import mishmash
from mishmash.orm import Library, Artist, Album, Track, Image, Tag, Meta, ALBUM_TYPE_IDS
from mishmash.orm import AlbumDate


# revision identifiers, used by Alembic.
revision = '74df8fa5a35f'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('images',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('type', sa.Enum('FRONT_COVER',
                                              'BACK_COVER',
                                              'MISC_COVER',
                                              'LOGO',
                                              'ARTIST',
                                              'LIVE',
                                              name='image_types'),
                              nullable=False),
                    sa.Column('mime_type',
                              sa.String(length=Image.MIMETYPE_LIMIT),
                              nullable=False),
                    sa.Column('md5', sa.String(length=Image.MD5_LIMIT),
                              nullable=False),
                    sa.Column('size', sa.Integer(), nullable=False),
                    sa.Column('description', sa.String(length=Image.DESC_LIMIT),
                              nullable=False),
                    sa.Column('data', sa.LargeBinary(), nullable=False),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_images'))
    )

    lib_t = op.create_table('libraries',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.Unicode(length=Library.NAME_LIMIT),
                              nullable=False),
                    sa.Column('last_sync', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_libraries')),
                    sa.UniqueConstraint('name', name=op.f('uq_libraries_name'))
    )
    op.bulk_insert(lib_t,
                   [
                       {"name": mishmash.orm.NULL_LIB_NAME},
                       {"name": mishmash.orm.MAIN_LIB_NAME},
                   ])

    meta_t = op.create_table('meta',
                    sa.Column('version',
                              sa.String(length=Meta.VERSION_LIMIT),
                              nullable=False),
                    sa.Column('last_sync', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('version', name=op.f('pk_meta'))
    )
    op.bulk_insert(meta_t, [{"version": mishmash.version}])

    artists_t = op.create_table('artists',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.Unicode(length=Artist.NAME_LIMIT),
                              nullable=False),
                    sa.Column('sort_name',
                              sa.Unicode(length=Artist.SORT_NAME_LIMIT),
                              nullable=False),
                    sa.Column('date_added', sa.DateTime(), nullable=False),
                    sa.Column('origin_city',
                              sa.Unicode(length=Artist.CITY_LIMIT),
                              nullable=True),
                    sa.Column('origin_state',
                              sa.Unicode(length=Artist.STATE_LIMIT),
                              nullable=True),
                    sa.Column('origin_country',
                              sa.String(length=Artist.COUNTRY_LIMIT),
                              nullable=True),
                    sa.Column('lib_id', sa.Integer(), nullable=False),
                    sa.ForeignKeyConstraint(
                        ['lib_id'], ['libraries.id'],
                        name=op.f('fk_artists_lib_id_libraries')),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_artists')),
                    sa.UniqueConstraint('name', 'origin_city', 'origin_state',
                                        'origin_country', 'lib_id',
                                        name=op.f('uq_artists_name'))
    )
    op.bulk_insert(artists_t,
                   [{"lib_id": mishmash.orm.NULL_LIB_ID,
                     "name": mishmash.orm.VARIOUS_ARTISTS_NAME,
                     "sort_name": mishmash.orm.getSortName(
                                    mishmash.orm.VARIOUS_ARTISTS_NAME),
                     "date_added": datetime.datetime.now(),
                    }])

    op.create_index(op.f('ix_artists_lib_id'), 'artists', ['lib_id'],
                    unique=False)
    op.create_index(op.f('ix_artists_name'), 'artists', ['name'],
                    unique=False)
    op.create_table('tags',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.Unicode(length=Tag.NAME_LIMIT),
                              nullable=False),
                    sa.Column('lib_id', sa.Integer(), nullable=False),
                    sa.ForeignKeyConstraint(
                        ['lib_id'], ['libraries.id'],
                        name=op.f('fk_tags_lib_id_libraries')),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_tags')),
                    sa.UniqueConstraint('name', 'lib_id',
                                        name=op.f('uq_tags_name'))
    )
    op.create_index(op.f('ix_tags_lib_id'), 'tags', ['lib_id'], unique=False)
    op.create_table('albums',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('title', sa.Unicode(length=Album.TITLE_LIMIT),
                              nullable=False),
                    sa.Column('type', sa.Enum(*ALBUM_TYPE_IDS, name='album_types'),
                              nullable=False),
                    sa.Column('date_added', sa.DateTime(), nullable=False),
                    sa.Column('release_date',
                              AlbumDate(length=Album.DATE_LIMIT),
                              nullable=True),
                    sa.Column('original_release_date',
                              AlbumDate(length=Album.DATE_LIMIT),
                              nullable=True),
                    sa.Column('recording_date',
                              AlbumDate(length=Album.DATE_LIMIT),
                              nullable=True),
                    sa.Column('artist_id', sa.Integer(), nullable=False),
                    sa.Column('lib_id', sa.Integer(), nullable=False),
                    sa.ForeignKeyConstraint(
                        ['artist_id'], ['artists.id'],
                        name=op.f('fk_albums_artist_id_artists')),
                    sa.ForeignKeyConstraint(
                        ['lib_id'], ['libraries.id'],
                        name=op.f('fk_albums_lib_id_libraries')),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_albums')),
                    sa.UniqueConstraint('title', 'artist_id', 'lib_id',
                                        'release_date', 'recording_date', 'original_release_date',
                                        name=op.f('uq_albums_title'))
    )
    op.create_index(op.f('ix_albums_artist_id'), 'albums', ['artist_id'],
                    unique=False)
    op.create_index(op.f('ix_albums_lib_id'), 'albums', ['lib_id'],
                    unique=False)
    op.create_index(op.f('ix_albums_title'), 'albums', ['title'],
                    unique=False)
    op.create_table('artist_images',
                    sa.Column('artist_id', sa.Integer(), nullable=True),
                    sa.Column('img_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(
                        ['artist_id'], ['artists.id'],
                        name=op.f('fk_artist_images_artist_id_artists')),
                    sa.ForeignKeyConstraint(
                        ['img_id'], ['images.id'],
                        name=op.f('fk_artist_images_img_id_images'))
    )
    op.create_table('artist_tags',
                    sa.Column('artist_id', sa.Integer(), nullable=True),
                    sa.Column('tag_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['artist_id'], ['artists.id'],
                            name=op.f('fk_artist_tags_artist_id_artists')),
    sa.ForeignKeyConstraint(['tag_id'], ['tags.id'],
                            name=op.f('fk_artist_tags_tag_id_tags'))
    )
    op.create_table('album_images',
                    sa.Column('album_id', sa.Integer(), nullable=True),
                    sa.Column('img_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(
                        ['album_id'], ['albums.id'],
                        name=op.f('fk_album_images_album_id_albums')),
                    sa.ForeignKeyConstraint(
                        ['img_id'], ['images.id'],
                        name=op.f('fk_album_images_img_id_images'))
    )
    op.create_table('album_tags',
                    sa.Column('album_id', sa.Integer(), nullable=True),
                    sa.Column('tag_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(
                        ['album_id'], ['albums.id'],
                        name=op.f('fk_album_tags_album_id_albums')),
                    sa.ForeignKeyConstraint(
                        ['tag_id'], ['tags.id'],
                        name=op.f('fk_album_tags_tag_id_tags'))
    )
    op.create_table('tracks',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('path', sa.String(length=Track.PATH_LIMIT),
                              nullable=False),
                    sa.Column('size_bytes', sa.Integer(), nullable=False),
                    sa.Column('ctime', sa.DateTime(), nullable=False),
                    sa.Column('mtime', sa.DateTime(), nullable=False),
                    sa.Column('date_added', sa.DateTime(), nullable=False),
                    sa.Column('time_secs', sa.Float(), nullable=False),
                    sa.Column('title', sa.Unicode(length=Track.TITLE_LIMIT),
                              nullable=False),
                    sa.Column('track_num', sa.SmallInteger(), nullable=True),
                    sa.Column('track_total', sa.SmallInteger(), nullable=True),
                    sa.Column('media_num', sa.SmallInteger(), nullable=True),
                    sa.Column('media_total', sa.SmallInteger(), nullable=True),
                    sa.Column('bit_rate', sa.SmallInteger(), nullable=True),
                    sa.Column('variable_bit_rate', sa.Boolean(name="vbr_bool"),
                              nullable=True),
                    sa.Column('artist_id', sa.Integer(), nullable=False),
                    sa.Column('album_id', sa.Integer(), nullable=True),
                    sa.Column('lib_id', sa.Integer(), nullable=False),
                    sa.Column('metadata_format',
                              sa.Enum(*['ID3v1.0', 'ID3v1.1', 'ID3v2.2',
                                        'ID3v2.3', 'ID3v2.4'],
                                      name='metadata_formats')),
                    sa.ForeignKeyConstraint(
                        ['album_id'], ['albums.id'],
                        name=op.f('fk_tracks_album_id_albums')),
                    sa.ForeignKeyConstraint(
                        ['artist_id'], ['artists.id'],
                        name=op.f('fk_tracks_artist_id_artists')),
                    sa.ForeignKeyConstraint(
                        ['lib_id'], ['libraries.id'],
                        name=op.f('fk_tracks_lib_id_libraries')),
                    sa.PrimaryKeyConstraint('id', name=op.f('pk_tracks')),
                    sa.UniqueConstraint('path', 'lib_id',
                                        name=op.f('uq_tracks_path'))
    )
    op.create_index(op.f('ix_tracks_album_id'), 'tracks', ['album_id'],
                    unique=False)
    op.create_index(op.f('ix_tracks_artist_id'), 'tracks', ['artist_id'],
                    unique=False)
    op.create_index(op.f('ix_tracks_lib_id'), 'tracks', ['lib_id'],
                    unique=False)
    op.create_index(op.f('ix_tracks_path'), 'tracks', ['path'], unique=False)
    op.create_index(op.f('ix_tracks_title'), 'tracks', ['title'], unique=False)
    op.create_table('track_tags',
                    sa.Column('track_id', sa.Integer(), nullable=True),
                    sa.Column('tag_id', sa.Integer(), nullable=True),
                              sa.ForeignKeyConstraint(
                                  ['tag_id'], ['tags.id'],
                                  name=op.f('fk_track_tags_tag_id_tags')),
                              sa.ForeignKeyConstraint(
                                  ['track_id'], ['tracks.id'],
                                  name=op.f('fk_track_tags_track_id_tracks'))
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('track_tags')
    op.drop_index(op.f('ix_tracks_title'), table_name='tracks')
    op.drop_index(op.f('ix_tracks_path'), table_name='tracks')
    op.drop_index(op.f('ix_tracks_lib_id'), table_name='tracks')
    op.drop_index(op.f('ix_tracks_artist_id'), table_name='tracks')
    op.drop_index(op.f('ix_tracks_album_id'), table_name='tracks')
    op.drop_table('tracks')
    op.drop_table('album_tags')
    op.drop_table('album_images')
    op.drop_table('artist_tags')
    op.drop_table('artist_images')
    op.drop_index(op.f('ix_albums_title'), table_name='albums')
    op.drop_index(op.f('ix_albums_lib_id'), table_name='albums')
    op.drop_index(op.f('ix_albums_artist_id'), table_name='albums')
    op.drop_table('albums')
    op.drop_index(op.f('ix_tags_lib_id'), table_name='tags')
    op.drop_table('tags')
    op.drop_index(op.f('ix_artists_name'), table_name='artists')
    op.drop_index(op.f('ix_artists_lib_id'), table_name='artists')
    op.drop_table('artists')
    op.drop_table('meta')
    op.drop_table('libraries')
    op.drop_table('images')
    # ### end Alembic commands ###
