use alembic
This commit is contained in:
parent
afdc8e30ca
commit
a32abcf472
|
@ -1,8 +1,8 @@
|
|||
"""create users table
|
||||
|
||||
Revision ID: f4b761edb8cd
|
||||
Revision ID: 799cc8915cb1
|
||||
Revises:
|
||||
Create Date: 2024-07-11 21:53:59.445637
|
||||
Create Date: 2024-07-11 22:08:25.362239
|
||||
|
||||
"""
|
||||
|
||||
|
@ -13,7 +13,7 @@ import sqlalchemy as sa
|
|||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "f4b761edb8cd"
|
||||
revision: str = "799cc8915cb1"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
@ -24,10 +24,11 @@ def upgrade() -> None:
|
|||
"users",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("username", sa.String(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("username"),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_users")),
|
||||
sa.UniqueConstraint("username", name=op.f("uq_users_username")),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("users")
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
"""update users table
|
||||
|
||||
Revision ID: 443e39c236a6
|
||||
Revises: 799cc8915cb1
|
||||
Create Date: 2024-07-11 22:11:47.011912
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "443e39c236a6"
|
||||
down_revision: Union[str, None] = "799cc8915cb1"
|
||||
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("foo", sa.Integer(), nullable=False))
|
||||
op.add_column("users", sa.Column("bar", sa.Integer(), nullable=False))
|
||||
op.create_unique_constraint(
|
||||
op.f("uq_users_foo_bar"), "users", ["foo", "bar"]
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint(op.f("uq_users_foo_bar"), "users", type_="unique")
|
||||
op.drop_column("users", "bar")
|
||||
op.drop_column("users", "foo")
|
||||
# ### end Alembic commands ###
|
|
@ -25,6 +25,13 @@ class DatabaseConfig(BaseModel):
|
|||
pool_size: int = 50
|
||||
max_overflow: int = 10
|
||||
|
||||
naming_convention: dict[str, str] = {
|
||||
'ix': 'ix_%(column_0_label)s',
|
||||
'uq': 'uq_%(table_name)s_%(column_0_N_name)s',
|
||||
'ck': 'ck_%(table_name)s_%(constraint_name)s',
|
||||
'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s',
|
||||
'pk': 'pk_%(table_name)s',
|
||||
}
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from sqlalchemy import MetaData
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, declared_attr
|
||||
|
||||
from core.config import settings
|
||||
from utils import camel_case_to_snake_case
|
||||
|
||||
|
||||
|
@ -7,6 +9,9 @@ from utils import camel_case_to_snake_case
|
|||
class Base(DeclarativeBase):
|
||||
__abstract__ = True
|
||||
|
||||
metadata = MetaData(
|
||||
naming_convention=settings.db.naming_convention,
|
||||
)
|
||||
@declared_attr.directive
|
||||
def __tablename__(cls) -> str:
|
||||
return f'{camel_case_to_snake_case(cls.__name__)}s'
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from sqlalchemy import UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from .base import Base
|
||||
|
@ -5,3 +6,9 @@ from .base import Base
|
|||
|
||||
class User(Base):
|
||||
username: Mapped[str] = mapped_column(unique=True)
|
||||
foo: Mapped[int]
|
||||
bar: Mapped[int]
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint('foo', 'bar'),
|
||||
)
|
Loading…
Reference in New Issue