Skip to main content

A small integration between Fastapi and Alembic.

Project description

Fastapi Migrations

PyPI PyPI - Python Version PyPI - License

GitHub last commit GitHub commit activity GitHub issues GitHub pull requests

This library provides a small wrapper for alembic.

Notice

Under inital development. This can not be ready-for-production library.

This can means:

  • Breaking changes may be introduced
  • Poor documentation and changeslogs
  • Not totally tested
  • Be forced to navigate through the source code to find out how it works

Wait to a version > 0.1.0 for usage in production environments.

Installation

You can install this library with:

pip3 install fastapi-migrations

Usage

You can use both programatically and by CLI (command line interface).

Imagine your project folders

app/
    cli/
        __init__.py
        action.py
    db/
        __init__.py
        base.py
    models/
        __init__.py
        my_model.py
    endpoints/
        __init__.py
        my_endpoint.py
    __init__.py
    config.py
    main.py

This is an example of main.py:

from fastapi import FastAPI
from fastapi_sqlalchemy import DBSessionMiddleware

# Load configs and endpoints
from app.config import settings
from app.endpoints import router

app: FastAPI = FastAPI(title=settings.project_name)

# register routes
app.include_router(router)

# add middlewares
app.add_middleware(DBSessionMiddleware, db_url=settings.database_uri)

if __name__ == '__main__':
    # Load cli commands
    from app.cli import app as cli

    cli()

Then your app/cli/__init__.py can be like:

import typer

from fastapi_migrations.cli import MigrationsCli
import app.cli.action as action

# main cli app
app: typer.Typer = typer.Typer()

# these are our cli actions
app.add_typer(action.app, name='action', help='Common actions the app do')

# this line adds the fastapi-migrations cli commands to our app
app.add_typer(MigrationsCli())

Now you can call your app from the command line and use fastapi-migrations like:

py app/main.py db show

If you want to use this library programatically, this is an example:

The file app/cli/action.py can be like:

import typer
from fastapi_migrations import MigrationsConfig, Migrations

app: typer.Typer = typer.Typer()

@app.command()
def show() -> None:
    config = MigrationsConfig()

    migrations = Migrations(config)

    migrations.show()

You can add this lines where you wish in your proyect. Here we ar adding it to a command line so we can call our app like:

py app/main.py action show

License

This software is distributed under MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-migrations-0.0.5.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

fastapi_migrations-0.0.5-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-migrations-0.0.5.tar.gz.

File metadata

  • Download URL: fastapi-migrations-0.0.5.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for fastapi-migrations-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f62b35fb7e12fcb60a50b6826330f327a7ccad3b6cadbb2de6278312071470aa
MD5 113cb1ee6741e3f9729d0754cc7722ef
BLAKE2b-256 5e997109e1cff35b2dde426fe4857e3f7ce90b4da30640bb6323d4e42379e796

See more details on using hashes here.

File details

Details for the file fastapi_migrations-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: fastapi_migrations-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for fastapi_migrations-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a3c35cf43b5d827d1edda8c261b105764114e66eab533390a1d4e6ca08cb85f7
MD5 5bcfe85f5a8c17f0766ab1b3e2667740
BLAKE2b-256 e6c02cc5a485910eb982589fad7b59122008dbef4ce319afd01780fd0842c7ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page