add contacts and address to database
This commit is contained in:
1533
alembic/versions/01691d3fa96a_initial_tables.py
Normal file
1533
alembic/versions/01691d3fa96a_initial_tables.py
Normal file
File diff suppressed because it is too large
Load Diff
60
alembic/versions/c151f5abb66d_add_contacts_and_address.py
Normal file
60
alembic/versions/c151f5abb66d_add_contacts_and_address.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
"""add contacts and address
|
||||||
|
|
||||||
|
Revision ID: c151f5abb66d
|
||||||
|
Revises: 01691d3fa96a
|
||||||
|
Create Date: 2025-10-01 18:58:32.345910
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'c151f5abb66d'
|
||||||
|
down_revision: Union[str, Sequence[str], None] = '01691d3fa96a'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Upgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('address',
|
||||||
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||||
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('country', sa.String(), nullable=False),
|
||||||
|
sa.Column('region', sa.String(), nullable=False),
|
||||||
|
sa.Column('locality', sa.String(), nullable=False),
|
||||||
|
sa.Column('street_address', sa.String(), nullable=False),
|
||||||
|
sa.Column('extended_address', sa.String(), nullable=False),
|
||||||
|
sa.Column('postal_code', sa.String(), nullable=False),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_table('contact',
|
||||||
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||||
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('type', sa.String(), nullable=False),
|
||||||
|
sa.Column('value', sa.String(), nullable=False),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.alter_column('user', 'tg_id',
|
||||||
|
existing_type=sa.BIGINT(),
|
||||||
|
nullable=True)
|
||||||
|
op.drop_column('user', 'address')
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Downgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('user', sa.Column('address', sa.VARCHAR(), autoincrement=False, nullable=True))
|
||||||
|
op.alter_column('user', 'tg_id',
|
||||||
|
existing_type=sa.BIGINT(),
|
||||||
|
nullable=False)
|
||||||
|
op.drop_table('contact')
|
||||||
|
op.drop_table('address')
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
|
|
||||||
from .models import Base
|
|
||||||
from .session import engine
|
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
async def init_db():
|
|
||||||
async with engine.begin() as conn:
|
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
if sys.platform == "win32":
|
|
||||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
|
||||||
asyncio.run(init_db())
|
|
||||||
@@ -13,14 +13,15 @@ class Base(DeclarativeBase):
|
|||||||
class User(Base):
|
class User(Base):
|
||||||
__tablename__ = "user"
|
__tablename__ = "user"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
tg_id: Mapped[int] = mapped_column(BigInteger, unique=True)
|
tg_id: Mapped[int] = mapped_column(BigInteger, nullable=True, unique=True)
|
||||||
fullname: Mapped[str] = mapped_column()
|
fullname: Mapped[str] = mapped_column()
|
||||||
role: Mapped[str] = mapped_column(default=UserRole.USER.value)
|
role: Mapped[str] = mapped_column(default=UserRole.USER.value)
|
||||||
phone: Mapped[str] = mapped_column(nullable=True)
|
phone: Mapped[str] = mapped_column(nullable=True)
|
||||||
address: Mapped[str] = mapped_column(nullable=True)
|
|
||||||
|
|
||||||
events = relationship("UserEvent", back_populates="user_rel")
|
events = relationship("UserEvent", back_populates="user_rel")
|
||||||
certificates = relationship("Certificate", back_populates="user_rel")
|
certificates = relationship("Certificate", back_populates="user_rel")
|
||||||
|
addresses = relationship("Address", back_populates="user_rel")
|
||||||
|
contacts = relationship("Contact", back_populates="user_rel")
|
||||||
|
|
||||||
|
|
||||||
class Event(Base):
|
class Event(Base):
|
||||||
@@ -34,7 +35,6 @@ class Event(Base):
|
|||||||
DateTime(timezone=True), server_default=func.now()
|
DateTime(timezone=True), server_default=func.now()
|
||||||
)
|
)
|
||||||
is_active: Mapped[bool] = mapped_column(default=True)
|
is_active: Mapped[bool] = mapped_column(default=True)
|
||||||
|
|
||||||
user_events = relationship("UserEvent", back_populates="event_rel")
|
user_events = relationship("UserEvent", back_populates="event_rel")
|
||||||
|
|
||||||
|
|
||||||
@@ -69,4 +69,32 @@ class Certificate(Base):
|
|||||||
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
||||||
course_id: Mapped[int] = mapped_column(ForeignKey("course.id"))
|
course_id: Mapped[int] = mapped_column(ForeignKey("course.id"))
|
||||||
date: Mapped[dt.datetime] = mapped_column()
|
date: Mapped[dt.datetime] = mapped_column()
|
||||||
|
|
||||||
|
course_rel = relationship("Course", back_populates="certificates")
|
||||||
|
user_rel = relationship("User", back_populates="certificates")
|
||||||
|
|
||||||
__table_args__ = (UniqueConstraint("user_id", "course_id", name="uq_user_course"),)
|
__table_args__ = (UniqueConstraint("user_id", "course_id", name="uq_user_course"),)
|
||||||
|
|
||||||
|
|
||||||
|
class Address(Base):
|
||||||
|
__tablename__ = "address"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
||||||
|
country: Mapped[str] = mapped_column()
|
||||||
|
region: Mapped[str] = mapped_column()
|
||||||
|
locality: Mapped[str] = mapped_column()
|
||||||
|
street_address: Mapped[str] = mapped_column()
|
||||||
|
extended_address: Mapped[str] = mapped_column()
|
||||||
|
postal_code: Mapped[str] = mapped_column()
|
||||||
|
|
||||||
|
user_rel = relationship("User", back_populates="addresses")
|
||||||
|
|
||||||
|
|
||||||
|
class Contact(Base):
|
||||||
|
__tablename__ = "contact"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
||||||
|
type: Mapped[str] = mapped_column()
|
||||||
|
value: Mapped[str] = mapped_column()
|
||||||
|
|
||||||
|
user_rel = relationship("User", back_populates="contacts")
|
||||||
|
|||||||
Reference in New Issue
Block a user