🔧 chore(api_key.py): improve error handling in delete_api_key_route function

🔧 chore(add_apikey_table.py): add hashed_api_key column to apikey table
🔧 chore(update_api_key.py): add migration script to update apikey table with hashed_api_key column
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-25 20:02:16 -03:00
commit dc20de76b5
3 changed files with 58 additions and 1 deletions

View file

@ -27,6 +27,9 @@ def upgrade() -> None:
op.create_table(
"apikey",
sa.Column("api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column(
"hashed_api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False
),
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("create_at", sa.DateTime(), nullable=False),
sa.Column("last_used_at", sa.DateTime(), nullable=True),

View file

@ -0,0 +1,54 @@
"""Update API key
Revision ID: 6384181fb7e8
Revises: d3749cf7ac7e
Create Date: 2023-08-25 20:00:22.889883
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import sqlmodel
import contextlib
# revision identifiers, used by Alembic.
revision: str = "6384181fb7e8"
down_revision: Union[str, None] = "d3749cf7ac7e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with contextlib.suppress(sa.exc.OperationalError):
op.add_column(
"apikey",
sa.Column(
"hashed_api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False
),
)
op.alter_column("apikey", "name", existing_type=sa.VARCHAR(), nullable=True)
op.create_index(
op.f("ix_apikey_hashed_api_key"), "apikey", ["hashed_api_key"], unique=False
)
op.create_index(op.f("ix_apikey_name"), "apikey", ["name"], unique=False)
op.create_foreign_key(None, "apikey", "user", ["user_id"], ["id"])
with contextlib.suppress(sa.exc.OperationalError):
op.alter_column(
"flow", "user_id", existing_type=sa.CHAR(length=32), nullable=False
)
op.create_foreign_key(None, "flow", "user", ["user_id"], ["id"])
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "flow", type_="foreignkey")
op.alter_column("flow", "user_id", existing_type=sa.CHAR(length=32), nullable=True)
op.drop_constraint(None, "apikey", type_="foreignkey")
op.drop_index(op.f("ix_apikey_name"), table_name="apikey")
op.drop_index(op.f("ix_apikey_hashed_api_key"), table_name="apikey")
op.alter_column("apikey", "name", existing_type=sa.VARCHAR(), nullable=False)
op.drop_column("apikey", "hashed_api_key")
# ### end Alembic commands ###

View file

@ -58,4 +58,4 @@ def delete_api_key_route(
delete_api_key(db, api_key_id)
return {"detail": "API Key deleted"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
raise HTTPException(status_code=400, detail=str(e)) from e