# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Add Vulnerability and release association tables

Revision ID: 1dbb95161e5a
Revises: 08ccc59d9857
Create Date: 2021-07-14 19:01:16.356312
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "1dbb95161e5a"
down_revision = "08ccc59d9857"


def upgrade():
    op.create_table(
        "vulnerabilities",
        sa.Column("source", sa.String(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("link", sa.String(), nullable=True),
        sa.Column("aliases", postgresql.ARRAY(sa.String()), nullable=True),
        sa.PrimaryKeyConstraint("source", "id"),
    )
    op.create_table(
        "release_vulnerabilities",
        sa.Column("release_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("vulnerability_source", sa.String(), nullable=False),
        sa.Column("vulnerability_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["release_id"], ["releases.id"], onupdate="CASCADE", ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["vulnerability_source", "vulnerability_id"],
            ["vulnerabilities.source", "vulnerabilities.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
    )


def downgrade():
    op.drop_table("release_vulnerabilities")
    op.drop_table("vulnerabilities")
