first init
This commit is contained in:
1
alembic/README
Normal file
1
alembic/README
Normal file
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
65
alembic/env.py
Normal file
65
alembic/env.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from alembic import context
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from logging.config import fileConfig
|
||||
|
||||
# 导入你的配置
|
||||
from config import settings
|
||||
|
||||
# 这是 Alembic 配置对象,提供对 .ini 文件中的值的访问。
|
||||
config = context.config
|
||||
|
||||
# 动态设置 sqlalchemy.url
|
||||
config.set_main_option("sqlalchemy.url", (
|
||||
settings.DB_URL or
|
||||
'{scheme}://{user}:{password}@{host}:{port}/{db}'.format(
|
||||
scheme=settings.DB_SCHEMA,
|
||||
user=settings.DB_USER,
|
||||
password=settings.DB_PASSWORD,
|
||||
host=settings.DB_HOST,
|
||||
port=settings.DB_PORT,
|
||||
db=settings.DB_NAME,
|
||||
)
|
||||
))
|
||||
|
||||
# 设置 target_metadata
|
||||
from core.db.models import BaseModel # 假设你的 SQLAlchemy 模型在 models.py 中
|
||||
|
||||
target_metadata = BaseModel.metadata
|
||||
|
||||
|
||||
def do_run_migrations(connection):
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
include_schemas=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_online():
|
||||
# 创建异步引擎
|
||||
connectable = create_async_engine(config.get_main_option("sqlalchemy.url"))
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
# 离线模式(同步)
|
||||
run_migrations_offline()
|
||||
else:
|
||||
import asyncio
|
||||
|
||||
# 在线模式(异步)
|
||||
# 检查是否已经有事件循环
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
except RuntimeError:
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
# 运行迁移
|
||||
loop.run_until_complete(run_migrations_online())
|
||||
26
alembic/script.py.mako
Normal file
26
alembic/script.py.mako
Normal file
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
102
alembic/versions/4c950125b0a6_init.py
Normal file
102
alembic/versions/4c950125b0a6_init.py
Normal file
@@ -0,0 +1,102 @@
|
||||
"""Init
|
||||
|
||||
Revision ID: 4c950125b0a6
|
||||
Revises:
|
||||
Create Date: 2025-02-17 03:17:02.044360
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '4c950125b0a6'
|
||||
down_revision: Union[str, None] = None
|
||||
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! ###
|
||||
op.create_table('system_cache_cards',
|
||||
sa.Column('cert_number', sa.String(length=50), nullable=False, comment='评级卡号'),
|
||||
sa.Column('card_number', sa.String(length=50), nullable=False, comment='卡片编号'),
|
||||
sa.Column('data', sa.JSON(), nullable=False, comment='标准数据'),
|
||||
sa.Column('raw_data', sa.JSON(), nullable=False, comment='原始数据'),
|
||||
sa.Column('source', sa.Enum('PSA', 'BGS', 'CGC', 'GBTC', 'CCG', 'BCTC', name='supportplatformtype', inherit_schema=True), nullable=False, comment='数据来源:PSA、BGS、CGC、GBTC、CCG、BCTC'),
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='自增id'),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='创建时间'),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='更新时间'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
comment='搜索卡片信息缓存表'
|
||||
)
|
||||
op.create_index('cert_and_card_number_source', 'system_cache_cards', ['cert_number', 'card_number', 'source'], unique=False)
|
||||
op.create_table('system_cards',
|
||||
sa.Column('cert_number', sa.String(length=50), nullable=False, comment='评级卡号'),
|
||||
sa.Column('card_year', sa.Integer(), nullable=False, comment='卡片年份'),
|
||||
sa.Column('card_brand', sa.Integer(), nullable=False, comment='卡片品牌'),
|
||||
sa.Column('card_score', sa.DECIMAL(precision=10, scale=1), nullable=False, comment='卡片评分'),
|
||||
sa.Column('auto_score', sa.DECIMAL(precision=10, scale=1), nullable=False, comment='签字评分'),
|
||||
sa.Column('card_name', sa.String(length=225), nullable=False, comment='卡片名称'),
|
||||
sa.Column('card_number', sa.String(length=225), nullable=False, comment='卡片编号'),
|
||||
sa.Column('card_type', sa.String(length=20), nullable=False, comment='卡片类型'),
|
||||
sa.Column('card_picture', sa.JSON(), nullable=False, comment='卡片正、反图片(本地)'),
|
||||
sa.Column('card_spare_picture', sa.JSON(), nullable=False, comment='卡片正、反图片(远程)'),
|
||||
sa.Column('create_user_id', sa.Integer(), nullable=False, comment='创建者'),
|
||||
sa.Column('category', sa.Enum('SPORTS', 'ANIME', 'OTHER', name='cardcategorytype', inherit_schema=True), nullable=False, comment='卡片分类'),
|
||||
sa.Column('source', sa.Enum('PSA', 'BGS', 'CGC', 'GBTC', 'CCG', 'BCTC', 'OTHER', name='platformsourcetype', inherit_schema=True), nullable=False, comment='来源,PSA、BGS、CGC、GBTC、CCG、BCTC :采集 、OTHER:自定义'),
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='自增id'),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='创建时间'),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='更新时间'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
comment='卡片表'
|
||||
)
|
||||
op.create_index('cert_number_category_source', 'system_cards', ['cert_number', 'category', 'source'], unique=False)
|
||||
op.create_table('system_users',
|
||||
sa.Column('nickname', sa.String(length=20), nullable=False, comment='昵称'),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False, comment='用户id'),
|
||||
sa.Column('head_avatar', sa.String(length=500), nullable=False, comment='头像链接'),
|
||||
sa.Column('wechat_openid', sa.String(length=100), nullable=True, comment='微信openid'),
|
||||
sa.Column('wechat_union_id', sa.String(length=100), nullable=True, comment='微信unionid'),
|
||||
sa.Column('phone', sa.Integer(), nullable=False, comment='手机号码'),
|
||||
sa.Column('vip_expire_date', sa.Date(), nullable=False, comment='会员到期时间'),
|
||||
sa.Column('integral', sa.Integer(), nullable=False, comment='会员积分'),
|
||||
sa.Column('card_total', sa.Integer(), nullable=True, comment='卡牌总数'),
|
||||
sa.Column('card_show_total', sa.Integer(), nullable=True, comment='卡片展示总数'),
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='自增id'),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='创建时间'),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='更新时间'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('user_id', name='uniq_user_id'),
|
||||
comment='用户表'
|
||||
)
|
||||
op.create_table('system_user_collections',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('card_id', sa.Integer(), nullable=False),
|
||||
sa.Column('purchase_price', sa.DECIMAL(precision=10, scale=2), nullable=False),
|
||||
sa.Column('display_price', sa.DECIMAL(precision=10, scale=2), nullable=False),
|
||||
sa.Column('displayed', sa.Boolean(), nullable=False),
|
||||
sa.Column('updated_at', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='自增id'),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='创建时间'),
|
||||
sa.CheckConstraint('display_value >= 0', name='check_display_price'),
|
||||
sa.CheckConstraint('purchase_price >= 0', name='check_purchase_price'),
|
||||
sa.ForeignKeyConstraint(['card_id'], ['system_cards.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['system_users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('user_id', 'card_id', name='uniq_user_card')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('system_user_collections')
|
||||
op.drop_table('system_users')
|
||||
op.drop_index('cert_number_category_source', table_name='system_cards')
|
||||
op.drop_table('system_cards')
|
||||
op.drop_index('cert_and_card_number_source', table_name='system_cache_cards')
|
||||
op.drop_table('system_cache_cards')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user