upload project source code

This commit is contained in:
2026-04-30 18:49:43 +08:00
commit 9b394ba682
2277 changed files with 660945 additions and 0 deletions

View File

@@ -0,0 +1 @@
Generic single-database configuration.

View File

@@ -0,0 +1,131 @@
import asyncio
from logging.config import fileConfig
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import pool
from alembic import context
from app.config.path_conf import ALEMBIC_VERSION_DIR
from app.utils.import_util import ImportUtil
from app.core.base_model import MappedBase
from app.config.setting import settings
# 确保 alembic 版本目录存在
ALEMBIC_VERSION_DIR.mkdir(parents=True, exist_ok=True)
# 清除MappedBase.metadata中的表定义避免重复注册
if hasattr(MappedBase, 'metadata') and MappedBase.metadata.tables:
print(f"🧹 清除已存在的表定义,当前有 {len(MappedBase.metadata.tables)} 个表")
# 创建一个新的空metadata对象
from sqlalchemy import MetaData
MappedBase.metadata = MetaData()
print("✅️ 已重置metadata")
# 自动查找所有模型
print("🔍 开始查找模型...")
found_models = ImportUtil.find_models(MappedBase)
print(f"📊 找到 {len(found_models)} 个有效模型")
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
alembic_config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if alembic_config.config_file_name is not None:
fileConfig(alembic_config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = MappedBase.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
alembic_config.set_main_option("sqlalchemy.url", settings.ASYNC_DB_URI)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = alembic_config.get_main_option("sqlalchemy.url")
# 确保URL不为None
if url is None:
raise ValueError("数据库URL未正确配置请检查环境配置文件")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
url = alembic_config.get_main_option("sqlalchemy.url")
# 确保URL不为None
if url is None:
raise ValueError("数据库URL未正确配置请检查环境配置文件")
connectable = create_async_engine(url, poolclass=pool.NullPool)
async def run_async_migrations():
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def do_run_migrations(connection: Connection) -> None:
def process_revision_directives(context, revision, directives):
script = directives[0]
# 检查所有操作集是否为空
all_empty = all(ops.is_empty() for ops in script.upgrade_ops_list)
if all_empty:
# 如果没有实际变更,不生成迁移文件
directives[:] = []
print('❎️ 未检测到模型变更,不生成迁移文件')
else:
print('✅️ 检测到模型变更,生成迁移文件')
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
compare_server_default=True,
transaction_per_migration=True,
process_revision_directives=process_revision_directives,
)
with context.begin_transaction():
context.run_migrations()
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View 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"}

View File

@@ -0,0 +1,30 @@
"""add success_time to yifan_wx_pay_order
Revision ID: 7b2c6f1a9d3e
Revises: e1db2024739c
Create Date: 2026-01-15 10:50:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "7b2c6f1a9d3e"
down_revision: Union[str, None] = "e1db2024739c"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.add_column(
"yifan_wx_pay_order",
sa.Column("success_time", sa.DateTime(), nullable=True, comment="支付成功时间"),
)
def downgrade() -> None:
op.drop_column("yifan_wx_pay_order", "success_time")

View File

@@ -0,0 +1,32 @@
"""add inviter_id to sys_user
Revision ID: 9c4f1d2a6b7e
Revises: 7b2c6f1a9d3e
Create Date: 2026-01-15 14:15:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "9c4f1d2a6b7e"
down_revision: Union[str, None] = "7b2c6f1a9d3e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.add_column(
"sys_user",
sa.Column("inviter_id", sa.Integer(), nullable=True, comment="邀请人ID"),
)
op.create_index(op.f("ix_sys_user_inviter_id"), "sys_user", ["inviter_id"], unique=False)
def downgrade() -> None:
op.drop_index(op.f("ix_sys_user_inviter_id"), table_name="sys_user")
op.drop_column("sys_user", "inviter_id")

View File

@@ -0,0 +1,142 @@
"""迁移脚本
Revision ID: e1db2024739c
Revises:
Create Date: 2025-12-22 17:27:01.532822
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic.
revision: str = 'e1db2024739c'
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('miniapp_user',
sa.Column('openid', sa.String(length=64), nullable=False, comment='微信openid'),
sa.Column('unionid', sa.String(length=64), nullable=True, comment='微信unionid'),
sa.Column('session_key', sa.String(length=64), nullable=True, comment='会话密钥'),
sa.Column('nickname', sa.String(length=64), nullable=True, comment='昵称'),
sa.Column('avatar', sa.String(length=512), nullable=True, comment='头像URL'),
sa.Column('phone', sa.String(length=20), nullable=True, comment='手机号'),
sa.Column('last_login', sa.DateTime(timezone=True), nullable=True, comment='最后登录时间'),
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='主键ID'),
sa.Column('uuid', sa.String(length=64), nullable=False, comment='UUID全局唯一标识'),
sa.Column('status', sa.String(length=10), nullable=False, comment='是否启用(0:启用 1:禁用)'),
sa.Column('description', sa.Text(), nullable=True, comment='备注/描述'),
sa.Column('created_time', sa.DateTime(), nullable=False, comment='创建时间'),
sa.Column('updated_time', sa.DateTime(), nullable=False, comment='更新时间'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('unionid'),
sa.UniqueConstraint('uuid'),
comment='小程序用户表'
)
op.create_index(op.f('ix_miniapp_user_openid'), 'miniapp_user', ['openid'], unique=True)
op.drop_index('ix_apscheduler_jobs_next_run_time', table_name='apscheduler_jobs')
op.drop_table('apscheduler_jobs')
op.alter_column('gen_table_column', 'is_pk',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('false'),
existing_comment='是否主键',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_increment',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('false'),
existing_comment='是否自增',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_nullable',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('true'),
existing_comment='是否允许为空',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_unique',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('false'),
existing_comment='是否唯一',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_insert',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('true'),
existing_comment='是否为新增字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_edit',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('true'),
existing_comment='是否编辑字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_list',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('true'),
existing_comment='是否列表字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_query',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text('false'),
existing_comment='是否查询字段',
existing_nullable=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('gen_table_column', 'is_query',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'0'"),
existing_comment='是否查询字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_list',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'1'"),
existing_comment='是否列表字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_edit',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'1'"),
existing_comment='是否编辑字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_insert',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'1'"),
existing_comment='是否为新增字段',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_unique',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'0'"),
existing_comment='是否唯一',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_nullable',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'1'"),
existing_comment='是否允许为空',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_increment',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'0'"),
existing_comment='是否自增',
existing_nullable=False)
op.alter_column('gen_table_column', 'is_pk',
existing_type=mysql.TINYINT(display_width=1),
server_default=sa.text("'0'"),
existing_comment='是否主键',
existing_nullable=False)
op.create_table('apscheduler_jobs',
sa.Column('id', mysql.VARCHAR(length=191), nullable=False),
sa.Column('next_run_time', mysql.DOUBLE(asdecimal=True), nullable=True),
sa.Column('job_state', sa.BLOB(), nullable=False),
sa.PrimaryKeyConstraint('id'),
mysql_collate='utf8mb4_0900_ai_ci',
mysql_default_charset='utf8mb4',
mysql_engine='InnoDB'
)
op.create_index('ix_apscheduler_jobs_next_run_time', 'apscheduler_jobs', ['next_run_time'], unique=False)
op.drop_index(op.f('ix_miniapp_user_openid'), table_name='miniapp_user')
op.drop_table('miniapp_user')
# ### end Alembic commands ###