Init Project Flexly Estimator

This commit is contained in:
Alexey Dudin
2026-11-03 09:37:44 +03:00
commit 447464c16c
160 changed files with 12779 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
"""Alembic environment: async engine, URL from the service settings (or -x dsn=...)."""
import asyncio
from alembic import context
from sqlalchemy.ext.asyncio import create_async_engine
from estimator.infra.db.session import normalize_dsn
from estimator.infra.db.tables import Base
config = context.config
target_metadata = Base.metadata
def _dsn() -> str:
override = context.get_x_argument(as_dictionary=True).get("dsn")
if override:
return normalize_dsn(override)
from estimator.config import get_settings
return normalize_dsn(get_settings().database.dsn.get_secret_value())
def _configure(connection) -> None:
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
render_as_batch=connection.dialect.name == "sqlite",
)
def _run_sync(connection) -> None:
_configure(connection)
with context.begin_transaction():
context.run_migrations()
async def _run_online() -> None:
engine = create_async_engine(_dsn())
try:
async with engine.connect() as conn:
await conn.run_sync(_run_sync)
finally:
await engine.dispose()
def run_migrations_offline() -> None:
context.configure(
url=_dsn(), target_metadata=target_metadata, literal_binds=True, compare_type=True
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(_run_online())
+22
View File
@@ -0,0 +1,22 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
+67
View File
@@ -0,0 +1,67 @@
"""estimates and estimate_versions
Revision ID: 0001
Revises:
Create Date: 2026-10-30
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None
JSON = sa.JSON().with_variant(postgresql.JSONB(), "postgresql")
TS = sa.DateTime(timezone=True)
def upgrade() -> None:
op.create_table(
"estimates",
sa.Column("id", sa.String(32), primary_key=True),
sa.Column("client", sa.String(100), nullable=False),
sa.Column("latest_version", sa.Integer, nullable=False),
sa.Column("idempotency_key", sa.String(200), nullable=True),
sa.Column("request_hash", sa.String(64), nullable=True),
sa.Column("created_at", TS, nullable=False),
sa.UniqueConstraint("client", "idempotency_key", name="uq_estimates_client_idem"),
)
op.create_index("ix_estimates_client_created", "estimates", ["client", "created_at", "id"])
op.create_table(
"estimate_versions",
sa.Column(
"estimate_id",
sa.String(32),
sa.ForeignKey("estimates.id", ondelete="CASCADE"),
primary_key=True,
),
sa.Column("version", sa.Integer, primary_key=True),
sa.Column("status", sa.String(20), nullable=False),
sa.Column("step", sa.String(20), nullable=True),
sa.Column("source", sa.String(20), nullable=False),
sa.Column("requirements", sa.Text, nullable=False),
sa.Column("region", sa.String(8), nullable=False),
sa.Column("currency", sa.String(8), nullable=False),
sa.Column("answers", JSON, nullable=False),
sa.Column("proceed_with_assumptions", sa.Boolean, nullable=False),
sa.Column("market_version", sa.String(32), nullable=True),
sa.Column("result", JSON, nullable=True),
sa.Column("price", JSON, nullable=True),
sa.Column("market_check", JSON, nullable=True),
sa.Column("error_code", sa.String(40), nullable=True),
sa.Column("error", sa.Text, nullable=True),
sa.Column("created_at", TS, nullable=False),
sa.Column("started_at", TS, nullable=True),
sa.Column("finished_at", TS, nullable=True),
)
op.create_index("ix_versions_status", "estimate_versions", ["status"])
def downgrade() -> None:
op.drop_index("ix_versions_status", table_name="estimate_versions")
op.drop_table("estimate_versions")
op.drop_index("ix_estimates_client_created", table_name="estimates")
op.drop_table("estimates")
+25
View File
@@ -0,0 +1,25 @@
"""engagement model per estimate version
Revision ID: 0002
Revises: 0001
Create Date: 2026-10-31
"""
import sqlalchemy as sa
from alembic import op
revision = "0002"
down_revision = "0001"
branch_labels = None
depends_on = None
def upgrade() -> None:
with op.batch_alter_table("estimate_versions") as batch:
batch.add_column(
sa.Column("engagement", sa.String(20), nullable=False, server_default="fixed_price")
)
def downgrade() -> None:
with op.batch_alter_table("estimate_versions") as batch:
batch.drop_column("engagement")
+23
View File
@@ -0,0 +1,23 @@
"""VAT rate per estimate version
Revision ID: 0003
Revises: 0002
Create Date: 2026-10-31
"""
import sqlalchemy as sa
from alembic import op
revision = "0003"
down_revision = "0002"
branch_labels = None
depends_on = None
def upgrade() -> None:
with op.batch_alter_table("estimate_versions") as batch:
batch.add_column(sa.Column("vat_rate", sa.Float, nullable=False, server_default="0"))
def downgrade() -> None:
with op.batch_alter_table("estimate_versions") as batch:
batch.drop_column("vat_rate")