feat: Add database migrations and auth system
- Add Alembic for database migrations - Implement user authentication system - Update frontend styles and components - Add new test audio functionality - Update stream management and UI
This commit is contained in:
1
alembic/README
Normal file
1
alembic/README
Normal file
@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
61
alembic/env.py
Normal file
61
alembic/env.py
Normal file
@ -0,0 +1,61 @@
|
||||
from logging.config import fileConfig
|
||||
import os
|
||||
import sys
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
from alembic import context
|
||||
|
||||
# Add the project root to the Python path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
# Import your SQLAlchemy models and engine
|
||||
from models import SQLModel
|
||||
from database import engine
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Import all your SQLModel models here so that Alembic can detect them
|
||||
from models import User, DBSession
|
||||
|
||||
# Set the target metadata to SQLModel.metadata
|
||||
target_metadata = SQLModel.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"},
|
||||
compare_type=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
connectable = engine
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
28
alembic/script.py.mako
Normal file
28
alembic/script.py.mako
Normal file
@ -0,0 +1,28 @@
|
||||
"""${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, Sequence[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:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
86
alembic/versions/1ab2db0e4b5e_make_username_unique.py
Normal file
86
alembic/versions/1ab2db0e4b5e_make_username_unique.py
Normal file
@ -0,0 +1,86 @@
|
||||
"""make username unique
|
||||
|
||||
Revision ID: 1ab2db0e4b5e
|
||||
Revises:
|
||||
Create Date: 2025-06-27 13:04:10.085253
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
import sqlmodel
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '1ab2db0e4b5e'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# 1. First, add the unique constraint to the username column
|
||||
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||
batch_op.create_unique_constraint('uq_user_username', ['username'])
|
||||
|
||||
# 2. Now create the dbsession table with the foreign key
|
||||
op.create_table('dbsession',
|
||||
sa.Column('token', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('user_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('ip_address', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('user_agent', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('last_activity', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.username'], ),
|
||||
sa.PrimaryKeyConstraint('token')
|
||||
)
|
||||
|
||||
# 3. Drop old tables if they exist
|
||||
if op.get_bind().engine.dialect.has_table(op.get_bind(), 'session'):
|
||||
op.drop_index(op.f('ix_session_token'), table_name='session')
|
||||
op.drop_index(op.f('ix_session_user_id'), table_name='session')
|
||||
op.drop_table('session')
|
||||
|
||||
if op.get_bind().engine.dialect.has_table(op.get_bind(), 'publicstream'):
|
||||
op.drop_table('publicstream')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# 1. First drop the dbsession table
|
||||
op.drop_table('dbsession')
|
||||
|
||||
# 2. Recreate the old tables
|
||||
op.create_table('publicstream',
|
||||
sa.Column('uid', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('size', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('mtime', sa.INTEGER(), autoincrement=False, nullable=False),
|
||||
sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
|
||||
sa.PrimaryKeyConstraint('uid', name=op.f('publicstream_pkey'))
|
||||
)
|
||||
|
||||
op.create_table('session',
|
||||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column('user_id', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('token', sa.TEXT(), autoincrement=False, nullable=True),
|
||||
sa.Column('ip_address', sa.VARCHAR(), autoincrement=False, nullable=False),
|
||||
sa.Column('user_agent', sa.VARCHAR(), autoincrement=False, nullable=True),
|
||||
sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
|
||||
sa.Column('expires_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
|
||||
sa.Column('last_used_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
|
||||
sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('session_pkey'))
|
||||
)
|
||||
|
||||
op.create_index(op.f('ix_session_user_id'), 'session', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_session_token'), 'session', ['token'], unique=True)
|
||||
|
||||
# 3. Finally, remove the unique constraint from username
|
||||
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||
batch_op.drop_constraint('uq_user_username', type_='unique')
|
||||
# ### end Alembic commands ###
|
@ -0,0 +1,30 @@
|
||||
"""add_processed_filename_to_uploadlog
|
||||
|
||||
Revision ID: f86c93c7a872
|
||||
Revises: 1ab2db0e4b5e
|
||||
Create Date: 2025-06-28 15:56:29.169668
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'f86c93c7a872'
|
||||
down_revision: Union[str, Sequence[str], None] = '1ab2db0e4b5e'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
op.add_column('uploadlog',
|
||||
sa.Column('processed_filename', sa.String(), nullable=True),
|
||||
schema=None)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
op.drop_column('uploadlog', 'processed_filename', schema=None)
|
Reference in New Issue
Block a user