Skip to main content

Create and Run migration for fastapi project

Project description

FastAPI-GENE

A lightweight CLI tool to scaffold fastapi projects, run migrations and migrate migration files.

Installation

Create and activate a virtual environment and then install fastapi_gene:

$ pip install fastapi_gene

Usage

Here are some examples of what fastapi_gene can do:

fastapi_gene createproject main

It creates a fastapi project named main along with it's subfolder models,schema,api and file app.py inside it.

main/
├── app.py
└── main/
    ├── __init__.py
    ├── api/
    │   ├── __init__.py
    │   └── item.py
    ├── models/
    │   └── __init__.py
    └── schema/
        ├── __init__.py
        └── item.py

To run the project just change path to main and run fastapi dev.now, you can navigate to http://127.0.0.1:8000/docs/ to see autogenerated items endpoints but make sure to install fastapi on your enviroment.😊

fastapi_gene createuser fastapi-users[sqlalchemy]

It creates file and add user table on it as well as map endpoint into your project automatically from fastapi-users[sqlalchemy] module but project structure should be maintained to work as smoothly.

  • Supported modules are:
    • fastapi-users[sqlalchemy]
    • fastapi-users[sqlalchemy,oauth]
    • fastapi-users[beanie]
    • fastapi-users[beanie,oauth]

Now, File structure changed to

main/
├── app.py
└── main/
    ├── __init__.py
    ├── api/
    │   ├── __init__.py
    │   |── item.py
    |   └── users.py    #created
    |
    ├── models/
    │   |── __init__.py
    |   |── db.py       #created
    |   └── users.py    #created
    |
    └── schema/
        ├── __init__.py
        |── item.py
        └── users.py    #created

// To reflect users endpoint on project include `user_router` in `app.py` as
# app.py

from fastapi import FastAPI
from main.api import item_router,user_router

app = FastAPI()


app.include_router(item_router)
app.include_router(user_router)

fastapi_gene makemigrations

fastapi_gene makemigrations init

Migrations directory wasnot created to store migrations file. To create that, you should run above command in your terminal and it creates migrations directory named alembic. It works same as alembic init alembic but you can also provide custom name as fastapi_gene makemigrations init --name=migrations

main/
├── app.py
├── alembic/...  #created
├── alembic.ini  #created
└── main/...

fastapi_gene makemigrations -m "create user tables"

After importing Base from user model and sqlalchemy_url inside env.py of migrations directory, you can run this command which will create the migration file with named randomnumber_create_user_tables.py inside version directory of migrations. It works same as alembic revision --autogenerate -m "create user tables".

fastapi_gene migrate up

It applies all migrations upto latest version. It works same as alembic upgrade head. By default, it passes head revision but you can provide desire revision as fastapi_gene migrate up revision_id

fastapi_gene migrate down

It reverts all migrations, bringing the database back to its initial empty state. It works same as alembic downgrade base. By default, it passes base revision but you can provide desire revision as fastapi_gene migrate down revision_id

Notes

  • command: To know about all the commands fastapi_gene --help
  • specific-command: For the information of specific commands fastapi_gene "command_name" --help

Download files

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

Source Distribution

fastapi_gene-0.0.7.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_gene-0.0.7-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_gene-0.0.7.tar.gz.

File metadata

  • Download URL: fastapi_gene-0.0.7.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.1

File hashes

Hashes for fastapi_gene-0.0.7.tar.gz
Algorithm Hash digest
SHA256 12cf8e352d029f375de339131a9d8541bf2ec0df1fe2234bf923d386c16e3317
MD5 f4938b45457447c5fc78a4d53af1e025
BLAKE2b-256 a6bb30d23ad60c78ecf40bdc4861ec7f969155b238c6b2cb54fffc2938a411fd

See more details on using hashes here.

File details

Details for the file fastapi_gene-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: fastapi_gene-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.1

File hashes

Hashes for fastapi_gene-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 fcbd00bb0b0e05b3cd6e3ddcc21b0ed2377156ba4da800f38f66641a2bc3f969
MD5 b1c2d9f1da30317f3a3ab661e3d764f9
BLAKE2b-256 34009f54a6b292c81f1a89c32627e73a79c94e3bdd6874be6167406835ff9862

See more details on using hashes here.

Supported by

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