Initial commit: Backend API Phase 1a complete
- FastAPI backend with JWT auth - PostgreSQL database schema - Trees and Sessions CRUD APIs - Export functionality (Markdown, Text, HTML) - Docker setup for local development - Alembic migrations
This commit is contained in:
81
backend/alembic/env.py
Normal file
81
backend/alembic/env.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import asyncio
|
||||
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
|
||||
|
||||
# Import your models
|
||||
from app.core.database import Base
|
||||
from app.models import User, Team, Tree, Session, Attachment
|
||||
from app.core.config import settings
|
||||
|
||||
# this is the Alembic Config object
|
||||
config = context.config
|
||||
|
||||
# Override sqlalchemy.url with the sync version for migrations
|
||||
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL_SYNC)
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
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_async_migrations() -> None:
|
||||
"""Run migrations in 'online' mode with async engine."""
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
connectable = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
connectable = create_engine(
|
||||
settings.DATABASE_URL_SYNC,
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
do_run_migrations(connection)
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
backend/alembic/script.py.mako
Normal file
26
backend/alembic/script.py.mako
Normal 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"}
|
||||
124
backend/alembic/versions/001_initial_schema.py
Normal file
124
backend/alembic/versions/001_initial_schema.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""Initial schema
|
||||
|
||||
Revision ID: 001
|
||||
Revises:
|
||||
Create Date: 2026-01-22
|
||||
|
||||
"""
|
||||
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 = '001'
|
||||
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:
|
||||
# Create teams table
|
||||
op.create_table(
|
||||
'teams',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create users table
|
||||
op.create_table(
|
||||
'users',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
|
||||
sa.Column('email', sa.String(255), nullable=False),
|
||||
sa.Column('password_hash', sa.String(255), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('role', sa.String(50), nullable=False, server_default='engineer'),
|
||||
sa.Column('team_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.Column('last_login', sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('email')
|
||||
)
|
||||
op.create_index('idx_users_email', 'users', ['email'], unique=True)
|
||||
|
||||
# Create trees table
|
||||
op.create_table(
|
||||
'trees',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('category', sa.String(100), nullable=True),
|
||||
sa.Column('tree_structure', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
|
||||
sa.Column('author_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('team_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), server_default='true', nullable=True),
|
||||
sa.Column('version', sa.Integer(), server_default='1', nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.Column('usage_count', sa.Integer(), server_default='0', nullable=True),
|
||||
sa.ForeignKeyConstraint(['author_id'], ['users.id'], ),
|
||||
sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_trees_category', 'trees', ['category'], unique=False)
|
||||
op.create_index('idx_trees_team', 'trees', ['team_id'], unique=False)
|
||||
# Full-text search index
|
||||
op.execute(
|
||||
"CREATE INDEX idx_trees_search ON trees USING gin(to_tsvector('english', coalesce(name, '') || ' ' || coalesce(description, '')))"
|
||||
)
|
||||
|
||||
# Create sessions table
|
||||
op.create_table(
|
||||
'sessions',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
|
||||
sa.Column('tree_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('tree_snapshot', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
|
||||
sa.Column('path_taken', postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default='[]'),
|
||||
sa.Column('decisions', postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default='[]'),
|
||||
sa.Column('started_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.Column('completed_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('ticket_number', sa.String(100), nullable=True),
|
||||
sa.Column('client_name', sa.String(255), nullable=True),
|
||||
sa.Column('exported', sa.Boolean(), server_default='false', nullable=True),
|
||||
sa.ForeignKeyConstraint(['tree_id'], ['trees.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('idx_sessions_user', 'sessions', ['user_id'], unique=False)
|
||||
op.create_index('idx_sessions_tree', 'sessions', ['tree_id'], unique=False)
|
||||
op.create_index('idx_sessions_dates', 'sessions', ['started_at', 'completed_at'], unique=False)
|
||||
|
||||
# Create attachments table
|
||||
op.create_table(
|
||||
'attachments',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
|
||||
sa.Column('session_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('node_id', sa.String(100), nullable=True),
|
||||
sa.Column('file_name', sa.String(255), nullable=False),
|
||||
sa.Column('file_type', sa.String(50), nullable=True),
|
||||
sa.Column('file_size', sa.Integer(), nullable=True),
|
||||
sa.Column('storage_path', sa.String(500), nullable=True),
|
||||
sa.Column('uploaded_at', sa.DateTime(), server_default=sa.text('NOW()'), nullable=True),
|
||||
sa.ForeignKeyConstraint(['session_id'], ['sessions.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table('attachments')
|
||||
op.drop_index('idx_sessions_dates', table_name='sessions')
|
||||
op.drop_index('idx_sessions_tree', table_name='sessions')
|
||||
op.drop_index('idx_sessions_user', table_name='sessions')
|
||||
op.drop_table('sessions')
|
||||
op.drop_index('idx_trees_search', table_name='trees')
|
||||
op.drop_index('idx_trees_team', table_name='trees')
|
||||
op.drop_index('idx_trees_category', table_name='trees')
|
||||
op.drop_table('trees')
|
||||
op.drop_index('idx_users_email', table_name='users')
|
||||
op.drop_table('users')
|
||||
op.drop_table('teams')
|
||||
Reference in New Issue
Block a user