homelab_automation/alembic/versions/0017_add_favorites_tables.py
Bruno Charest 661d005fc7
Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
Add favorites feature with toggle filters, group management, color/icon pickers, dashboard widget, and star buttons across containers/docker sections with persistent storage and real-time UI updates
2025-12-23 14:56:31 -05:00

108 lines
4.9 KiB
Python

"""Add favorites tables (favorite_groups, favorite_containers)
Revision ID: 0017_add_favorites_tables
Revises: 0016
Create Date: 2025-12-22
"""
from __future__ import annotations
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
# revision identifiers, used by Alembic.
revision: str = '0017_add_favorites_tables'
down_revision: Union[str, None] = '0016'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
bind = op.get_bind()
insp = inspect(bind)
def _table_exists(name: str) -> bool:
try:
return insp.has_table(name)
except Exception:
return name in insp.get_table_names()
def _index_exists(table: str, index: str) -> bool:
try:
return any(i.get('name') == index for i in insp.get_indexes(table))
except Exception:
return False
if not _table_exists('favorite_groups'):
op.create_table(
'favorite_groups',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('sort_order', sa.Integer(), nullable=False, server_default=sa.text('0')),
sa.Column('color', sa.String(length=20), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
)
if _table_exists('favorite_groups'):
if not _index_exists('favorite_groups', 'ix_favorite_groups_user_id'):
op.create_index('ix_favorite_groups_user_id', 'favorite_groups', ['user_id'])
if not _index_exists('favorite_groups', 'ix_favorite_groups_sort_order'):
op.create_index('ix_favorite_groups_sort_order', 'favorite_groups', ['user_id', 'sort_order'])
if not _index_exists('favorite_groups', 'uq_favorite_groups_user_name'):
op.create_index(
'uq_favorite_groups_user_name',
'favorite_groups',
['user_id', 'name'],
unique=True,
)
if not _table_exists('favorite_containers'):
op.create_table(
'favorite_containers',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('docker_container_id', sa.Integer(), nullable=False),
sa.Column('group_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['docker_container_id'], ['docker_containers.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['group_id'], ['favorite_groups.id'], ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id'),
)
if _table_exists('favorite_containers'):
if not _index_exists('favorite_containers', 'ix_favorite_containers_user_id'):
op.create_index('ix_favorite_containers_user_id', 'favorite_containers', ['user_id'])
if not _index_exists('favorite_containers', 'ix_favorite_containers_docker_container_id'):
op.create_index('ix_favorite_containers_docker_container_id', 'favorite_containers', ['docker_container_id'])
if not _index_exists('favorite_containers', 'ix_favorite_containers_group_id'):
op.create_index('ix_favorite_containers_group_id', 'favorite_containers', ['group_id'])
if not _index_exists('favorite_containers', 'uq_favorite_containers_user_docker_container'):
op.create_index(
'uq_favorite_containers_user_docker_container',
'favorite_containers',
['user_id', 'docker_container_id'],
unique=True,
)
def downgrade() -> None:
op.drop_index('uq_favorite_containers_user_docker_container', table_name='favorite_containers')
op.drop_index('ix_favorite_containers_group_id', table_name='favorite_containers')
op.drop_index('ix_favorite_containers_docker_container_id', table_name='favorite_containers')
op.drop_index('ix_favorite_containers_user_id', table_name='favorite_containers')
op.drop_table('favorite_containers')
op.drop_index('uq_favorite_groups_user_name', table_name='favorite_groups')
op.drop_index('ix_favorite_groups_sort_order', table_name='favorite_groups')
op.drop_index('ix_favorite_groups_user_id', table_name='favorite_groups')
op.drop_table('favorite_groups')