Updating to support categories

This commit is contained in:
2023-03-11 12:32:41 +00:00
parent bdab151dae
commit ec7bd6dde5
27 changed files with 807 additions and 42 deletions

1
backend/alembic/README Normal file
View File

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

81
backend/alembic/env.py Normal file
View File

@@ -0,0 +1,81 @@
import asyncio
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from sqlmodel import Session, SQLModel, create_engine
from sqlmodel.ext.asyncio.session import AsyncEngine
from alembic import context
from src.database.engine import engine
from src.models import * # necessarily to import something from file where your models are stored
# 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
target_metadata = None
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None
# comment line above and instead of that write
target_metadata = SQLModel.metadata
async 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.
"""
connectable = engine
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
def do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True)
with context.begin_transaction():
context.run_migrations()
async 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
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
if context.is_offline_mode():
asyncio.run(run_migrations_offline())
else:
asyncio.run(run_migrations_online())

View File

@@ -0,0 +1,25 @@
"""${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 ""}
import sqlmodel
# 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"}

View File

@@ -0,0 +1,46 @@
"""add category table
Revision ID: 2d2d4523082b
Revises:
Create Date: 2023-03-09 20:29:44.081609
"""
import sqlalchemy as sa
import sqlmodel
from alembic import op
from src.models.category import Category
# revision identifiers, used by Alembic.
revision = '2d2d4523082b'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# create category table
Category.__table__.create(bind=op.get_bind())
op.bulk_insert(
Category.__table__,
[
{"id": 1, "name": "Alfabet"},
]
)
with op.batch_alter_table('sign', schema=None) as batch_op:
# create column category_id
batch_op.add_column(sa.Column('category_id', sa.Integer(), nullable=True, default=1))
batch_op.create_foreign_key("sign_category", 'category', ['category_id'], ['id'])
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('sign', schema=None) as batch_op:
batch_op.drop_constraint("sign_category", type_='foreignkey')
batch_op.drop_column('category_id')
# drop category table
Category.__table__.drop(bind=op.get_bind())
# ### end Alembic commands ###