diff --git a/chat_test/app/migrations/env.py b/chat_test/app/migrations/env.py index 0ff0215..d6cf50b 100644 --- a/chat_test/app/migrations/env.py +++ b/chat_test/app/migrations/env.py @@ -8,7 +8,7 @@ from alembic import context sys.path.insert(0, dirname(dirname(abspath(__file__)))) from app.database import DATABASE_URL, Base -from app.users.models import Users # noqa +from app.users.models import Users, UsersVerificationCodes # noqa from app.users.chat.models import Chats, Messages, UsersXChats # noqa # this is the Alembic Config object, which provides diff --git a/chat_test/app/migrations/versions/dd610307d6a0_изменение_models_py.py b/chat_test/app/migrations/versions/3a44aa43d35d_добавлена_таблица_с_кодами_подтверждения.py similarity index 74% rename from chat_test/app/migrations/versions/dd610307d6a0_изменение_models_py.py rename to chat_test/app/migrations/versions/3a44aa43d35d_добавлена_таблица_с_кодами_подтверждения.py index 043ce4d..51cc1ac 100644 --- a/chat_test/app/migrations/versions/dd610307d6a0_изменение_models_py.py +++ b/chat_test/app/migrations/versions/3a44aa43d35d_добавлена_таблица_с_кодами_подтверждения.py @@ -1,8 +1,8 @@ -"""Изменение models.py +"""Добавлена таблица с кодами подтверждения -Revision ID: dd610307d6a0 +Revision ID: 3a44aa43d35d Revises: -Create Date: 2024-02-09 12:49:56.643283 +Create Date: 2024-02-10 14:19:16.644002 """ from typing import Sequence, Union @@ -12,7 +12,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision: str = 'dd610307d6a0' +revision: str = '3a44aa43d35d' down_revision: Union[str, None] = None branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None @@ -29,6 +29,7 @@ def upgrade() -> None: sa.Column('black_phoenix', sa.Boolean(), nullable=False), sa.Column('avatar_image', sa.String(), server_default='app/static/images/ты уже пешка BP.png', nullable=True), sa.Column('date_of_birth', sa.Date(), nullable=False), + sa.Column('date_of_registration', sa.Date(), server_default=sa.text('now()'), nullable=False), sa.PrimaryKeyConstraint('id') ) op.create_table('chats', @@ -37,9 +38,18 @@ def upgrade() -> None: sa.ForeignKeyConstraint(['chat_for'], ['users.id'], ), sa.PrimaryKeyConstraint('id') ) + op.create_table('usersverificationcodes', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('code', sa.String(), nullable=False), + sa.Column('description', sa.String(), nullable=False), + sa.Column('date_of_creation', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) op.create_table('messages', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('message', sa.String(), nullable=False), + sa.Column('message', sa.String(), nullable=True), sa.Column('image_url', sa.String(), nullable=True), sa.Column('chat_id', sa.Integer(), nullable=True), sa.Column('user_id', sa.Integer(), nullable=True), @@ -64,6 +74,7 @@ def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.drop_table('usersxchats') op.drop_table('messages') + op.drop_table('usersverificationcodes') op.drop_table('chats') op.drop_table('users') # ### end Alembic commands ### diff --git a/chat_test/app/migrations/versions/8b2764a42ee6_изменение_models_py.py b/chat_test/app/migrations/versions/8b2764a42ee6_изменение_models_py.py deleted file mode 100644 index b7c1b44..0000000 --- a/chat_test/app/migrations/versions/8b2764a42ee6_изменение_models_py.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Изменение models.py - -Revision ID: 8b2764a42ee6 -Revises: dd610307d6a0 -Create Date: 2024-02-09 12:59:20.127130 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = '8b2764a42ee6' -down_revision: Union[str, None] = 'dd610307d6a0' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('users', sa.Column('date_of_registration', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False)) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('users', 'date_of_registration') - # ### end Alembic commands ### diff --git a/chat_test/app/migrations/versions/ae1bfb8ea886_изменение_models_py.py b/chat_test/app/migrations/versions/ae1bfb8ea886_изменение_models_py.py deleted file mode 100644 index 239c64f..0000000 --- a/chat_test/app/migrations/versions/ae1bfb8ea886_изменение_models_py.py +++ /dev/null @@ -1,38 +0,0 @@ -"""Изменение models.py - -Revision ID: ae1bfb8ea886 -Revises: 8b2764a42ee6 -Create Date: 2024-02-09 13:18:35.765055 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = 'ae1bfb8ea886' -down_revision: Union[str, None] = '8b2764a42ee6' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('users', 'date_of_registration', - existing_type=postgresql.TIMESTAMP(timezone=True), - type_=sa.Date(), - existing_nullable=False, - existing_server_default=sa.text('now()')) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('users', 'date_of_registration', - existing_type=sa.Date(), - type_=postgresql.TIMESTAMP(timezone=True), - existing_nullable=False, - existing_server_default=sa.text('now()')) - # ### end Alembic commands ### diff --git a/chat_test/app/users/chat/models.py b/chat_test/app/users/chat/models.py index 3e7cf95..022a7dd 100644 --- a/chat_test/app/users/chat/models.py +++ b/chat_test/app/users/chat/models.py @@ -27,7 +27,7 @@ class Messages(Base): id: Mapped[int] = mapped_column(primary_key=True) chat_id = mapped_column(ForeignKey("chats.id")) user_id = mapped_column(ForeignKey("users.id")) - message: Mapped[str] + message: Mapped[Optional[str]] image_url: Mapped[Optional[str]] created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) visibility: Mapped[bool] = mapped_column(server_default='true') diff --git a/chat_test/app/users/models.py b/chat_test/app/users/models.py index b9b2af4..a56088f 100644 --- a/chat_test/app/users/models.py +++ b/chat_test/app/users/models.py @@ -1,7 +1,7 @@ from datetime import date, datetime from typing import Optional -from sqlalchemy import DateTime, func +from sqlalchemy import func, ForeignKey, DateTime from sqlalchemy.orm import mapped_column, Mapped, relationship from app.database import Base @@ -23,6 +23,22 @@ class Users(Base): message = relationship("Messages", back_populates="user") usersxchats = relationship("UsersXChats", back_populates="user") chat = relationship("Chats", back_populates="user_to_exclude") + verificationcode = relationship("UsersVerificationCodes", back_populates="user") def __str__(self): return f"Юзер {self.username}" + + +class UsersVerificationCodes(Base): + __tablename__ = "usersverificationcodes" + + id: Mapped[int] = mapped_column(primary_key=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id")) + code: Mapped[str] + description: Mapped[str] + date_of_creation: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) + + user = relationship("Users", back_populates="verificationcode") + + def __str__(self): + return f"Код {self.code} для юзера {self.user}. {self.description}" diff --git a/chat_test/migrations/README b/chat_test/migrations/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/chat_test/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/chat_test/migrations/env.py b/chat_test/migrations/env.py new file mode 100644 index 0000000..36112a3 --- /dev/null +++ b/chat_test/migrations/env.py @@ -0,0 +1,78 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +# 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 + +# 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() diff --git a/chat_test/migrations/script.py.mako b/chat_test/migrations/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/chat_test/migrations/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"}