Init first files
This commit is contained in:
48
backend/alembic/env.py
Normal file
48
backend/alembic/env.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
from alembic import context
|
||||
from app.core.config import get_settings
|
||||
from app.core.db import Base
|
||||
from app.models import models # noqa: F401
|
||||
|
||||
config = context.config
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
settings = get_settings()
|
||||
config.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(url=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_migrations_online() -> 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()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
import asyncio
|
||||
|
||||
asyncio.run(run_migrations_online())
|
||||
24
backend/alembic/script.py.mako
Normal file
24
backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,24 @@
|
||||
"""${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 identifiers, used by Alembic.
|
||||
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"}
|
||||
98
backend/alembic/versions/0001_init.py
Normal file
98
backend/alembic/versions/0001_init.py
Normal file
@@ -0,0 +1,98 @@
|
||||
"""init schema
|
||||
|
||||
Revision ID: 0001_init
|
||||
Revises:
|
||||
Create Date: 2026-02-12
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "0001_init"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.Integer(), primary_key=True),
|
||||
sa.Column("email", sa.String(length=255), nullable=False, unique=True),
|
||||
sa.Column("password_hash", sa.String(length=255), nullable=False),
|
||||
sa.Column("role", sa.String(length=20), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_users_email", "users", ["email"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"targets",
|
||||
sa.Column("id", sa.Integer(), primary_key=True),
|
||||
sa.Column("name", sa.String(length=120), nullable=False, unique=True),
|
||||
sa.Column("host", sa.String(length=255), nullable=False),
|
||||
sa.Column("port", sa.Integer(), nullable=False, server_default="5432"),
|
||||
sa.Column("dbname", sa.String(length=120), nullable=False),
|
||||
sa.Column("username", sa.String(length=120), nullable=False),
|
||||
sa.Column("encrypted_password", sa.Text(), nullable=False),
|
||||
sa.Column("sslmode", sa.String(length=20), nullable=False, server_default="prefer"),
|
||||
sa.Column("tags", sa.JSON(), nullable=False, server_default="{}"),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_targets_name", "targets", ["name"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"metrics",
|
||||
sa.Column("id", sa.Integer(), primary_key=True),
|
||||
sa.Column("target_id", sa.Integer(), sa.ForeignKey("targets.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("ts", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("metric_name", sa.String(length=120), nullable=False),
|
||||
sa.Column("value", sa.Float(), nullable=False),
|
||||
sa.Column("labels", sa.JSON(), nullable=False, server_default="{}"),
|
||||
)
|
||||
op.create_index("ix_metrics_target_id", "metrics", ["target_id"])
|
||||
op.create_index("ix_metrics_ts", "metrics", ["ts"])
|
||||
op.create_index("ix_metrics_metric_name", "metrics", ["metric_name"])
|
||||
|
||||
op.create_table(
|
||||
"query_stats",
|
||||
sa.Column("id", sa.Integer(), primary_key=True),
|
||||
sa.Column("target_id", sa.Integer(), sa.ForeignKey("targets.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("ts", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("queryid", sa.String(length=100), nullable=False),
|
||||
sa.Column("calls", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("total_time", sa.Float(), nullable=False, server_default="0"),
|
||||
sa.Column("mean_time", sa.Float(), nullable=False, server_default="0"),
|
||||
sa.Column("rows", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("query_text", sa.Text(), nullable=True),
|
||||
)
|
||||
op.create_index("ix_query_stats_target_id", "query_stats", ["target_id"])
|
||||
op.create_index("ix_query_stats_ts", "query_stats", ["ts"])
|
||||
|
||||
op.create_table(
|
||||
"audit_logs",
|
||||
sa.Column("id", sa.Integer(), primary_key=True),
|
||||
sa.Column("ts", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
|
||||
sa.Column("action", sa.String(length=120), nullable=False),
|
||||
sa.Column("payload", sa.JSON(), nullable=False, server_default="{}"),
|
||||
)
|
||||
op.create_index("ix_audit_logs_ts", "audit_logs", ["ts"])
|
||||
op.create_index("ix_audit_logs_user_id", "audit_logs", ["user_id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_audit_logs_user_id", table_name="audit_logs")
|
||||
op.drop_index("ix_audit_logs_ts", table_name="audit_logs")
|
||||
op.drop_table("audit_logs")
|
||||
op.drop_index("ix_query_stats_ts", table_name="query_stats")
|
||||
op.drop_index("ix_query_stats_target_id", table_name="query_stats")
|
||||
op.drop_table("query_stats")
|
||||
op.drop_index("ix_metrics_metric_name", table_name="metrics")
|
||||
op.drop_index("ix_metrics_ts", table_name="metrics")
|
||||
op.drop_index("ix_metrics_target_id", table_name="metrics")
|
||||
op.drop_table("metrics")
|
||||
op.drop_index("ix_targets_name", table_name="targets")
|
||||
op.drop_table("targets")
|
||||
op.drop_index("ix_users_email", table_name="users")
|
||||
op.drop_table("users")
|
||||
Reference in New Issue
Block a user