first commit
This commit is contained in:
62
alembic/env.py
Normal file
62
alembic/env.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.base import Base
|
||||
from app.models import car, expense, user # noqa: F401
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=settings.database_url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
import asyncio
|
||||
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
123
alembic/versions/202605110001_initial_schema.py
Normal file
123
alembic/versions/202605110001_initial_schema.py
Normal file
@@ -0,0 +1,123 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: 202605110001
|
||||
Revises:
|
||||
Create Date: 2026-05-11
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605110001"
|
||||
down_revision: str | None = None
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
service_type = sa.Enum(
|
||||
"maintenance",
|
||||
"repair",
|
||||
"fluid",
|
||||
"tire",
|
||||
"inspection",
|
||||
"insurance",
|
||||
"tax",
|
||||
"other",
|
||||
name="servicetype",
|
||||
create_type=False,
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("telegram_id", sa.BigInteger(), nullable=False),
|
||||
sa.Column("username", sa.String(length=128), nullable=True),
|
||||
sa.Column("first_name", sa.String(length=128), nullable=True),
|
||||
sa.Column("last_name", sa.String(length=128), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_users_telegram_id"), "users", ["telegram_id"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"cars",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("owner_id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(length=160), nullable=False),
|
||||
sa.Column("make", sa.String(length=80), nullable=True),
|
||||
sa.Column("model", sa.String(length=80), nullable=True),
|
||||
sa.Column("year", sa.Integer(), nullable=True),
|
||||
sa.Column("plate_number", sa.String(length=32), nullable=True),
|
||||
sa.Column("vin", sa.String(length=32), nullable=True),
|
||||
sa.Column("fuel_type", sa.String(length=32), nullable=True),
|
||||
sa.Column("purchase_date", sa.Date(), nullable=True),
|
||||
sa.Column("purchase_price", sa.Numeric(12, 2), nullable=True),
|
||||
sa.Column("current_odometer", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_cars_owner_id"), "cars", ["owner_id"], unique=False)
|
||||
|
||||
op.create_table(
|
||||
"fuel_entries",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("car_id", sa.Integer(), nullable=False),
|
||||
sa.Column("entry_date", sa.Date(), nullable=False),
|
||||
sa.Column("odometer", sa.Integer(), nullable=False),
|
||||
sa.Column("liters", sa.Numeric(8, 3), nullable=False),
|
||||
sa.Column("price_per_liter", sa.Numeric(10, 2), nullable=False),
|
||||
sa.Column("total_cost", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("station", sa.String(length=160), nullable=True),
|
||||
sa.Column("fuel_brand", sa.String(length=80), nullable=True),
|
||||
sa.Column("is_full_tank", sa.Boolean(), nullable=False),
|
||||
sa.Column("notes", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["car_id"], ["cars.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_fuel_entries_car_id"), "fuel_entries", ["car_id"], unique=False)
|
||||
op.create_index(op.f("ix_fuel_entries_entry_date"), "fuel_entries", ["entry_date"], unique=False)
|
||||
|
||||
op.create_table(
|
||||
"service_entries",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("car_id", sa.Integer(), nullable=False),
|
||||
sa.Column("entry_date", sa.Date(), nullable=False),
|
||||
sa.Column("odometer", sa.Integer(), nullable=True),
|
||||
sa.Column("service_type", service_type, nullable=False),
|
||||
sa.Column("title", sa.String(length=180), nullable=False),
|
||||
sa.Column("category", sa.String(length=80), nullable=True),
|
||||
sa.Column("vendor", sa.String(length=160), nullable=True),
|
||||
sa.Column("total_cost", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("next_due_date", sa.Date(), nullable=True),
|
||||
sa.Column("next_due_odometer", sa.Integer(), nullable=True),
|
||||
sa.Column("notes", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["car_id"], ["cars.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_service_entries_car_id"), "service_entries", ["car_id"], unique=False)
|
||||
op.create_index(op.f("ix_service_entries_entry_date"), "service_entries", ["entry_date"], unique=False)
|
||||
op.create_index(op.f("ix_service_entries_service_type"), "service_entries", ["service_type"], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(op.f("ix_service_entries_service_type"), table_name="service_entries")
|
||||
op.drop_index(op.f("ix_service_entries_entry_date"), table_name="service_entries")
|
||||
op.drop_index(op.f("ix_service_entries_car_id"), table_name="service_entries")
|
||||
op.drop_table("service_entries")
|
||||
op.drop_index(op.f("ix_fuel_entries_entry_date"), table_name="fuel_entries")
|
||||
op.drop_index(op.f("ix_fuel_entries_car_id"), table_name="fuel_entries")
|
||||
op.drop_table("fuel_entries")
|
||||
op.drop_index(op.f("ix_cars_owner_id"), table_name="cars")
|
||||
op.drop_table("cars")
|
||||
op.drop_index(op.f("ix_users_telegram_id"), table_name="users")
|
||||
op.drop_table("users")
|
||||
service_type.drop(op.get_bind(), checkfirst=True)
|
||||
49
alembic/versions/202605110002_car_catalog.py
Normal file
49
alembic/versions/202605110002_car_catalog.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""car catalog
|
||||
|
||||
Revision ID: 202605110002
|
||||
Revises: 202605110001
|
||||
Create Date: 2026-05-11
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605110002"
|
||||
down_revision: str | None = "202605110001"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"car_makes",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(length=80), nullable=False),
|
||||
sa.Column("country", sa.String(length=80), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_car_makes_name"), "car_makes", ["name"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"car_models",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("make_id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(length=100), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["make_id"], ["car_makes.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("make_id", "name", name="uq_car_models_make_name"),
|
||||
)
|
||||
op.create_index(op.f("ix_car_models_make_id"), "car_models", ["make_id"], unique=False)
|
||||
op.create_index(op.f("ix_car_models_name"), "car_models", ["name"], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(op.f("ix_car_models_name"), table_name="car_models")
|
||||
op.drop_index(op.f("ix_car_models_make_id"), table_name="car_models")
|
||||
op.drop_table("car_models")
|
||||
op.drop_index(op.f("ix_car_makes_name"), table_name="car_makes")
|
||||
op.drop_table("car_makes")
|
||||
26
alembic/versions/202605110003_user_preferences.py
Normal file
26
alembic/versions/202605110003_user_preferences.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""user preferences
|
||||
|
||||
Revision ID: 202605110003
|
||||
Revises: 202605110002
|
||||
Create Date: 2026-05-11
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605110003"
|
||||
down_revision: str | None = "202605110002"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("users", sa.Column("locale", sa.String(length=8), server_default="ru", nullable=False))
|
||||
op.add_column("users", sa.Column("currency", sa.String(length=3), server_default="RUB", nullable=False))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("users", "currency")
|
||||
op.drop_column("users", "locale")
|
||||
Reference in New Issue
Block a user