-
Notifications
You must be signed in to change notification settings - Fork 51
feat: 数据库ORM重构和Alembic迁移支持 #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
413b068
feat: 添加数据库ORM重构和Alembic迁移支持
TommrraraSnow e806418
fix: 移除异常处理中的noqa注释以提高代码清晰度
TommrraraSnow 05938d6
feat: 添加greenlet依赖到项目配置中
TommrraraSnow 7be6e34
fix: 修复export_apl_config函数的runtime error并改进文件异常处理
TommrraraSnow 1b0cf1d
refactor: 重构APL数据库异常处理 - 使用卫语句和错误自然传播
TommrraraSnow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| [alembic] | ||
| script_location = alembic | ||
| sqlalchemy.url = sqlite:///zsim/data/zsim.db | ||
|
|
||
| [loggers] | ||
| keys = root,sqlalchemy,alembic | ||
|
|
||
| [handlers] | ||
| keys = console | ||
|
|
||
| [formatters] | ||
| keys = generic | ||
|
|
||
| [logger_root] | ||
| level = WARN | ||
| handlers = console | ||
|
|
||
| [logger_sqlalchemy] | ||
| level = WARN | ||
| handlers = | ||
| qualname = sqlalchemy.engine | ||
|
|
||
| [logger_alembic] | ||
| level = INFO | ||
| handlers = | ||
| qualname = alembic | ||
|
|
||
| [handler_console] | ||
| class = StreamHandler | ||
| args = (sys.stdout,) | ||
| level = NOTSET | ||
| formatter = generic | ||
|
|
||
| [formatter_generic] | ||
| format = %(levelname)-5.5s [%(name)s] %(message)s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Alembic环境配置""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from logging.config import fileConfig | ||
| from pathlib import Path | ||
|
|
||
| from sqlalchemy import engine_from_config, pool | ||
|
|
||
| from alembic import context | ||
|
|
||
| PROJECT_ROOT = Path(__file__).resolve().parents[1] | ||
| if str(PROJECT_ROOT) not in sys.path: | ||
| sys.path.insert(0, str(PROJECT_ROOT)) | ||
|
|
||
| config = context.config | ||
| if config.config_file_name is not None: | ||
| fileConfig(config.config_file_name) | ||
|
|
||
|
|
||
| def _load_metadata(): | ||
| """加载SQLAlchemy元数据""" | ||
|
|
||
| import zsim.api_src.services.database.apl_db # noqa: F401 | ||
| import zsim.api_src.services.database.character_db # noqa: F401 | ||
| import zsim.api_src.services.database.enemy_db # noqa: F401 | ||
| import zsim.api_src.services.database.session_db # noqa: F401 | ||
| from zsim.api_src.services.database.orm import Base | ||
|
|
||
| return Base.metadata | ||
|
|
||
|
|
||
| def _get_database_url() -> str: | ||
| """获取同步数据库URL""" | ||
|
|
||
| from zsim.api_src.services.database.orm import get_sync_database_url | ||
|
|
||
| return get_sync_database_url() | ||
|
|
||
|
|
||
| target_metadata = _load_metadata() | ||
| config.set_main_option("sqlalchemy.url", _get_database_url()) | ||
|
|
||
|
|
||
| def run_migrations_offline() -> None: | ||
| """Offline模式运行迁移""" | ||
|
|
||
| url = config.get_main_option("sqlalchemy.url") | ||
| context.configure(url=url, target_metadata=target_metadata, literal_binds=True) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| def run_migrations_online() -> None: | ||
| """Online模式运行迁移""" | ||
|
|
||
| connectable = engine_from_config( | ||
| config.get_section(config.config_ini_section) or {}, | ||
| prefix="sqlalchemy.", | ||
| poolclass=pool.NullPool, | ||
| ) | ||
|
|
||
| with connectable.connect() as connection: | ||
| context.configure(connection=connection, target_metadata=target_metadata) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| if context.is_offline_mode(): | ||
| run_migrations_offline() | ||
| else: | ||
| run_migrations_online() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """${message} | ||
|
|
||
| Revision ID: ${up_revision} | ||
| Revises:${" " + (down_revision | comma,n) if down_revision else ""} | ||
| Create Date: ${create_date} | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Sequence | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| ${imports if imports else ""} | ||
|
|
||
| revision: str = ${repr(up_revision)} | ||
| down_revision: str | Sequence[str] | None = ${repr(down_revision)} | ||
| branch_labels: str | Sequence[str] | None = ${repr(branch_labels)} | ||
| depends_on: str | Sequence[str] | None = ${repr(depends_on)} | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """执行升级操作""" | ||
| ${upgrades if upgrades else "pass"} | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """执行回滚操作""" | ||
| ${downgrades if downgrades else "pass"} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """init schema | ||
|
|
||
| Revision ID: 74ee1818bd42 | ||
| Revises: | ||
| Create Date: 2025-10-07 12:40:12.492096 | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Sequence | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from alembic import op | ||
|
|
||
| revision: str = "74ee1818bd42" | ||
| down_revision: str | Sequence[str] | None = None | ||
| branch_labels: str | Sequence[str] | None = None | ||
| depends_on: str | Sequence[str] | None = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """执行升级操作""" | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.create_table("apl_configs", | ||
| sa.Column("id", sa.String(length=64), nullable=False), | ||
| sa.Column("title", sa.String(length=255), nullable=False), | ||
| sa.Column("author", sa.String(length=255), nullable=True), | ||
| sa.Column("comment", sa.Text(), nullable=True), | ||
| sa.Column("create_time", sa.String(length=32), nullable=False), | ||
| sa.Column("latest_change_time", sa.String(length=32), nullable=False), | ||
| sa.Column("content", sa.Text(), nullable=False), | ||
| sa.PrimaryKeyConstraint("id") | ||
| ) | ||
| op.create_table("character_configs", | ||
| sa.Column("config_id", sa.String(length=128), nullable=False), | ||
| sa.Column("name", sa.String(length=255), nullable=False), | ||
| sa.Column("config_name", sa.String(length=255), nullable=False), | ||
| sa.Column("weapon", sa.String(length=255), nullable=False), | ||
| sa.Column("weapon_level", sa.Integer(), nullable=False), | ||
| sa.Column("cinema", sa.Integer(), nullable=False), | ||
| sa.Column("crit_balancing", sa.Boolean(), nullable=False), | ||
| sa.Column("crit_rate_limit", sa.Float(), nullable=False), | ||
| sa.Column("scATK_percent", sa.Integer(), nullable=False), | ||
| sa.Column("scATK", sa.Integer(), nullable=False), | ||
| sa.Column("scHP_percent", sa.Integer(), nullable=False), | ||
| sa.Column("scHP", sa.Integer(), nullable=False), | ||
| sa.Column("scDEF_percent", sa.Integer(), nullable=False), | ||
| sa.Column("scDEF", sa.Integer(), nullable=False), | ||
| sa.Column("scAnomalyProficiency", sa.Integer(), nullable=False), | ||
| sa.Column("scPEN", sa.Integer(), nullable=False), | ||
| sa.Column("scCRIT", sa.Integer(), nullable=False), | ||
| sa.Column("scCRIT_DMG", sa.Integer(), nullable=False), | ||
| sa.Column("drive4", sa.Text(), nullable=False), | ||
| sa.Column("drive5", sa.Text(), nullable=False), | ||
| sa.Column("drive6", sa.Text(), nullable=False), | ||
| sa.Column("equip_style", sa.String(length=255), nullable=False), | ||
| sa.Column("equip_set4", sa.String(length=255), nullable=True), | ||
| sa.Column("equip_set2_a", sa.String(length=255), nullable=True), | ||
| sa.Column("equip_set2_b", sa.String(length=255), nullable=True), | ||
| sa.Column("equip_set2_c", sa.String(length=255), nullable=True), | ||
| sa.Column("create_time", sa.String(length=32), nullable=False), | ||
| sa.Column("update_time", sa.String(length=32), nullable=False), | ||
| sa.PrimaryKeyConstraint("config_id") | ||
| ) | ||
| op.create_table("enemy_configs", | ||
| sa.Column("config_id", sa.String(length=128), nullable=False), | ||
| sa.Column("enemy_index", sa.Integer(), nullable=False), | ||
| sa.Column("enemy_adjust", sa.Text(), nullable=False), | ||
| sa.Column("create_time", sa.String(length=32), nullable=False), | ||
| sa.Column("update_time", sa.String(length=32), nullable=False), | ||
| sa.PrimaryKeyConstraint("config_id") | ||
| ) | ||
| op.create_table("sessions", | ||
| sa.Column("session_id", sa.String(length=128), nullable=False), | ||
| sa.Column("session_name", sa.String(length=255), nullable=False), | ||
| sa.Column("create_time", sa.String(length=32), nullable=False), | ||
| sa.Column("status", sa.String(length=32), nullable=False), | ||
| sa.Column("session_run", sa.Text(), nullable=True), | ||
| sa.Column("session_result", sa.Text(), nullable=True), | ||
| sa.PrimaryKeyConstraint("session_id") | ||
| ) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """执行回滚操作""" | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_table("sessions") | ||
| op.drop_table("enemy_configs") | ||
| op.drop_table("character_configs") | ||
| op.drop_table("apl_configs") | ||
| # ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.