Fix Alembic issue
This commit is contained in:
@@ -7,6 +7,7 @@ Import all models to ensure they are registered with SQLAlchemy Base.
|
||||
# Import all models to register them with the Base metadata
|
||||
from .notifications import (
|
||||
Notification,
|
||||
NotificationTemplate,
|
||||
NotificationType,
|
||||
NotificationStatus,
|
||||
NotificationPriority,
|
||||
@@ -21,6 +22,7 @@ from .templates import (
|
||||
# List all models for easier access
|
||||
__all__ = [
|
||||
"Notification",
|
||||
"NotificationTemplate",
|
||||
"NotificationType",
|
||||
"NotificationStatus",
|
||||
"NotificationPriority",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Alembic environment configuration for notification service"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
@@ -25,7 +24,7 @@ try:
|
||||
from shared.database.base import Base
|
||||
|
||||
# Import all models to ensure they are registered with Base.metadata
|
||||
from app.models import * # Import all models
|
||||
from app.models import * # noqa: F401, F403
|
||||
|
||||
except ImportError as e:
|
||||
print(f"Import error in migrations env.py: {e}")
|
||||
@@ -35,12 +34,19 @@ except ImportError as e:
|
||||
# this is the Alembic Config object
|
||||
config = context.config
|
||||
|
||||
# Set database URL from environment variables or settings
|
||||
# Try service-specific DATABASE_URL first, then fall back to generic
|
||||
database_url = os.getenv('NOTIFICATION_DATABASE_URL') or os.getenv('DATABASE_URL')
|
||||
# Determine service name from file path
|
||||
service_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
|
||||
service_name_upper = service_name.upper().replace('-', '_')
|
||||
|
||||
# Set database URL from environment variables with multiple fallback strategies
|
||||
database_url = (
|
||||
os.getenv(f'{service_name_upper}_DATABASE_URL') or # Service-specific
|
||||
os.getenv('DATABASE_URL') # Generic fallback
|
||||
)
|
||||
|
||||
# If DATABASE_URL is not set, construct from individual components
|
||||
if not database_url:
|
||||
# Try generic PostgreSQL environment variables first
|
||||
postgres_host = os.getenv('POSTGRES_HOST')
|
||||
postgres_port = os.getenv('POSTGRES_PORT', '5432')
|
||||
postgres_db = os.getenv('POSTGRES_DB')
|
||||
@@ -50,11 +56,28 @@ if not database_url:
|
||||
if all([postgres_host, postgres_db, postgres_user, postgres_password]):
|
||||
database_url = f"postgresql+asyncpg://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
|
||||
else:
|
||||
# Fallback to settings
|
||||
database_url = getattr(settings, 'DATABASE_URL', None)
|
||||
# Try service-specific environment variables
|
||||
db_host = os.getenv(f'{service_name_upper}_DB_HOST', f'{service_name}-db-service')
|
||||
db_port = os.getenv(f'{service_name_upper}_DB_PORT', '5432')
|
||||
db_name = os.getenv(f'{service_name_upper}_DB_NAME', f'{service_name.replace("-", "_")}_db')
|
||||
db_user = os.getenv(f'{service_name_upper}_DB_USER', f'{service_name.replace("-", "_")}_user')
|
||||
db_password = os.getenv(f'{service_name_upper}_DB_PASSWORD')
|
||||
|
||||
if database_url:
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
if db_password:
|
||||
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
|
||||
else:
|
||||
# Final fallback: try to get from settings object
|
||||
try:
|
||||
database_url = getattr(settings, 'DATABASE_URL', None)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not database_url:
|
||||
error_msg = f"ERROR: No database URL configured for {service_name} service"
|
||||
print(error_msg)
|
||||
raise Exception(error_msg)
|
||||
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
|
||||
# Interpret the config file for Python logging
|
||||
if config.config_file_name is not None:
|
||||
@@ -63,6 +86,7 @@ if config.config_file_name is not None:
|
||||
# Set target metadata
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
@@ -78,7 +102,9 @@ def run_migrations_offline() -> None:
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
"""Execute migrations with the given connection."""
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
@@ -89,8 +115,9 @@ def do_run_migrations(connection: Connection) -> None:
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
"""Run migrations in 'online' mode with async support."""
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
@@ -102,10 +129,12 @@ async def run_async_migrations() -> None:
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
|
||||
@@ -1,245 +0,0 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,184 @@
|
||||
"""initial_schema_20251001_1119
|
||||
|
||||
Revision ID: 51fa7b09a051
|
||||
Revises:
|
||||
Create Date: 2025-10-01 11:19:26.135733+02:00
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '51fa7b09a051'
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
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(length=100), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('subject', sa.String(length=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(length=255), nullable=True),
|
||||
sa.Column('from_name', sa.String(length=255), nullable=True),
|
||||
sa.Column('reply_to', sa.String(length=255), nullable=True),
|
||||
sa.Column('variables', sa.JSON(), nullable=True),
|
||||
sa.Column('sample_data', sa.JSON(), nullable=True),
|
||||
sa.Column('language', sa.String(length=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(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), 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_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(length=50), nullable=True),
|
||||
sa.Column('provider_message_id', sa.String(length=255), nullable=True),
|
||||
sa.Column('provider_response', sa.JSON(), nullable=True),
|
||||
sa.Column('attempted_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('response_time_ms', sa.Integer(), nullable=True),
|
||||
sa.Column('error_code', sa.String(length=50), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('log_metadata', sa.JSON(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_notification_logs_notification_id'), 'notification_logs', ['notification_id'], unique=False)
|
||||
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(length=5), nullable=True),
|
||||
sa.Column('quiet_hours_end', sa.String(length=5), nullable=True),
|
||||
sa.Column('timezone', sa.String(length=50), nullable=True),
|
||||
sa.Column('digest_frequency', sa.String(length=20), nullable=True),
|
||||
sa.Column('max_emails_per_day', sa.Integer(), nullable=True),
|
||||
sa.Column('language', sa.String(length=2), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_notification_preferences_tenant_id'), 'notification_preferences', ['tenant_id'], unique=False)
|
||||
op.create_index(op.f('ix_notification_preferences_user_id'), 'notification_preferences', ['user_id'], unique=True)
|
||||
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(length=100), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('category', sa.String(length=50), nullable=False),
|
||||
sa.Column('type', sa.Enum('EMAIL', 'WHATSAPP', 'PUSH', 'SMS', name='notificationtype'), nullable=False),
|
||||
sa.Column('subject_template', sa.String(length=255), nullable=True),
|
||||
sa.Column('body_template', sa.Text(), nullable=False),
|
||||
sa.Column('html_template', sa.Text(), nullable=True),
|
||||
sa.Column('language', sa.String(length=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', sa.JSON(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), 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_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(length=255), nullable=True),
|
||||
sa.Column('message', sa.Text(), nullable=False),
|
||||
sa.Column('html_content', sa.Text(), nullable=True),
|
||||
sa.Column('template_id', sa.String(length=100), nullable=True),
|
||||
sa.Column('template_data', sa.JSON(), nullable=True),
|
||||
sa.Column('recipient_email', sa.String(length=255), nullable=True),
|
||||
sa.Column('recipient_phone', sa.String(length=20), nullable=True),
|
||||
sa.Column('delivery_channel', sa.String(length=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', sa.JSON(), 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(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_notifications_created_at'), 'notifications', ['created_at'], unique=False)
|
||||
op.create_index(op.f('ix_notifications_status'), 'notifications', ['status'], unique=False)
|
||||
op.create_index(op.f('ix_notifications_tenant_id'), 'notifications', ['tenant_id'], unique=False)
|
||||
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(length=100), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('whatsapp_template_name', sa.String(length=255), nullable=False),
|
||||
sa.Column('whatsapp_template_id', sa.String(length=255), nullable=True),
|
||||
sa.Column('language_code', sa.String(length=10), nullable=True),
|
||||
sa.Column('header_text', sa.String(length=60), nullable=True),
|
||||
sa.Column('body_text', sa.Text(), nullable=False),
|
||||
sa.Column('footer_text', sa.String(length=60), nullable=True),
|
||||
sa.Column('parameter_count', sa.Integer(), nullable=True),
|
||||
sa.Column('parameters', sa.JSON(), nullable=True),
|
||||
sa.Column('approval_status', sa.String(length=20), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), 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)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_whatsapp_templates_tenant_id'), table_name='whatsapp_templates')
|
||||
op.drop_table('whatsapp_templates')
|
||||
op.drop_index(op.f('ix_notifications_tenant_id'), table_name='notifications')
|
||||
op.drop_index(op.f('ix_notifications_status'), table_name='notifications')
|
||||
op.drop_index(op.f('ix_notifications_created_at'), table_name='notifications')
|
||||
op.drop_table('notifications')
|
||||
op.drop_index(op.f('ix_notification_templates_tenant_id'), table_name='notification_templates')
|
||||
op.drop_table('notification_templates')
|
||||
op.drop_index(op.f('ix_notification_preferences_user_id'), table_name='notification_preferences')
|
||||
op.drop_index(op.f('ix_notification_preferences_tenant_id'), table_name='notification_preferences')
|
||||
op.drop_table('notification_preferences')
|
||||
op.drop_index(op.f('ix_notification_logs_notification_id'), table_name='notification_logs')
|
||||
op.drop_table('notification_logs')
|
||||
op.drop_index(op.f('ix_email_templates_tenant_id'), table_name='email_templates')
|
||||
op.drop_table('email_templates')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user