homelab_automation/alembic/versions/0019_add_container_customizations.py
Bruno Charest 8affa0f8b7
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
feat: Implement container customization and add Portainer installation/removal playbooks.
2025-12-27 11:02:24 -05:00

82 lines
3.5 KiB
Python

"""Add container_customizations table
Revision ID: 0019_add_container_customizations
Revises: 0018
Create Date: 2025-12-24
"""
from __future__ import annotations
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision: str = '0019_add_container_customizations'
down_revision: Union[str, None] = '0018'
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('container_customizations'):
op.create_table(
'container_customizations',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('host_id', sa.String(), nullable=False),
sa.Column('container_id', sa.String(length=64), nullable=False),
sa.Column('icon_key', sa.String(length=100), nullable=True),
sa.Column('icon_color', sa.String(length=20), nullable=True),
sa.Column('bg_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.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
)
if _table_exists('container_customizations'):
if not _index_exists('container_customizations', 'ix_container_customizations_user_id'):
op.create_index('ix_container_customizations_user_id', 'container_customizations', ['user_id'])
if not _index_exists('container_customizations', 'ix_container_customizations_host_id'):
op.create_index('ix_container_customizations_host_id', 'container_customizations', ['host_id'])
if not _index_exists('container_customizations', 'ix_container_customizations_host_container'):
op.create_index(
'ix_container_customizations_host_container',
'container_customizations',
['host_id', 'container_id'],
)
if not _index_exists('container_customizations', 'uq_container_customizations_user_host_container'):
op.create_index(
'uq_container_customizations_user_host_container',
'container_customizations',
['user_id', 'host_id', 'container_id'],
unique=True,
)
def downgrade() -> None:
op.drop_index('uq_container_customizations_user_host_container', table_name='container_customizations')
op.drop_index('ix_container_customizations_host_container', table_name='container_customizations')
op.drop_index('ix_container_customizations_host_id', table_name='container_customizations')
op.drop_index('ix_container_customizations_user_id', table_name='container_customizations')
op.drop_table('container_customizations')