Изменения БД
This commit is contained in:
parent
11b1ea014f
commit
94cd0e36a6
9 changed files with 140 additions and 76 deletions
|
@ -8,7 +8,7 @@ from alembic import context
|
||||||
sys.path.insert(0, dirname(dirname(abspath(__file__))))
|
sys.path.insert(0, dirname(dirname(abspath(__file__))))
|
||||||
|
|
||||||
from app.database import DATABASE_URL, Base
|
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
|
from app.users.chat.models import Chats, Messages, UsersXChats # noqa
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""Изменение models.py
|
"""Добавлена таблица с кодами подтверждения
|
||||||
|
|
||||||
Revision ID: dd610307d6a0
|
Revision ID: 3a44aa43d35d
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2024-02-09 12:49:56.643283
|
Create Date: 2024-02-10 14:19:16.644002
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from typing import Sequence, Union
|
from typing import Sequence, Union
|
||||||
|
@ -12,7 +12,7 @@ import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision: str = 'dd610307d6a0'
|
revision: str = '3a44aa43d35d'
|
||||||
down_revision: Union[str, None] = None
|
down_revision: Union[str, None] = None
|
||||||
branch_labels: Union[str, Sequence[str], None] = None
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
depends_on: 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('black_phoenix', sa.Boolean(), nullable=False),
|
||||||
sa.Column('avatar_image', sa.String(), server_default='app/static/images/ты уже пешка BP.png', nullable=True),
|
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_birth', sa.Date(), nullable=False),
|
||||||
|
sa.Column('date_of_registration', sa.Date(), server_default=sa.text('now()'), nullable=False),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('chats',
|
op.create_table('chats',
|
||||||
|
@ -37,9 +38,18 @@ def upgrade() -> None:
|
||||||
sa.ForeignKeyConstraint(['chat_for'], ['users.id'], ),
|
sa.ForeignKeyConstraint(['chat_for'], ['users.id'], ),
|
||||||
sa.PrimaryKeyConstraint('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',
|
op.create_table('messages',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
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('image_url', sa.String(), nullable=True),
|
||||||
sa.Column('chat_id', sa.Integer(), nullable=True),
|
sa.Column('chat_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('user_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! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.drop_table('usersxchats')
|
op.drop_table('usersxchats')
|
||||||
op.drop_table('messages')
|
op.drop_table('messages')
|
||||||
|
op.drop_table('usersverificationcodes')
|
||||||
op.drop_table('chats')
|
op.drop_table('chats')
|
||||||
op.drop_table('users')
|
op.drop_table('users')
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
|
@ -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 ###
|
|
|
@ -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 ###
|
|
|
@ -27,7 +27,7 @@ class Messages(Base):
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
chat_id = mapped_column(ForeignKey("chats.id"))
|
chat_id = mapped_column(ForeignKey("chats.id"))
|
||||||
user_id = mapped_column(ForeignKey("users.id"))
|
user_id = mapped_column(ForeignKey("users.id"))
|
||||||
message: Mapped[str]
|
message: Mapped[Optional[str]]
|
||||||
image_url: Mapped[Optional[str]]
|
image_url: Mapped[Optional[str]]
|
||||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||||
visibility: Mapped[bool] = mapped_column(server_default='true')
|
visibility: Mapped[bool] = mapped_column(server_default='true')
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import DateTime, func
|
from sqlalchemy import func, ForeignKey, DateTime
|
||||||
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
||||||
|
|
||||||
from app.database import Base
|
from app.database import Base
|
||||||
|
@ -23,6 +23,22 @@ class Users(Base):
|
||||||
message = relationship("Messages", back_populates="user")
|
message = relationship("Messages", back_populates="user")
|
||||||
usersxchats = relationship("UsersXChats", back_populates="user")
|
usersxchats = relationship("UsersXChats", back_populates="user")
|
||||||
chat = relationship("Chats", back_populates="user_to_exclude")
|
chat = relationship("Chats", back_populates="user_to_exclude")
|
||||||
|
verificationcode = relationship("UsersVerificationCodes", back_populates="user")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Юзер {self.username}"
|
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}"
|
||||||
|
|
1
chat_test/migrations/README
Normal file
1
chat_test/migrations/README
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Generic single-database configuration.
|
78
chat_test/migrations/env.py
Normal file
78
chat_test/migrations/env.py
Normal file
|
@ -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()
|
26
chat_test/migrations/script.py.mako
Normal file
26
chat_test/migrations/script.py.mako
Normal file
|
@ -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"}
|
Loading…
Add table
Reference in a new issue