"""Initial schema for notification service Revision ID: 000001 Revises: Create Date: 2025-09-30 18:00:00.0000 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision = '000001' down_revision = None branch_labels = None depends_on = None def upgrade() -> None: # Create notifications table op.create_table('notifications', sa.Column('id', sa.UUID(), nullable=False), sa.Column('tenant_id', sa.UUID(), nullable=False), sa.Column('sender_id', sa.UUID(), nullable=False), sa.Column('recipient_id', sa.UUID(), nullable=True), sa.Column('type', sa.Enum('EMAIL', 'WHATSAPP', 'PUSH', 'SMS', name='notificationtype'), nullable=False), sa.Column('status', sa.Enum('PENDING', 'SENT', 'DELIVERED', 'FAILED', 'CANCELLED', name='notificationstatus'), nullable=True), sa.Column('priority', sa.Enum('LOW', 'NORMAL', 'HIGH', 'URGENT', name='notificationpriority'), nullable=True), sa.Column('subject', sa.String(255), nullable=True), sa.Column('message', sa.Text(), nullable=False), sa.Column('html_content', sa.Text(), nullable=True), sa.Column('template_id', sa.String(100), nullable=True), sa.Column('template_data', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('recipient_email', sa.String(255), nullable=True), sa.Column('recipient_phone', sa.String(20), nullable=True), sa.Column('delivery_channel', sa.String(50), nullable=True), sa.Column('scheduled_at', sa.DateTime(), nullable=True), sa.Column('sent_at', sa.DateTime(), nullable=True), sa.Column('delivered_at', sa.DateTime(), nullable=True), sa.Column('log_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('error_message', sa.Text(), nullable=True), sa.Column('retry_count', sa.Integer(), nullable=True), sa.Column('max_retries', sa.Integer(), nullable=True), sa.Column('broadcast', sa.Boolean(), nullable=True), sa.Column('read', sa.Boolean(), nullable=True), sa.Column('read_at', sa.DateTime(), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_notifications_tenant_id'), 'notifications', ['tenant_id'], unique=False) op.create_index(op.f('ix_notifications_sender_id'), 'notifications', ['sender_id'], unique=False) op.create_index(op.f('ix_notifications_recipient_id'), 'notifications', ['recipient_id'], unique=False) op.create_index(op.f('ix_notifications_type'), 'notifications', ['type'], unique=False) op.create_index(op.f('ix_notifications_status'), 'notifications', ['status'], unique=False) op.create_index(op.f('ix_notifications_priority'), 'notifications', ['priority'], unique=False) op.create_index(op.f('ix_notifications_created_at'), 'notifications', ['created_at'], unique=False) op.create_index(op.f('ix_notifications_scheduled_at'), 'notifications', ['scheduled_at'], unique=False) op.create_index(op.f('ix_notifications_sent_at'), 'notifications', ['sent_at'], unique=False) # Create notification_templates table op.create_table('notification_templates', sa.Column('id', sa.UUID(), nullable=False), sa.Column('tenant_id', sa.UUID(), nullable=True), sa.Column('template_key', sa.String(100), nullable=False), sa.Column('name', sa.String(255), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('category', sa.String(50), nullable=False), sa.Column('type', sa.Enum('EMAIL', 'WHATSAPP', 'PUSH', 'SMS', name='notificationtype'), nullable=False), sa.Column('subject_template', sa.String(255), nullable=True), sa.Column('body_template', sa.Text(), nullable=False), sa.Column('html_template', sa.Text(), nullable=True), sa.Column('language', sa.String(2), nullable=True), sa.Column('is_active', sa.Boolean(), nullable=True), sa.Column('is_system', sa.Boolean(), nullable=True), sa.Column('default_priority', sa.Enum('LOW', 'NORMAL', 'HIGH', 'URGENT', name='notificationpriority'), nullable=True), sa.Column('required_variables', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('template_key') ) op.create_index(op.f('ix_notification_templates_tenant_id'), 'notification_templates', ['tenant_id'], unique=False) op.create_index(op.f('ix_notification_templates_name'), 'notification_templates', ['name'], unique=False) op.create_index(op.f('ix_notification_templates_category'), 'notification_templates', ['category'], unique=False) op.create_index(op.f('ix_notification_templates_type'), 'notification_templates', ['type'], unique=False) # Create notification_preferences table op.create_table('notification_preferences', sa.Column('id', sa.UUID(), nullable=False), sa.Column('user_id', sa.UUID(), nullable=False), sa.Column('tenant_id', sa.UUID(), nullable=False), sa.Column('email_enabled', sa.Boolean(), nullable=True), sa.Column('email_alerts', sa.Boolean(), nullable=True), sa.Column('email_marketing', sa.Boolean(), nullable=True), sa.Column('email_reports', sa.Boolean(), nullable=True), sa.Column('whatsapp_enabled', sa.Boolean(), nullable=True), sa.Column('whatsapp_alerts', sa.Boolean(), nullable=True), sa.Column('whatsapp_reports', sa.Boolean(), nullable=True), sa.Column('push_enabled', sa.Boolean(), nullable=True), sa.Column('push_alerts', sa.Boolean(), nullable=True), sa.Column('push_reports', sa.Boolean(), nullable=True), sa.Column('quiet_hours_start', sa.String(5), nullable=True), sa.Column('quiet_hours_end', sa.String(5), nullable=True), sa.Column('timezone', sa.String(50), nullable=True), sa.Column('digest_frequency', sa.String(20), nullable=True), sa.Column('max_emails_per_day', sa.Integer(), nullable=True), sa.Column('language', sa.String(2), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('user_id') ) op.create_index(op.f('ix_notification_preferences_user_id'), 'notification_preferences', ['user_id'], unique=False) op.create_index(op.f('ix_notification_preferences_tenant_id'), 'notification_preferences', ['tenant_id'], unique=False) # Create notification_logs table op.create_table('notification_logs', sa.Column('id', sa.UUID(), nullable=False), sa.Column('notification_id', sa.UUID(), nullable=False), sa.Column('attempt_number', sa.Integer(), nullable=False), sa.Column('status', sa.Enum('PENDING', 'SENT', 'DELIVERED', 'FAILED', 'CANCELLED', name='notificationstatus'), nullable=False), sa.Column('provider', sa.String(50), nullable=True), sa.Column('provider_message_id', sa.String(255), nullable=True), sa.Column('provider_response', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('attempted_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('response_time_ms', sa.Integer(), nullable=True), sa.Column('error_code', sa.String(50), nullable=True), sa.Column('error_message', sa.Text(), nullable=True), sa.Column('log_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_notification_logs_notification_id'), 'notification_logs', ['notification_id'], unique=False) op.create_index(op.f('ix_notification_logs_attempted_at'), 'notification_logs', ['attempted_at'], unique=False) op.create_index(op.f('ix_notification_logs_provider'), 'notification_logs', ['provider'], unique=False) op.create_index(op.f('ix_notification_logs_status'), 'notification_logs', ['status'], unique=False) # Create email_templates table op.create_table('email_templates', sa.Column('id', sa.UUID(), nullable=False), sa.Column('tenant_id', sa.UUID(), nullable=True), sa.Column('template_key', sa.String(100), nullable=False), sa.Column('name', sa.String(255), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('subject', sa.String(255), nullable=False), sa.Column('html_body', sa.Text(), nullable=False), sa.Column('text_body', sa.Text(), nullable=True), sa.Column('from_email', sa.String(255), nullable=True), sa.Column('from_name', sa.String(255), nullable=True), sa.Column('reply_to', sa.String(255), nullable=True), sa.Column('variables', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('sample_data', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('language', sa.String(2), nullable=True), sa.Column('is_active', sa.Boolean(), nullable=True), sa.Column('is_system', sa.Boolean(), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('template_key') ) op.create_index(op.f('ix_email_templates_tenant_id'), 'email_templates', ['tenant_id'], unique=False) op.create_index(op.f('ix_email_templates_template_key'), 'email_templates', ['template_key'], unique=False) op.create_index(op.f('ix_email_templates_name'), 'email_templates', ['name'], unique=False) # Create whatsapp_templates table op.create_table('whatsapp_templates', sa.Column('id', sa.UUID(), nullable=False), sa.Column('tenant_id', sa.UUID(), nullable=True), sa.Column('template_key', sa.String(100), nullable=False), sa.Column('name', sa.String(255), nullable=False), sa.Column('whatsapp_template_name', sa.String(255), nullable=False), sa.Column('whatsapp_template_id', sa.String(255), nullable=True), sa.Column('language_code', sa.String(10), nullable=True), sa.Column('header_text', sa.String(60), nullable=True), sa.Column('body_text', sa.Text(), nullable=False), sa.Column('footer_text', sa.String(60), nullable=True), sa.Column('parameter_count', sa.Integer(), nullable=True), sa.Column('parameters', postgresql.JSON(astext_type=sa.Text()), nullable=True), sa.Column('approval_status', sa.String(20), nullable=True), sa.Column('is_active', sa.Boolean(), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('timezone(\'utc\', now())'), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('template_key') ) op.create_index(op.f('ix_whatsapp_templates_tenant_id'), 'whatsapp_templates', ['tenant_id'], unique=False) op.create_index(op.f('ix_whatsapp_templates_template_key'), 'whatsapp_templates', ['template_key'], unique=False) op.create_index(op.f('ix_whatsapp_templates_name'), 'whatsapp_templates', ['name'], unique=False) op.create_index(op.f('ix_whatsapp_templates_approval_status'), 'whatsapp_templates', ['approval_status'], unique=False) def downgrade() -> None: # Drop whatsapp_templates table op.drop_index(op.f('ix_whatsapp_templates_approval_status'), table_name='whatsapp_templates') op.drop_index(op.f('ix_whatsapp_templates_name'), table_name='whatsapp_templates') op.drop_index(op.f('ix_whatsapp_templates_template_key'), table_name='whatsapp_templates') op.drop_index(op.f('ix_whatsapp_templates_tenant_id'), table_name='whatsapp_templates') op.drop_table('whatsapp_templates') # Drop email_templates table op.drop_index(op.f('ix_email_templates_name'), table_name='email_templates') op.drop_index(op.f('ix_email_templates_template_key'), table_name='email_templates') op.drop_index(op.f('ix_email_templates_tenant_id'), table_name='email_templates') op.drop_table('email_templates') # Drop notification_logs table op.drop_index(op.f('ix_notification_logs_status'), table_name='notification_logs') op.drop_index(op.f('ix_notification_logs_provider'), table_name='notification_logs') op.drop_index(op.f('ix_notification_logs_attempted_at'), table_name='notification_logs') op.drop_index(op.f('ix_notification_logs_notification_id'), table_name='notification_logs') op.drop_table('notification_logs') # Drop notification_preferences table op.drop_index(op.f('ix_notification_preferences_tenant_id'), table_name='notification_preferences') op.drop_index(op.f('ix_notification_preferences_user_id'), table_name='notification_preferences') op.drop_table('notification_preferences') # Drop notification_templates table op.drop_index(op.f('ix_notification_templates_type'), table_name='notification_templates') op.drop_index(op.f('ix_notification_templates_category'), table_name='notification_templates') op.drop_index(op.f('ix_notification_templates_name'), table_name='notification_templates') op.drop_index(op.f('ix_notification_templates_tenant_id'), table_name='notification_templates') op.drop_table('notification_templates') # Drop notifications table op.drop_index(op.f('ix_notifications_sent_at'), table_name='notifications') op.drop_index(op.f('ix_notifications_scheduled_at'), table_name='notifications') op.drop_index(op.f('ix_notifications_created_at'), table_name='notifications') op.drop_index(op.f('ix_notifications_priority'), table_name='notifications') op.drop_index(op.f('ix_notifications_status'), table_name='notifications') op.drop_index(op.f('ix_notifications_type'), table_name='notifications') op.drop_index(op.f('ix_notifications_recipient_id'), table_name='notifications') op.drop_index(op.f('ix_notifications_sender_id'), table_name='notifications') op.drop_index(op.f('ix_notifications_tenant_id'), table_name='notifications') op.drop_table('notifications') # Drop enums notification_priority_enum = sa.Enum(name='notificationpriority') notification_priority_enum.drop(op.get_bind(), checkfirst=True) notification_status_enum = sa.Enum(name='notificationstatus') notification_status_enum.drop(op.get_bind(), checkfirst=True) notification_type_enum = sa.Enum(name='notificationtype') notification_type_enum.drop(op.get_bind(), checkfirst=True)