Skip to content

docs: add complete alembic integration example - #1741

Open
pctablet505 wants to merge 5 commits into
ormar-orm:masterfrom
pctablet505:fix-1400-alembic-docs-example
Open

pctablet505 wants to merge 5 commits into
ormar-orm:masterfrom
pctablet505:fix-1400-alembic-docs-example

Conversation

@pctablet505

@pctablet505 pctablet505 commented Jul 15, 2026

Copy link
Copy Markdown

Fixes #1400.

The current Alembic docs skip too many steps for anyone new to ormar — imports, where metadata comes from, and how to handle models split across apps. I added a complete, working example based on the FastAPI-template reference linked in the issue.

It covers a single-file layout, a multi-app layout, a minimal alembic.ini, and wiring the shared metadata into alembic/env.py. I ran alembic revision --autogenerate and alembic upgrade head against both layouts and built the docs with mkdocs build to make sure everything works.

@codspeed

codspeed Bot commented Jul 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 98 untouched benchmarks


Comparing pctablet505:fix-1400-alembic-docs-example (3da069a) with master (79f8153)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (867109e) during the generation of this report, so 79f8153 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:47
Expand the migrations/Alembic docs with a working, end-to-end example:

- Explain where the SQLAlchemy MetaData comes from in ormar and how to
  expose it for Alembic.
- Add a sample project layout with my_project/models.py, alembic.ini,
  and alembic/env.py.
- Show how to handle models split across multiple apps/packages by
  importing all models before Alembic reads the metadata.
- Keep the existing compare_type and include_object tips intact.

Fixes ormar-orm#1400
@pctablet505
pctablet505 force-pushed the fix-1400-alembic-docs-example branch from eecaa52 to 393859e Compare August 21, 2026 15:19
Comment thread docs/models/migrations.md Outdated
Comment thread docs/models/migrations.md Outdated
Comment thread docs/models/migrations.md
Comment thread docs/models/migrations.md
Comment thread docs/models/migrations.md Outdated
Comment thread docs/models/migrations.md Outdated
Comment thread docs/models/migrations.md
@pctablet505
pctablet505 force-pushed the fix-1400-alembic-docs-example branch from 4d4ca5b to b495465 Compare September 3, 2026 06:07
…ing, and package layouts

- Trim duplicate model definitions in 'Where does metadata come from?'
- Extract runnable models to docs_src/models/docs019.py via snippet syntax
- Include standard logging sections in alembic.ini to prevent KeyError: 'formatters'
- Add callout and driver comparison table explaining async vs sync URLs and alembic init -t async
- Drop redundant prepend_sys_path from alembic.ini (env.py handles sys.path)
- Pin db.sqlite path in alembic.ini using %(here)s to avoid cwd dependency
- Add missing __init__.py files in multi-app package layout
- Add noqa: E402 annotations to env.py snippets after sys.path manipulation
- Retain explanatory comment on user_module_prefix matching sqlalchemy_module_prefix and normalize quotes
@pctablet505
pctablet505 force-pushed the fix-1400-alembic-docs-example branch from b495465 to 3da069a Compare September 11, 2026 06:43
@pctablet505

Copy link
Copy Markdown
Author

Hi @collerek, thanks for the review! All 7 points have been addressed in the branch:

  1. Snippet inclusion: Extracted models to docs_src/models/docs019.py and included them via --8<-- "../docs_src/models/docs019.py".
  2. Trimmed duplicates: Simplified "Where does metadata come from?" to the single-line extraction.
  3. Logging sections: Added standard [loggers], [handlers], and [formatters] to alembic.ini so fileConfig works out of the box.
  4. Driver comparison: Added a callout + driver comparison table explaining async vs. sync URLs (aiosqlite/asyncpg vs. sqlite/psycopg2) and pointed to alembic init -t async.
  5. Path handling: Dropped prepend_sys_path from alembic.ini (handled by env.py) and pinned sqlite:///%(here)s/db.sqlite.
  6. Package layout & lint: Added missing __init__.py files to the multi-app layout and # noqa: E402 to env.py.
  7. Module prefix: Retained context explaining user_module_prefix="sa." must match sqlalchemy_module_prefix, and normalized quote formatting.

Branch is rebased onto the latest master and mergeable. Ready for your re-review whenever you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A *working* alembic example in the docs

2 participants