From 51aa79788b3ddb1f9b4c9eae8b147f70e3e0e020 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Tue, 2 Jan 2024 21:14:38 +0100 Subject: [PATCH] reset alembic --- .../20230722_1219_2dcc708f88f8_user_auth.py | 40 ------------------- ...ions.py => 20240102_2012_68cd4c3a0af0_.py} | 39 +++++++++++++----- app/models/stuff.py | 22 ++++++++-- 3 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 alembic/versions/20230722_1219_2dcc708f88f8_user_auth.py rename alembic/versions/{20230311_2008_0d1ee3949d21_init_migrations.py => 20240102_2012_68cd4c3a0af0_.py} (80%) diff --git a/alembic/versions/20230722_1219_2dcc708f88f8_user_auth.py b/alembic/versions/20230722_1219_2dcc708f88f8_user_auth.py deleted file mode 100644 index bccd19d..0000000 --- a/alembic/versions/20230722_1219_2dcc708f88f8_user_auth.py +++ /dev/null @@ -1,40 +0,0 @@ -"""user auth - -Revision ID: 2dcc708f88f8 -Revises: 0d1ee3949d21 -Create Date: 2023-07-22 12:19:28.780926 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '2dcc708f88f8' -down_revision = '0d1ee3949d21' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('user', - sa.Column('id', sa.UUID(), nullable=False), - sa.Column('email', sa.String(), nullable=False), - sa.Column('first_name', sa.String(), nullable=False), - sa.Column('last_name', sa.String(), nullable=False), - sa.Column('password', sa.LargeBinary(), nullable=False), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('id') - ) - op.create_unique_constraint(None, 'nonsense', ['name'], schema='happy_hog') - op.create_unique_constraint(None, 'stuff', ['name'], schema='happy_hog') - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint(None, 'stuff', schema='happy_hog', type_='unique') - op.drop_constraint(None, 'nonsense', schema='happy_hog', type_='unique') - op.drop_table('user') - # ### end Alembic commands ### diff --git a/alembic/versions/20230311_2008_0d1ee3949d21_init_migrations.py b/alembic/versions/20240102_2012_68cd4c3a0af0_.py similarity index 80% rename from alembic/versions/20230311_2008_0d1ee3949d21_init_migrations.py rename to alembic/versions/20240102_2012_68cd4c3a0af0_.py index 0691d00..da157e1 100644 --- a/alembic/versions/20230311_2008_0d1ee3949d21_init_migrations.py +++ b/alembic/versions/20240102_2012_68cd4c3a0af0_.py @@ -1,16 +1,16 @@ -"""init migrations +""" -Revision ID: 0d1ee3949d21 +Revision ID: 68cd4c3a0af0 Revises: -Create Date: 2023-03-11 20:08:07.362613 +Create Date: 2024-01-02 20:12:46.214651 """ from alembic import op import sqlalchemy as sa -from sqlalchemy.dialects import postgresql + # revision identifiers, used by Alembic. -revision = '0d1ee3949d21' +revision = '68cd4c3a0af0' down_revision = None branch_labels = None depends_on = None @@ -19,18 +19,18 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('nonsense', - sa.Column('id', sa.UUID(), autoincrement=True, nullable=True), + sa.Column('id', sa.UUID(), autoincrement=True, nullable=False), sa.Column('name', sa.String(), nullable=False), - sa.Column('description', sa.String(), nullable=False), + sa.Column('description', sa.String(), nullable=True), sa.PrimaryKeyConstraint('name'), sa.UniqueConstraint('id'), sa.UniqueConstraint('name'), schema='happy_hog' ) op.create_table('stuff', - sa.Column('id', sa.UUID(), autoincrement=True, nullable=True), + sa.Column('id', sa.UUID(), autoincrement=True, nullable=False), sa.Column('name', sa.String(), nullable=False), - sa.Column('description', sa.String(), nullable=False), + sa.Column('description', sa.String(), nullable=True), sa.PrimaryKeyConstraint('name'), sa.UniqueConstraint('id'), sa.UniqueConstraint('name'), @@ -67,6 +67,25 @@ def upgrade(): sa.PrimaryKeyConstraint('id', name='work_pkey'), schema='shakespeare' ) + op.create_table('user', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('email', sa.String(), nullable=False), + sa.Column('first_name', sa.String(), nullable=False), + sa.Column('last_name', sa.String(), nullable=False), + sa.Column('_password', sa.LargeBinary(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email') + ) + op.create_table('stuff_full_of_nonsense', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('stuff_id', sa.UUID(), nullable=False), + sa.Column('nonsense_id', sa.UUID(), nullable=False), + sa.Column('but_why', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['nonsense_id'], ['happy_hog.nonsense.id'], ), + sa.ForeignKeyConstraint(['stuff_id'], ['happy_hog.stuff.id'], ), + sa.PrimaryKeyConstraint('id'), + schema='happy_hog' + ) op.create_table('chapter', sa.Column('id', sa.Integer(), nullable=False), sa.Column('work_id', sa.String(length=32), nullable=False), @@ -113,6 +132,8 @@ def downgrade(): op.drop_table('paragraph', schema='shakespeare') op.drop_table('character_work', schema='shakespeare') op.drop_table('chapter', schema='shakespeare') + op.drop_table('stuff_full_of_nonsense', schema='happy_hog') + op.drop_table('user') op.drop_table('work', schema='shakespeare') op.drop_table('wordform', schema='shakespeare') op.drop_table('character', schema='shakespeare') diff --git a/app/models/stuff.py b/app/models/stuff.py index e0b3c49..8bf33af 100644 --- a/app/models/stuff.py +++ b/app/models/stuff.py @@ -1,12 +1,13 @@ import uuid from fastapi import HTTPException, status -from sqlalchemy import String, select +from sqlalchemy import String, select, ForeignKey from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.asyncio import AsyncSession -from sqlalchemy.orm import mapped_column, Mapped +from sqlalchemy.orm import mapped_column, Mapped, relationship, joinedload from app.models.base import Base +from app.models.nonsense import Nonsense class Stuff(Base): @@ -16,6 +17,8 @@ class Stuff(Base): name: Mapped[str] = mapped_column(String, primary_key=True, unique=True) description: Mapped[str | None] + nonsense: Mapped["Nonsense"] = relationship("Nonsense", secondary="happy_hog.stuff_full_of_nonsense") + @classmethod async def find(cls, db_session: AsyncSession, name: str): """ @@ -24,7 +27,11 @@ class Stuff(Base): :param name: :return: """ - stmt = select(cls).where(cls.name == name) + stmt = ( + select(cls) + .options(joinedload(cls.nonsense)) + .where(cls.name == name) + ) result = await db_session.execute(stmt) instance = result.scalars().first() if instance is None: @@ -34,3 +41,12 @@ class Stuff(Base): ) else: return instance + + +class StuffFullOfNonsense(Base): + __tablename__ = "stuff_full_of_nonsense" + __table_args__ = ({"schema": "happy_hog"},) + id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True) + stuff_id: Mapped[Stuff] = mapped_column(UUID, ForeignKey("happy_hog.stuff.id")) + nonsense_id: Mapped["Nonsense"] = mapped_column(UUID, ForeignKey("happy_hog.nonsense.id")) + but_why: Mapped[str | None]