-
Notifications
You must be signed in to change notification settings - Fork 50
chore: include migrations in pyinstaller bundle #153
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
Open
TommrraraSnow
wants to merge
1
commit into
main
Choose a base branch
from
codex/automate-migrations-during-application-startup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
This file was deleted.
Oops, something went wrong.
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
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
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 @@ | ||
| """Utilities for programmatically running Alembic migrations.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contextlib import contextmanager | ||
|
|
||
| from alembic import command | ||
| from alembic.config import Config | ||
| from importlib.resources import as_file, files | ||
|
|
||
| from zsim.api_src.services.database.orm import get_sync_database_url, get_sync_engine | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _alembic_cfg() -> Config: | ||
| """构造指向包内迁移脚本的Alembic配置。""" | ||
|
|
||
| script_location = files("zsim.api_src.services.database.migrations") | ||
| with as_file(script_location) as script_dir: | ||
| cfg = Config() | ||
| cfg.set_main_option("script_location", str(script_dir)) | ||
| cfg.set_main_option("sqlalchemy.url", get_sync_database_url()) | ||
| yield cfg | ||
|
|
||
|
|
||
| def run_migrations_to_head() -> None: | ||
| """确保数据库结构升级到最新版本。""" | ||
|
|
||
| # 初始化同步引擎以确保数据库文件和目录已创建 | ||
| get_sync_engine() | ||
| with _alembic_cfg() as cfg: | ||
| command.upgrade(cfg, "head") | ||
|
|
||
|
|
||
| __all__ = ["run_migrations_to_head"] |
File renamed without changes.
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,70 @@ | ||
| """Alembic环境配置""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from logging.config import fileConfig | ||
| from pathlib import Path | ||
|
|
||
| from alembic import context | ||
|
|
||
| PROJECT_ROOT = Path(__file__).resolve().parents[5] | ||
| if str(PROJECT_ROOT) not in sys.path: | ||
| sys.path.insert(0, str(PROJECT_ROOT)) | ||
|
|
||
| from zsim.api_src.services.database import ( # noqa: E402 # isort:skip | ||
| apl_db, | ||
| character_db, | ||
| enemy_db, | ||
| session_db, | ||
| ) | ||
| from zsim.api_src.services.database.orm import ( # noqa: E402 # isort:skip | ||
| Base, | ||
| get_sync_database_url, | ||
| get_sync_engine, | ||
| ) | ||
|
|
||
| config = context.config | ||
| if config.config_file_name is not None: | ||
| fileConfig(config.config_file_name) | ||
|
|
||
| target_metadata = Base.metadata | ||
| _ = (apl_db, character_db, enemy_db, session_db) | ||
|
|
||
|
|
||
| def run_migrations_offline() -> None: | ||
| """Offline模式运行迁移""" | ||
|
|
||
| url = get_sync_database_url() | ||
| context.configure( | ||
| url=url, | ||
| target_metadata=target_metadata, | ||
| literal_binds=True, | ||
| render_as_batch=True, | ||
| dialect_opts={"paramstyle": "named"}, | ||
| ) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| def run_migrations_online() -> None: | ||
| """Online模式运行迁移""" | ||
|
|
||
| connectable = get_sync_engine() | ||
|
|
||
| with connectable.connect() as connection: | ||
| context.configure( | ||
| connection=connection, | ||
| target_metadata=target_metadata, | ||
| render_as_batch=True, | ||
| ) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| if context.is_offline_mode(): | ||
| run_migrations_offline() | ||
| else: | ||
| run_migrations_online() | ||
File renamed without changes.
Empty file.
File renamed without changes.
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new Alembic env now computes
PROJECT_ROOTwithPath(__file__).resolve().parents[5]. That works when the migrations live on disk at/…/zsim/api_src/services/database/migrations, but whenrun_migrations_to_head()loads the scripts viaimportlib.resources.as_filein a PyInstaller build the directory is extracted to a temporary folder such as/tmp/tmpabcd/migrations. In that context theparentssequence has fewer than six elements and this line raisesIndexErrorbefore migrations run, causing the packaged API to crash during startup. Consider deriving the project root more defensively (e.g. walk up until you find thezsimpackage or guard against short paths) so the code works both in-source and when migrations are extracted to a flat temporary directory.Useful? React with 👍 / 👎.