-
Notifications
You must be signed in to change notification settings - Fork 0
add alembic for migrations #11
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
swelborn
wants to merge
3
commits into
key-not-transfer-id
Choose a base branch
from
alembic
base: key-not-transfer-id
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,3 +1,17 @@ | ||
| # fastcache_api | ||
|
|
||
| API for fastcache | ||
|
|
||
| ## Database | ||
|
|
||
| Schema is managed by Alembic (`src/fastcache_api/alembic/`), not | ||
| create-on-boot. After changing `tables.py`, or on a fresh checkout: | ||
|
|
||
| ```sh | ||
| uv run alembic upgrade head # apply migrations | ||
| uv run python scripts/gen_migration.py "message" # autogenerate a new one | ||
| ``` | ||
|
|
||
| `alembic upgrade head` must be run once before the first start (and again | ||
| after pulling any change that adds a migration); the systemd unit does this | ||
| automatically via `ExecStartPre`. |
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,46 @@ | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| from alembic import command | ||
| from alembic.config import Config | ||
| from sqlalchemy import create_engine | ||
|
|
||
| import fastcache_api.tables as _tables # noqa: F401 - registers tables on Base.metadata | ||
|
|
||
| REPO_ROOT = Path(__file__).parent.parent | ||
| VERSIONS_DIR = REPO_ROOT / "src/fastcache_api/alembic/versions" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| message = ( | ||
| " ".join(sys.argv[1:]) | ||
| if len(sys.argv) > 1 | ||
| else input("Migration name: ").strip() | ||
| ) | ||
| if not message: | ||
| print("Aborted: migration name cannot be empty.") | ||
| sys.exit(1) | ||
|
|
||
| before = {f for f in VERSIONS_DIR.glob("*.py") if f.name != "__init__.py"} | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp: | ||
| db_path = Path(tmp) / "gen_migration.sqlite" | ||
| engine = create_engine(f"sqlite:///{db_path}") | ||
| cfg = Config(toml_file=str(REPO_ROOT / "pyproject.toml")) | ||
| cfg.set_main_option( | ||
| "sqlalchemy.url", engine.url.render_as_string(hide_password=False) | ||
| ) | ||
| command.upgrade(cfg, "head") | ||
| command.revision(cfg, autogenerate=True, message=message) | ||
| engine.dispose() | ||
|
|
||
| new_files = { | ||
| f for f in VERSIONS_DIR.glob("*.py") if f.name != "__init__.py" | ||
| } - before | ||
| for f in sorted(new_files): | ||
| print(f"Generated: {f.name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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,41 @@ | ||
| import logging | ||
|
|
||
| from alembic import context | ||
| from sqlalchemy import create_engine, pool | ||
|
|
||
| import fastcache_api.tables as _tables # noqa: F401 - registers tables | ||
| from fastcache_api.config import settings | ||
| from fastcache_api.tables import Base | ||
|
|
||
| config = context.config | ||
| logging.basicConfig(level=logging.INFO) | ||
| target_metadata = Base.metadata | ||
|
|
||
|
|
||
| def get_url() -> str: | ||
| # set programmatically by scripts/gen_migration.py for a throwaway db | ||
| url = config.get_alembic_option("sqlalchemy.url") | ||
| if url: | ||
| return url | ||
| # for running migrations against the real db: alembic needs a sync | ||
| # driver, unlike the app's own aiosqlite engine. | ||
| return f"sqlite:///{settings.SQLITE_PATH}" | ||
|
swelborn marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def run_migrations() -> None: | ||
| connectable = create_engine(get_url(), poolclass=pool.NullPool) | ||
|
|
||
| with connectable.connect() as connection: | ||
| # batch mode: sqlite can't ALTER most column properties in place, | ||
| # alembic works around it by recreating the table. | ||
| context.configure( | ||
| connection=connection, | ||
| target_metadata=target_metadata, | ||
| render_as_batch=True, | ||
| ) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| run_migrations() | ||
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,24 @@ | ||
| """${message} | ||
|
|
||
| Revision ID: ${up_revision} | ||
| Revises: ${down_revision | comma,n} | ||
| Create Date: ${create_date} | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| ${imports if imports else ""} | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = ${repr(up_revision)} | ||
| down_revision: str | list[str] | None = ${repr(down_revision)} | ||
| branch_labels: str | list[str] | None = ${repr(branch_labels)} | ||
| depends_on: str | list[str] | None = ${repr(depends_on)} | ||
|
|
||
|
|
||
| def upgrade(): | ||
| ${upgrades if upgrades else "pass"} | ||
|
|
||
|
|
||
| def downgrade(): | ||
| ${downgrades if downgrades else "pass"} |
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,53 @@ | ||
| """init | ||
|
|
||
| Revision ID: 1d91b0606408 | ||
| Revises: | ||
| Create Date: 2026-07-10 16:20:35.643455 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "1d91b0606408" | ||
| down_revision: str | list[str] | None = None | ||
| branch_labels: str | list[str] | None = None | ||
| depends_on: str | list[str] | None = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.create_table( | ||
| "caches", | ||
| sa.Column("id", sa.Uuid(), nullable=False), | ||
| sa.Column("key", sa.String(), nullable=True), | ||
| sa.Column("pid", sa.Integer(), nullable=False), | ||
| sa.Column("create_time", sa.Float(), nullable=True), | ||
| sa.Column("user", sa.String(), nullable=False), | ||
| sa.Column("state", sa.String(), nullable=False), | ||
| sa.Column("exit_code", sa.Integer(), nullable=True), | ||
| sa.Column("log_path", sa.String(), nullable=False), | ||
| sa.Column("config", sa.JSON(), nullable=False), | ||
| sa.Column( | ||
| "created_at", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.text("(CURRENT_TIMESTAMP)"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column( | ||
| "updated_at", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.text("(CURRENT_TIMESTAMP)"), | ||
| nullable=False, | ||
| ), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| sa.UniqueConstraint("key"), | ||
| ) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_table("caches") | ||
| # ### end Alembic commands ### |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.