musicstreamingapi-pxgi21/alembic/versions/001_initial_migration.py
2025-06-05 05:49:19 +00:00

134 lines
5.6 KiB
Python

"""Initial database schema
Revision ID: 001
Revises:
Create Date: 2023-08-07 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create users table
op.create_table(
'users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('username', sa.String(), nullable=False),
sa.Column('hashed_password', sa.String(), nullable=False),
sa.Column('full_name', sa.String(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False, default=True),
sa.Column('is_superuser', sa.Boolean(), nullable=False, default=False),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
# Create artists table
op.create_table(
'artists',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('bio', sa.Text(), nullable=True),
sa.Column('image_path', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_artists_id'), 'artists', ['id'], unique=False)
op.create_index(op.f('ix_artists_name'), 'artists', ['name'], unique=False)
# Create albums table
op.create_table(
'albums',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('release_date', sa.Date(), nullable=True),
sa.Column('cover_image_path', sa.String(), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('artist_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['artist_id'], ['artists.id'], ),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_albums_id'), 'albums', ['id'], unique=False)
op.create_index(op.f('ix_albums_title'), 'albums', ['title'], unique=False)
# Create songs table
op.create_table(
'songs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('duration', sa.Float(), nullable=True),
sa.Column('file_path', sa.String(), nullable=False),
sa.Column('track_number', sa.Integer(), nullable=True),
sa.Column('artist_id', sa.Integer(), nullable=False),
sa.Column('album_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['album_id'], ['albums.id'], ),
sa.ForeignKeyConstraint(['artist_id'], ['artists.id'], ),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_songs_id'), 'songs', ['id'], unique=False)
op.create_index(op.f('ix_songs_title'), 'songs', ['title'], unique=False)
# Create playlists table
op.create_table(
'playlists',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('is_public', sa.Boolean(), nullable=False, default=True),
sa.Column('cover_image_path', sa.String(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_playlists_id'), 'playlists', ['id'], unique=False)
op.create_index(op.f('ix_playlists_name'), 'playlists', ['name'], unique=False)
# Create song_playlist association table
op.create_table(
'song_playlist',
sa.Column('song_id', sa.Integer(), nullable=False),
sa.Column('playlist_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['playlist_id'], ['playlists.id'], ),
sa.ForeignKeyConstraint(['song_id'], ['songs.id'], ),
sa.PrimaryKeyConstraint('song_id', 'playlist_id'),
)
def downgrade() -> None:
# Drop tables in reverse order
op.drop_table('song_playlist')
op.drop_index(op.f('ix_playlists_name'), table_name='playlists')
op.drop_index(op.f('ix_playlists_id'), table_name='playlists')
op.drop_table('playlists')
op.drop_index(op.f('ix_songs_title'), table_name='songs')
op.drop_index(op.f('ix_songs_id'), table_name='songs')
op.drop_table('songs')
op.drop_index(op.f('ix_albums_title'), table_name='albums')
op.drop_index(op.f('ix_albums_id'), table_name='albums')
op.drop_table('albums')
op.drop_index(op.f('ix_artists_name'), table_name='artists')
op.drop_index(op.f('ix_artists_id'), table_name='artists')
op.drop_table('artists')
op.drop_index(op.f('ix_users_username'), table_name='users')
op.drop_index(op.f('ix_users_id'), table_name='users')
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_table('users')