"""Initial Build

Revision ID: 34935013ab33
Revises:
Create Date: 2020-03-10 23:05:03.634539

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "34935013ab33"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "channel",
        sa.Column("id", sa.String(length=32), nullable=False),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.Column("active", sa.Boolean(), nullable=True),
        sa.Column("infos", sa.JSON(), nullable=True),
        sa.Column("hub_infos", sa.JSON(), nullable=True),
        sa.Column("subscribe_timestamp", sa.DateTime(), nullable=True),
        sa.Column("unsubscribe_timestamp", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_channel")),
    )
    op.create_table(
        "user",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("_password_hash", sa.LargeBinary(length=128), nullable=False),
        sa.Column("admin", sa.Boolean(), nullable=True),
        sa.Column("_pushover_key", sa.String(length=32), nullable=True),
        sa.Column("_youtube_credentials", sa.JSON(), nullable=True),
        sa.Column("_line_notify_credentials", sa.String(length=64), nullable=True),
        sa.Column("_dropbox_credentials", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("username", name=op.f("pk_user")),
    )
    op.create_table(
        "notification",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("initiator", sa.String(length=16), nullable=False),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.Column(
            "service", sa.Enum("Pushover", "LineNotify", name="service"), nullable=True
        ),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("kwargs", sa.JSON(), nullable=True),
        sa.Column("sent_timestamp", sa.DateTime(), nullable=True),
        sa.Column("response", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_notification_username_user")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_notification")),
    )
    with op.batch_alter_table("notification", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_notification_sent_timestamp"),
            ["sent_timestamp"],
            unique=False,
        )

    op.create_table(
        "subscription",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("channel_id", sa.String(length=32), nullable=False),
        sa.Column("subscribe_timestamp", sa.DateTime(), nullable=True),
        sa.Column("unsubscribe_timestamp", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["channel.id"],
            name=op.f("fk_subscription_channel_id_channel"),
        ),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_subscription_username_user")
        ),
        sa.PrimaryKeyConstraint("username", "channel_id", name=op.f("pk_subscription")),
        sa.UniqueConstraint("channel_id", name=op.f("uq_subscription_channel_id")),
        sa.UniqueConstraint("username", name=op.f("uq_subscription_username")),
    )
    op.create_table(
        "tag",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=64), nullable=False),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_tag_username_user")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_tag")),
    )
    with op.batch_alter_table("tag", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_tag_name"), ["name"], unique=False)

    op.create_table(
        "video",
        sa.Column("id", sa.String(length=16), nullable=False),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.Column("uploaded_timestamp", sa.DateTime(), nullable=True),
        sa.Column("details", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"], ["channel.id"], name=op.f("fk_video_channel_id_channel")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_video")),
    )
    op.create_table(
        "action",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=32), nullable=False),
        sa.Column(
            "type",
            sa.Enum("Notification", "Playlist", "Download", name="actiontype"),
            nullable=False,
        ),
        sa.Column("details", sa.JSON(), nullable=True),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["subscription.channel_id"],
            name=op.f("fk_action_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username", "channel_id"],
            ["subscription.username", "subscription.channel_id"],
            name=op.f("fk_action_username_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username"],
            ["subscription.username"],
            name=op.f("fk_action_username_subscription"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_action")),
    )
    op.create_table(
        "callback",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("type", sa.String(length=32), nullable=True),
        sa.Column("timestamp", sa.DateTime(), nullable=True),
        sa.Column("infos", sa.JSON(), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.Column("video_id", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"], ["channel.id"], name=op.f("fk_callback_channel_id_channel")
        ),
        sa.ForeignKeyConstraint(
            ["video_id"], ["video.id"], name=op.f("fk_callback_video_id_video")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_callback")),
    )
    with op.batch_alter_table("callback", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_callback_timestamp"), ["timestamp"], unique=False
        )

    op.create_table(
        "subscription_tag",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("channel_id", sa.String(length=32), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["subscription.channel_id"],
            name=op.f("fk_subscription_tag_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["tag_id"], ["tag.id"], name=op.f("fk_subscription_tag_tag_id_tag")
        ),
        sa.ForeignKeyConstraint(
            ["username", "channel_id"],
            ["subscription.username", "subscription.channel_id"],
            name=op.f("fk_subscription_tag_username_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username"],
            ["subscription.username"],
            name=op.f("fk_subscription_tag_username_subscription"),
        ),
        sa.PrimaryKeyConstraint(
            "username", "channel_id", "tag_id", name=op.f("pk_subscription_tag")
        ),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("subscription_tag")
    with op.batch_alter_table("callback", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_callback_timestamp"))

    op.drop_table("callback")
    op.drop_table("action")
    op.drop_table("video")
    with op.batch_alter_table("tag", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_tag_name"))

    op.drop_table("tag")
    op.drop_table("subscription")
    with op.batch_alter_table("notification", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_notification_sent_timestamp"))

    op.drop_table("notification")
    op.drop_table("user")
    op.drop_table("channel")
    # ### end Alembic commands ###

"""Initial Build

Revision ID: 34935013ab33
Revises:
Create Date: 2020-03-10 23:05:03.634539

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "34935013ab33"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "channel",
        sa.Column("id", sa.String(length=32), nullable=False),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.Column("active", sa.Boolean(), nullable=True),
        sa.Column("infos", sa.JSON(), nullable=True),
        sa.Column("hub_infos", sa.JSON(), nullable=True),
        sa.Column("subscribe_timestamp", sa.DateTime(), nullable=True),
        sa.Column("unsubscribe_timestamp", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_channel")),
    )
    op.create_table(
        "user",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("_password_hash", sa.LargeBinary(length=128), nullable=False),
        sa.Column("admin", sa.Boolean(), nullable=True),
        sa.Column("_pushover_key", sa.String(length=32), nullable=True),
        sa.Column("_youtube_credentials", sa.JSON(), nullable=True),
        sa.Column("_line_notify_credentials", sa.String(length=64), nullable=True),
        sa.Column("_dropbox_credentials", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("username", name=op.f("pk_user")),
    )
    op.create_table(
        "notification",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("initiator", sa.String(length=16), nullable=False),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.Column(
            "service", sa.Enum("Pushover", "LineNotify", name="service"), nullable=True
        ),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("kwargs", sa.JSON(), nullable=True),
        sa.Column("sent_timestamp", sa.DateTime(), nullable=True),
        sa.Column("response", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_notification_username_user")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_notification")),
    )
    with op.batch_alter_table("notification", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_notification_sent_timestamp"),
            ["sent_timestamp"],
            unique=False,
        )

    op.create_table(
        "subscription",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("channel_id", sa.String(length=32), nullable=False),
        sa.Column("subscribe_timestamp", sa.DateTime(), nullable=True),
        sa.Column("unsubscribe_timestamp", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["channel.id"],
            name=op.f("fk_subscription_channel_id_channel"),
        ),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_subscription_username_user")
        ),
        sa.PrimaryKeyConstraint("username", "channel_id", name=op.f("pk_subscription")),
        sa.UniqueConstraint("channel_id", name=op.f("uq_subscription_channel_id")),
        sa.UniqueConstraint("username", name=op.f("uq_subscription_username")),
    )
    op.create_table(
        "tag",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=64), nullable=False),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["username"], ["user.username"], name=op.f("fk_tag_username_user")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_tag")),
    )
    with op.batch_alter_table("tag", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_tag_name"), ["name"], unique=False)

    op.create_table(
        "video",
        sa.Column("id", sa.String(length=16), nullable=False),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.Column("uploaded_timestamp", sa.DateTime(), nullable=True),
        sa.Column("details", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"], ["channel.id"], name=op.f("fk_video_channel_id_channel")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_video")),
    )
    op.create_table(
        "action",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=32), nullable=False),
        sa.Column(
            "type",
            sa.Enum("Notification", "Playlist", "Download", name="actiontype"),
            nullable=False,
        ),
        sa.Column("details", sa.JSON(), nullable=True),
        sa.Column("username", sa.String(length=32), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["subscription.channel_id"],
            name=op.f("fk_action_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username", "channel_id"],
            ["subscription.username", "subscription.channel_id"],
            name=op.f("fk_action_username_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username"],
            ["subscription.username"],
            name=op.f("fk_action_username_subscription"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_action")),
    )
    op.create_table(
        "callback",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("type", sa.String(length=32), nullable=True),
        sa.Column("timestamp", sa.DateTime(), nullable=True),
        sa.Column("infos", sa.JSON(), nullable=True),
        sa.Column("channel_id", sa.String(length=32), nullable=True),
        sa.Column("video_id", sa.String(length=32), nullable=True),
        sa.ForeignKeyConstraint(
            ["channel_id"], ["channel.id"], name=op.f("fk_callback_channel_id_channel")
        ),
        sa.ForeignKeyConstraint(
            ["video_id"], ["video.id"], name=op.f("fk_callback_video_id_video")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_callback")),
    )
    with op.batch_alter_table("callback", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_callback_timestamp"), ["timestamp"], unique=False
        )

    op.create_table(
        "subscription_tag",
        sa.Column("username", sa.String(length=32), nullable=False),
        sa.Column("channel_id", sa.String(length=32), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["channel_id"],
            ["subscription.channel_id"],
            name=op.f("fk_subscription_tag_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["tag_id"], ["tag.id"], name=op.f("fk_subscription_tag_tag_id_tag")
        ),
        sa.ForeignKeyConstraint(
            ["username", "channel_id"],
            ["subscription.username", "subscription.channel_id"],
            name=op.f("fk_subscription_tag_username_channel_id_subscription"),
        ),
        sa.ForeignKeyConstraint(
            ["username"],
            ["subscription.username"],
            name=op.f("fk_subscription_tag_username_subscription"),
        ),
        sa.PrimaryKeyConstraint(
            "username", "channel_id", "tag_id", name=op.f("pk_subscription_tag")
        ),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("subscription_tag")
    with op.batch_alter_table("callback", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_callback_timestamp"))

    op.drop_table("callback")
    op.drop_table("action")
    op.drop_table("video")
    with op.batch_alter_table("tag", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_tag_name"))

    op.drop_table("tag")
    op.drop_table("subscription")
    with op.batch_alter_table("notification", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_notification_sent_timestamp"))

    op.drop_table("notification")
    op.drop_table("user")
    op.drop_table("channel")
    # ### end Alembic commands ###

Initial Build  Revision ID: 34935013ab33 Revises: Create Date: 2020-03-10 23:05:03.634539 # revision identifiers, used by Alembic. # ### commands auto generated by Alembic - please adjust! ### # ### end Alembic commands ### # ### commands auto generated by Alembic - please adjust! ### # ### end Alembic commands ###