musicmuster/migrations/versions/8e06d465923a_have_id_field_reflect_table_name.py
2025-08-18 20:02:52 +01:00

87 lines
2.6 KiB
Python

"""Have id field reflect table name
Revision ID: 8e06d465923a
Revises: 6d36cde8dea0
Create Date: 2025-04-22 13:23:18.813024
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from dataclasses import dataclass
@dataclass
class TableInfo:
table: str
old: str
new: str
data = [
TableInfo("notecolours", "id", "notecolour_id"),
TableInfo("playdates", "id", "playdate_id"),
TableInfo("playlists", "id", "playlist_id"),
TableInfo("playlist_rows", "id", "playlistrow_id"),
TableInfo("queries", "id", "query_id"),
TableInfo("settings", "id", "setting_id"),
TableInfo("tracks", "id", "track_id"),
]
# revision identifiers, used by Alembic.
revision = '8e06d465923a'
down_revision = '6d36cde8dea0'
branch_labels = None
depends_on = None
def upgrade(engine_name: str) -> None:
globals()["upgrade_%s" % engine_name]()
def downgrade(engine_name: str) -> None:
globals()["downgrade_%s" % engine_name]()
def upgrade_() -> None:
# Drop foreign key constraints
op.drop_constraint('fk_playdates_track_id_tracks', 'playdates', type_='foreignkey')
op.drop_constraint('fk_playlist_rows_track_id_tracks', 'playlist_rows', type_='foreignkey')
for record in data:
op.alter_column(
record.table,
record.old,
new_column_name=record.new,
existing_type=sa.Integer(), # Specify the existing column type
existing_nullable=False # If the column is NOT NULL, specify that too
)
# Recreate the foreign key constraints
op.create_foreign_key('fk_playdates_track_id_tracks', 'playdates', 'tracks', ['track_id'], ['track_id'])
op.create_foreign_key('fk_playlist_rows_track_id_tracks', 'playlist_rows', 'tracks', ['track_id'], ['track_id'])
# ### end Alembic commands ###
def downgrade_() -> None:
# Drop foreign key constraints
op.drop_constraint('fk_playdates_track_id_tracks', 'playdates', type_='foreignkey')
op.drop_constraint('fk_playlist_rows_track_id_tracks', 'playlist_rows', type_='foreignkey')
for record in data:
op.alter_column(
record.table,
record.new,
new_column_name=record.old,
existing_type=sa.Integer(), # Specify the existing column type
existing_nullable=False # If the column is NOT NULL, specify that too
)
# Recreate the foreign key constraints
op.create_foreign_key('fk_playdates_track_id_tracks', 'playdates', 'tracks', ['track_id'], ['track_id'])
op.create_foreign_key('fk_playlist_rows_track_id_tracks', 'playlist_rows', 'tracks', ['track_id'], ['track_id'])
# ### end Alembic commands ###