Files
Scrobbler/src/scrobbler/migrations/versions/d6113d27cce6_initial_schema.py
T
mattandClaude Opus 5.5 c79c42ee00
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 50s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
Federation module skeleton and its own migration branch (phase 0, steps 3-4)
- scrobbler.federation with init_app() as its only entry point, called
  from create_app(); does nothing unless FEDERATION_ENABLED
- FederationConfig validates FEDERATION_DOMAIN (bare host) and
  FEDERATION_BASE_URL (https) when enabled, with clear errors
- federation/protocol/ package for pure, framework-free protocol code
- Package README with the boundary rules, configuration and migration
  workflow; .env.example lists the settings (off)
- Alembic: existing history labelled `core`; new empty `federation`
  branch root (depends on the core users table). Everything upgrades
  `heads` (entrypoint, CI, tests); tests check both branches apply and
  that federation can be removed without touching core

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 19:59:47 +01:00

146 lines
6.4 KiB
Python

"""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 = ('core',)
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 ###