51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
|
|
"""users:补齐 Google 登录 openid 索引
|
|||
|
|
|
|||
|
|
Revision ID: 0022_users_google_openid
|
|||
|
|
Revises: 0021_memory_source_segment_id
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from typing import Sequence, Union
|
|||
|
|
|
|||
|
|
import sqlalchemy as sa
|
|||
|
|
|
|||
|
|
from alembic import op
|
|||
|
|
|
|||
|
|
revision: str = "0022_users_google_openid"
|
|||
|
|
down_revision: Union[str, None] = "0021_memory_source_segment_id"
|
|||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _table_exists(table_name: str) -> bool:
|
|||
|
|
return sa.inspect(op.get_bind()).has_table(table_name)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _column_names(table_name: str) -> set[str]:
|
|||
|
|
inspector = sa.inspect(op.get_bind())
|
|||
|
|
return {column["name"] for column in inspector.get_columns(table_name)}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _index_names(table_name: str) -> set[str]:
|
|||
|
|
inspector = sa.inspect(op.get_bind())
|
|||
|
|
return {index["name"] for index in inspector.get_indexes(table_name)}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def upgrade() -> None:
|
|||
|
|
if not _table_exists("users"):
|
|||
|
|
return
|
|||
|
|
if "openid" not in _column_names("users"):
|
|||
|
|
op.add_column("users", sa.Column("openid", sa.String(), nullable=True))
|
|||
|
|
if "ix_users_openid" not in _index_names("users"):
|
|||
|
|
op.create_index("ix_users_openid", "users", ["openid"], unique=True)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def downgrade() -> None:
|
|||
|
|
if not _table_exists("users"):
|
|||
|
|
return
|
|||
|
|
if "ix_users_openid" in _index_names("users"):
|
|||
|
|
op.drop_index("ix_users_openid", table_name="users")
|
|||
|
|
if "openid" in _column_names("users"):
|
|||
|
|
op.drop_column("users", "openid")
|