|
| 1 | +"""Add tables for custom dashboards |
| 2 | +
|
| 3 | +Revision ID: e5d79b8c4a55 |
| 4 | +Revises: d6c49c5435c2 |
| 5 | +Create Date: 2025-11-10 10:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | + |
| 12 | +from app.alembic.alembic_utils import _has_table |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision = 'e5d79b8c4a55' |
| 16 | +down_revision = 'd5a720d1b99b' |
| 17 | +branch_labels = None |
| 18 | +depends_on = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade(): |
| 22 | + if not _has_table('custom_dashboard'): |
| 23 | + op.create_table( |
| 24 | + 'custom_dashboard', |
| 25 | + sa.Column('id', sa.Integer, primary_key=True), |
| 26 | + sa.Column('dashboard_uuid', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False, unique=True), |
| 27 | + sa.Column('name', sa.String(length=255), nullable=False), |
| 28 | + sa.Column('description', sa.Text, nullable=True), |
| 29 | + sa.Column('owner_id', sa.Integer, sa.ForeignKey('user.id'), nullable=False), |
| 30 | + sa.Column('is_shared', sa.Boolean, nullable=False, server_default=sa.text('false')), |
| 31 | + sa.Column('definition', postgresql.JSONB, nullable=True), |
| 32 | + sa.Column('created_at', sa.DateTime, server_default=sa.func.now(), nullable=False), |
| 33 | + sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False) |
| 34 | + ) |
| 35 | + |
| 36 | + if not _has_table('custom_dashboard_widget'): |
| 37 | + op.create_table( |
| 38 | + 'custom_dashboard_widget', |
| 39 | + sa.Column('id', sa.Integer, primary_key=True), |
| 40 | + sa.Column('widget_uuid', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False, unique=True), |
| 41 | + sa.Column('dashboard_id', sa.Integer, sa.ForeignKey('custom_dashboard.id', ondelete='CASCADE'), nullable=False), |
| 42 | + sa.Column('name', sa.String(length=255), nullable=False), |
| 43 | + sa.Column('chart_type', sa.String(length=64), nullable=False), |
| 44 | + sa.Column('definition', postgresql.JSONB, nullable=False), |
| 45 | + sa.Column('position', sa.Integer, nullable=False, server_default=sa.text('0')), |
| 46 | + sa.Column('created_at', sa.DateTime, server_default=sa.func.now(), nullable=False), |
| 47 | + sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False) |
| 48 | + ) |
| 49 | + op.create_index('ix_custom_dashboard_widget_dashboard_id', 'custom_dashboard_widget', ['dashboard_id']) |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +def downgrade(): |
| 54 | + if _has_table('custom_dashboard_widget'): |
| 55 | + op.drop_index('ix_custom_dashboard_widget_dashboard_id', table_name='custom_dashboard_widget') |
| 56 | + op.drop_table('custom_dashboard_widget') |
| 57 | + |
| 58 | + if _has_table('custom_dashboard'): |
| 59 | + op.drop_table('custom_dashboard') |
0 commit comments