homelab_automation/alembic/versions/0011_add_docker_management_tables.py
Bruno Charest 68a9b0f390
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
Remove Node.js cache files containing npm vulnerability data for vitest and vite packages
2025-12-15 20:36:06 -05:00

120 lines
5.5 KiB
Python

"""Add Docker management tables
Revision ID: 0011
Revises: 0010
Create Date: 2025-01-15
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '0011_add_docker_management_tables'
down_revision: Union[str, None] = '0010_remove_logs_foreign_keys'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Add Docker-related columns to hosts table
op.add_column('hosts', sa.Column('docker_enabled', sa.Boolean(), nullable=False, server_default=sa.text('0')))
op.add_column('hosts', sa.Column('docker_version', sa.String(50), nullable=True))
op.add_column('hosts', sa.Column('docker_status', sa.String(20), nullable=True))
op.add_column('hosts', sa.Column('docker_last_collect_at', sa.DateTime(timezone=True), nullable=True))
# Create docker_containers table
op.create_table(
'docker_containers',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('host_id', sa.String(), nullable=False),
sa.Column('container_id', sa.String(64), nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('image', sa.String(255), nullable=True),
sa.Column('state', sa.String(20), nullable=False, server_default='unknown'),
sa.Column('status', sa.String(255), nullable=True),
sa.Column('health', sa.String(20), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('ports', sa.JSON(), nullable=True),
sa.Column('labels', sa.JSON(), nullable=True),
sa.Column('compose_project', sa.String(255), nullable=True),
sa.Column('last_update_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('ix_docker_containers_host_id', 'docker_containers', ['host_id'])
op.create_index('ix_docker_containers_container_id', 'docker_containers', ['container_id'])
# Create docker_images table
op.create_table(
'docker_images',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('host_id', sa.String(), nullable=False),
sa.Column('image_id', sa.String(64), nullable=False),
sa.Column('repo_tags', sa.JSON(), nullable=True),
sa.Column('size', sa.BigInteger(), nullable=True),
sa.Column('created', sa.DateTime(timezone=True), nullable=True),
sa.Column('last_update_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('ix_docker_images_host_id', 'docker_images', ['host_id'])
# Create docker_volumes table
op.create_table(
'docker_volumes',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('host_id', sa.String(), nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('driver', sa.String(50), nullable=True),
sa.Column('mountpoint', sa.Text(), nullable=True),
sa.Column('scope', sa.String(20), nullable=True),
sa.Column('last_update_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('ix_docker_volumes_host_id', 'docker_volumes', ['host_id'])
# Create docker_alerts table
op.create_table(
'docker_alerts',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('host_id', sa.String(), nullable=False),
sa.Column('container_name', sa.String(255), nullable=False),
sa.Column('severity', sa.String(20), nullable=False, server_default='warning'),
sa.Column('state', sa.String(20), nullable=False, server_default='open'),
sa.Column('message', sa.Text(), nullable=True),
sa.Column('opened_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column('closed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('acknowledged_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('acknowledged_by', sa.String(100), nullable=True),
sa.Column('last_notified_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_docker_alerts_state_host', 'docker_alerts', ['state', 'host_id'])
def downgrade() -> None:
# Drop Docker tables
op.drop_index('idx_docker_alerts_state_host', 'docker_alerts')
op.drop_table('docker_alerts')
op.drop_index('ix_docker_volumes_host_id', 'docker_volumes')
op.drop_table('docker_volumes')
op.drop_index('ix_docker_images_host_id', 'docker_images')
op.drop_table('docker_images')
op.drop_index('ix_docker_containers_container_id', 'docker_containers')
op.drop_index('ix_docker_containers_host_id', 'docker_containers')
op.drop_table('docker_containers')
# Remove Docker columns from hosts
op.drop_column('hosts', 'docker_last_collect_at')
op.drop_column('hosts', 'docker_status')
op.drop_column('hosts', 'docker_version')
op.drop_column('hosts', 'docker_enabled')