"""Initial migration

Revision ID: bbe766649a99
Revises:
Create Date: 2024-02-29 14:23:07.749071

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import sqlite
from sqlalchemy.engine.reflection import Inspector

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


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('release')
    op.drop_table('users')
    # ### end Alembic commands ###


def upgrade():
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)
    tables = inspector.get_table_names()

    if "users" not in tables:
        op.create_table('users',
                        sa.Column('id', sa.INTEGER(), nullable=False),
                        sa.Column('github_access_token', sa.VARCHAR(length=255), nullable=True),
                        sa.Column('github_id', sa.INTEGER(), nullable=True),
                        sa.Column('github_login', sa.VARCHAR(length=255), nullable=True),
                        sa.PrimaryKeyConstraint('id'))

    if "release" not in tables:
        op.create_table('release',
                        sa.Column('id', sa.INTEGER(), nullable=False),
                        sa.Column('state', sa.VARCHAR(length=20), nullable=True),
                        sa.Column('running', sa.BOOLEAN(), nullable=True),
                        sa.Column('step', sa.INTEGER(), nullable=True),
                        sa.Column('details', sqlite.JSON(), nullable=True),
                        sa.Column('included_files', sqlite.JSON(), nullable=True),
                        sa.Column('start', sa.DATETIME(), nullable=True),
                        sa.Column('started_by', sa.VARCHAR(), nullable=True),
                        sa.Column('end', sa.DATETIME(), nullable=True),
                        sa.Column('release_script', sqlite.JSON(), nullable=True),
                        sa.Column('worker_id', sa.VARCHAR(length=20), nullable=True),
                        sa.Column('local_dir', sa.VARCHAR(), nullable=True),
                        sa.PrimaryKeyConstraint('id'))
