App factory, data model, initial migration and HTTP metrics
- Flask app factory with env-driven config (.env loaded at import) - SQLAlchemy models: users, API apps, UI tokens, Last.fm auth tokens, sessions, scrobbles (deduplicated) and now-playing - Initial Alembic migration; CI checks migrations match the models - Prometheus metric definitions, per-endpoint RED hooks with bounded labels, and a standalone metrics server - docker compose Postgres with a test database; CI test job runs against a Postgres service container Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
606348800b
commit
98c8f2e824
21 files changed
+1777
-6
No files matched your search
@@ -0,0 +1,145 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: d6113d27cce6
|
||||
Revises:
|
||||
Create Date: 2026-09-25 13:54:01.877141
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'd6113d27cce6'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=64), nullable=False),
|
||||
sa.Column('email', sa.String(length=255), nullable=False),
|
||||
sa.Column('password_hash', sa.String(length=255), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.create_index('uq_users_username_lower', [sa.literal_column('lower(username)')], unique=True)
|
||||
|
||||
op.create_table('api_apps',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('api_key', sa.String(length=32), nullable=False),
|
||||
sa.Column('shared_secret', sa.String(length=32), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('owner_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['owner_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('api_key')
|
||||
)
|
||||
with op.batch_alter_table('api_apps', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_api_apps_owner_id'), ['owner_id'], unique=False)
|
||||
|
||||
op.create_table('now_playing',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('artist', sa.Text(), nullable=False),
|
||||
sa.Column('track', sa.Text(), nullable=False),
|
||||
sa.Column('album', sa.Text(), nullable=True),
|
||||
sa.Column('album_artist', sa.Text(), nullable=True),
|
||||
sa.Column('duration', sa.Integer(), nullable=True),
|
||||
sa.Column('started_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('user_id')
|
||||
)
|
||||
op.create_table('ui_tokens',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('token_hash', sa.String(length=64), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('last_used_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('token_hash')
|
||||
)
|
||||
with op.batch_alter_table('ui_tokens', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_ui_tokens_user_id'), ['user_id'], unique=False)
|
||||
|
||||
op.create_table('auth_tokens',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('token', sa.String(length=32), nullable=False),
|
||||
sa.Column('api_app_id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['api_app_id'], ['api_apps.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('token')
|
||||
)
|
||||
op.create_table('scrobbles',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('artist', sa.Text(), nullable=False),
|
||||
sa.Column('track', sa.Text(), nullable=False),
|
||||
sa.Column('album', sa.Text(), nullable=True),
|
||||
sa.Column('album_artist', sa.Text(), nullable=True),
|
||||
sa.Column('track_number', sa.Integer(), nullable=True),
|
||||
sa.Column('duration', sa.Integer(), nullable=True),
|
||||
sa.Column('mbid', sa.String(length=36), nullable=True),
|
||||
sa.Column('played_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('submitted_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('api_app_id', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['api_app_id'], ['api_apps.id'], ondelete='SET NULL'),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('user_id', 'played_at', 'artist', 'track', name='uq_scrobbles_dedupe')
|
||||
)
|
||||
with op.batch_alter_table('scrobbles', schema=None) as batch_op:
|
||||
batch_op.create_index('ix_scrobbles_user_played_at', ['user_id', sa.literal_column('played_at DESC')], unique=False)
|
||||
|
||||
op.create_table('sessions',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('session_key', sa.String(length=32), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('api_app_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['api_app_id'], ['api_apps.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('session_key')
|
||||
)
|
||||
with op.batch_alter_table('sessions', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_sessions_user_id'), ['user_id'], unique=False)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('sessions', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_sessions_user_id'))
|
||||
|
||||
op.drop_table('sessions')
|
||||
with op.batch_alter_table('scrobbles', schema=None) as batch_op:
|
||||
batch_op.drop_index('ix_scrobbles_user_played_at')
|
||||
|
||||
op.drop_table('scrobbles')
|
||||
op.drop_table('auth_tokens')
|
||||
with op.batch_alter_table('ui_tokens', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_ui_tokens_user_id'))
|
||||
|
||||
op.drop_table('ui_tokens')
|
||||
op.drop_table('now_playing')
|
||||
with op.batch_alter_table('api_apps', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_api_apps_owner_id'))
|
||||
|
||||
op.drop_table('api_apps')
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_index('uq_users_username_lower')
|
||||
|
||||
op.drop_table('users')
|
||||
# ### end Alembic commands ###
|
||||
Reference in new issue
Block a user