Adding alembic and sql support

drew/tilt-local-dev
Drew Bednar 2 years ago
parent 5f14b61099
commit 1442af6828

3
.gitignore vendored

@ -137,3 +137,6 @@ dmypy.json
# Cython debug symbols
cython_debug/
# sqlite
*.sqlite3

@ -0,0 +1,2 @@
[MASTER]
ignore-paths=^alembic\\.*$|^alembic/.*$

@ -0,0 +1,105 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python-dateutil library that can be
# installed by adding `alembic[tz]` to the pip requirements
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "version_path_separator" below.
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
# version path separator; As mentioned above, this is the character used to split
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
# Valid values for version_path_separator are:
#
# version_path_separator = :
# version_path_separator = ;
# version_path_separator = space
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = driver://user:pass@localhost/dbname
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

@ -0,0 +1 @@
Generic single-database configuration.

@ -0,0 +1,85 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from project import create_app
from project.config import settings
from project.database import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option("sqlalchemy.url", str(settings.DATABASE_URL))
# Used create_app to create a new fastapi_app instance to ensure the relevant models are loaded.
fastapi_app = create_app()
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

@ -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 = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

@ -0,0 +1,28 @@
"""empty message
Revision ID: 4e1bcbead27f
Revises:
Create Date: 2022-09-21 20:11:34.057323
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "4e1bcbead27f"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###

@ -0,0 +1,36 @@
"""empty message
Revision ID: b03fcdc9b301
Revises: 4e1bcbead27f
Create Date: 2022-09-23 15:50:30.792964
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "b03fcdc9b301"
down_revision = "4e1bcbead27f"
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"users",
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column("username", sa.String(length=128), nullable=False),
sa.Column("email", sa.String(length=128), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("email"),
sa.UniqueConstraint("username"),
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("users")
# ### end Alembic commands ###

@ -1,22 +1,4 @@
from celery import Celery
from fastapi import FastAPI
from project import create_app
app = FastAPI()
celery = Celery(
__name__, broker="redis://127.0.0.1:6379/0", backend="redis://127.0.0.1:6379/0"
)
@app.get("/")
async def root():
return {"message": "Hello World"}
@celery.task
def divide(x, y):
import time
time.sleep(5)
return x / y
app = create_app()
celery = app.celery_app

@ -0,0 +1,18 @@
from fastapi import FastAPI
from project.celery_utils import create_celery
def create_app() -> FastAPI:
app = FastAPI()
app.celery_app = create_celery()
from .users import users_router
app.include_router(users_router)
@app.get("/")
async def root():
return {"message": "Hello World"}
return app

@ -0,0 +1,11 @@
from celery import current_app as celery_current_app
from project.config import settings
def create_celery():
"""Clerey factory function."""
celery_app = celery_current_app
celery_app.config_from_object(settings, namespace="CELERY")
return celery_app

@ -0,0 +1,47 @@
import os
import pathlib
from functools import lru_cache
class BaseConfig:
BASE_DIR: pathlib.Path = pathlib.Path(__file__).parent.parent
DATABASE_URL: str = os.environ.get(
"DATABASE_URL", f"sqlite:///{BASE_DIR}/db.sqlite3"
)
DATABASE_CONNECT_DICT: dict = {}
CELERY_BROKER_URL: str = os.environ.get(
"CELERY_BROKER_URL", "redis://127.0.0.1:6379/0"
)
CELERY_RESULT_BACKEND: str = os.environ.get(
"CELERY_RESULT_BACKEND", "redis://127.0.0.1:6379/0"
)
class DevelopmentConfig(BaseConfig):
pass
class ProductionConfig(BaseConfig):
pass
class TestingConfig(BaseConfig):
pass
@lru_cache()
def get_settings():
config_cls_dict = {
"development": DevelopmentConfig,
"production": ProductionConfig,
"testing": TestingConfig,
}
config_name = os.environ.get("FASTAPI_CONFIG", "development")
config_cls = config_cls_dict[config_name]
return config_cls()
settings = get_settings()

@ -0,0 +1,13 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from project.config import settings
# https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-sqlalchemy-engine
engine = create_engine(
settings.DATABASE_URL, connect_args=settings.DATABASE_CONNECT_DICT
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

@ -0,0 +1,9 @@
from fastapi import APIRouter
from project.celery_utils import create_celery
users_router = APIRouter(
prefix="/users",
)
from . import models, tasks # noqa

@ -0,0 +1,18 @@
from sqlalchemy import BigInteger, Column, String
from project.database import Base
class User(Base):
__tablename__ = "users"
id = Column(BigInteger, primary_key=True, autoincrement=True)
username = Column(String(128), unique=True, nullable=False)
email = Column(String(128), unique=True, nullable=False)
def __init__(
self, username, email, *args, **kwargs # pylint: disable=unused-argument
) -> None:
self.username = username
self.email = email

@ -0,0 +1,15 @@
"""
Many resources on the web recommend using celery.task. This might cause circular imports since you'll have to import the Celery instance.
We used shared_task to make our code reusable, which, again, requires current_app in create_celery instead of creating a new Celery instance.
Now, we can copy this file anywhere in the app and it will work as expected.
"""
from celery import shared_task
@shared_task
def divide(x, y):
import time
time.sleep(5)
return x / y

@ -1,5 +1,7 @@
alembic==1.8.1
celery==5.2.7
fastapi==0.79.0
flower==1.2.0
redis==4.3.4
SQLAlchemy==1.4.40
uvicorn[standard]==0.18.2

@ -4,6 +4,8 @@
#
# pip-compile requirements.in
#
alembic==1.8.1
# via -r requirements.in
amqp==5.1.1
# via kombu
anyio==3.6.1
@ -37,6 +39,8 @@ fastapi==0.79.0
# via -r requirements.in
flower==1.2.0
# via -r requirements.in
greenlet==1.1.3
# via sqlalchemy
h11==0.13.0
# via uvicorn
httptools==0.5.0
@ -45,8 +49,16 @@ humanize==4.4.0
# via flower
idna==3.4
# via anyio
importlib-metadata==4.12.0
# via alembic
importlib-resources==5.9.0
# via alembic
kombu==5.2.4
# via celery
mako==1.2.2
# via alembic
markupsafe==2.1.1
# via mako
packaging==21.3
# via redis
prometheus-client==0.14.1
@ -71,6 +83,10 @@ six==1.16.0
# via click-repl
sniffio==1.3.0
# via anyio
sqlalchemy==1.4.40
# via
# -r requirements.in
# alembic
starlette==0.19.1
# via fastapi
tornado==6.2
@ -96,3 +112,7 @@ websockets==10.3
# via uvicorn
wrapt==1.14.1
# via deprecated
zipp==3.8.1
# via
# importlib-metadata
# importlib-resources

@ -38,3 +38,30 @@ def stop_redis(c):
"""Stops the Redis integration environent."""
print("Stopping Redis")
c.run("docker compose -f docker-compose-redis.yml down")
@task
def create_migration(c):
"""Creates a sqlalchemy migration."""
c.run("alembic revision --autogenerate")
@task
def apply_migration(c, revision="head"):
"""Applies an Alembic migration."""
c.run(f"alembic upgrade {revision}")
@task(help={"username": "Username of the application", "email": "User's email"})
def add_user(username="", email=""):
"Adds a dummy user to the application Database"
if not username or email:
raise ValueError("You must provide a username and email")
from project.database import SessionLocal
from project.users.models import User
session = SessionLocal()
user = User(username=username, email=email)
session.add(user)
session.commit()
session.close()

Loading…
Cancel
Save