first commit

This commit is contained in:
eeymoo
2026-09-19 12:54:45 +08:00
commit 6fc5b64077
126 changed files with 8601 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.
+61
View File
@@ -0,0 +1,61 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.config import get_settings
from app.db import Base
from app.models import database # noqa: F401
settings = get_settings()
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def get_url():
return settings.database_url
def run_migrations_offline() -> None:
url = get_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:
configuration = config.get_section(config.config_ini_section, {})
configuration["sqlalchemy.url"] = get_url()
connectable = engine_from_config(
configuration,
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
View 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"}
+121
View File
@@ -0,0 +1,121 @@
"""initial
Revision ID: 0001
Revises:
Create Date: 2026-08-02 00:00:00.000000
"""
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 = "0001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"samples",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("repo", sa.String(255), nullable=False),
sa.Column("commit_sha", sa.String(40), nullable=False),
sa.Column("language", sa.String(50), nullable=False),
sa.Column("diff", sa.Text, nullable=False),
sa.Column("before_context", sa.Text, nullable=True),
sa.Column("after_context", sa.Text, nullable=True),
sa.Column("created_at", sa.DateTime, nullable=False),
)
op.create_table(
"defects",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("sample_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("samples.id"), nullable=False),
sa.Column("defect_type", sa.String(100), nullable=False),
sa.Column("language", sa.String(50), nullable=False),
sa.Column("line_start", sa.Integer, nullable=True),
sa.Column("line_end", sa.Integer, nullable=True),
sa.Column("description", sa.Text, nullable=True),
sa.Column("reference_fix", sa.Text, nullable=True),
sa.Column("created_at", sa.DateTime, nullable=False),
)
op.create_table(
"prompt_templates",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("strategy_id", sa.String(100), nullable=False),
sa.Column("level", sa.String(20), nullable=False),
sa.Column("created_at", sa.DateTime, nullable=False),
sa.UniqueConstraint("strategy_id", "level", name="uix_strategy_level"),
)
op.create_table(
"prompt_template_versions",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("template_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("prompt_templates.id"), nullable=False),
sa.Column("version_number", sa.Integer, nullable=False),
sa.Column("body", sa.Text, nullable=False),
sa.Column("variables_schema", sa.JSON, nullable=False),
sa.Column("created_at", sa.DateTime, nullable=False),
)
op.create_table(
"experiments",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("name", sa.String(255), nullable=False),
sa.Column("models", sa.JSON, nullable=False),
sa.Column("levels", sa.JSON, nullable=False),
sa.Column("sample_ids", sa.JSON, nullable=False),
sa.Column("repeats", sa.Integer, nullable=False),
sa.Column("sampling_params", sa.JSON, nullable=False),
sa.Column("status", sa.String(20), nullable=False),
sa.Column("created_at", sa.DateTime, nullable=False),
)
op.create_table(
"experiment_runs",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("run_id", sa.String(64), unique=True, nullable=False),
sa.Column("experiment_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("experiments.id"), nullable=False),
sa.Column("sample_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("samples.id"), nullable=False),
sa.Column("model_id", sa.String(100), nullable=False),
sa.Column("template_version_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("prompt_template_versions.id"), nullable=False),
sa.Column("repeat_index", sa.Integer, nullable=False),
sa.Column("status", sa.String(20), nullable=False),
sa.Column("retry_count", sa.Integer, nullable=False),
sa.Column("sampling_params", sa.JSON, nullable=False),
sa.Column("started_at", sa.DateTime, nullable=True),
sa.Column("completed_at", sa.DateTime, nullable=True),
sa.Column("created_at", sa.DateTime, nullable=False),
)
op.create_table(
"results",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("run_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("experiment_runs.id"), nullable=False, unique=True),
sa.Column("raw_output", sa.Text, nullable=True),
sa.Column("token_usage", sa.JSON, nullable=True),
sa.Column("latency_ms", sa.Float, nullable=True),
sa.Column("parsed_findings", sa.JSON, nullable=True),
sa.Column("detection_rate", sa.Float, nullable=True),
sa.Column("false_positive_rate", sa.Float, nullable=True),
sa.Column("coverage_rate", sa.Float, nullable=True),
sa.Column("stability_score", sa.Float, nullable=True),
sa.Column("likert_score", sa.Integer, nullable=True),
sa.Column("created_at", sa.DateTime, nullable=False),
sa.Column("updated_at", sa.DateTime, nullable=False),
)
def downgrade() -> None:
op.drop_table("results")
op.drop_table("experiment_runs")
op.drop_table("experiments")
op.drop_table("prompt_template_versions")
op.drop_table("prompt_templates")
op.drop_table("defects")
op.drop_table("samples")