310 lines
16 KiB
Python
310 lines
16 KiB
Python
"""
|
|||
|
|
Copyright 2024-2026 ReplyPier
|
||
|
|
|
||
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
you may not use this file except in compliance with the License.
|
||
|
|
You may obtain a copy of the License at
|
||
|
|
|
||
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
|
||
|
|
Unless required by applicable law or agreed to in writing, software
|
||
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
See the License for the specific language governing permissions and
|
||
|
|
limitations under the License.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from typing import Sequence, Union
|
||
|
|
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
|
||
|
|
|
||
|
|
# revision identifiers, used by Alembic.
|
||
|
|
revision: str = 'b365b8447598'
|
||
|
|
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('organizations',
|
||
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||
|
|
sa.Column('domain', sa.String(length=100), nullable=False),
|
||
|
|
sa.Column('timezone', sa.String(length=50), nullable=False),
|
||
|
|
sa.Column('business_hours', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('settings', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('domain')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_organizations_id'), 'organizations', ['id'], unique=False)
|
||
|
|
op.create_table('permissions',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(), nullable=False),
|
||
|
|
sa.Column('description', sa.String(), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('name')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_permissions_id'), 'permissions', ['id'], unique=False)
|
||
|
|
op.create_table('agents',
|
||
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||
|
|
sa.Column('display_name', sa.String(length=100), nullable=True),
|
||
|
|
sa.Column('description', sa.Text(), nullable=True),
|
||
|
|
sa.Column('agent_type', sa.Enum('CUSTOMER_SUPPORT', 'SALES', 'TECH_SUPPORT', 'GENERAL', 'CUSTOM', name='agenttype'), nullable=False),
|
||
|
|
sa.Column('instructions', sa.Text(), nullable=False),
|
||
|
|
sa.Column('tools', sa.Text(), nullable=True),
|
||
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('is_default', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('transfer_to_human', sa.Boolean(), nullable=False),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_table('ai_configs',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('model_type', sa.Enum('OPENAI', 'ANTHROPIC', 'DEEPSEEK', 'GOOGLE', 'GOOGLEVERTEX', 'GROQ', 'MISTRAL', 'HUGGINGFACE', 'OLLAMA', 'XAI', name='aimodeltype'), nullable=False),
|
||
|
|
sa.Column('model_name', sa.String(), nullable=False),
|
||
|
|
sa.Column('encrypted_api_key', sa.String(), nullable=False),
|
||
|
|
sa.Column('settings', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_ai_configs_id'), 'ai_configs', ['id'], unique=False)
|
||
|
|
op.create_table('customers',
|
||
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('email', sa.String(), nullable=False),
|
||
|
|
sa.Column('full_name', sa.String(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('email', 'organization_id', name='uix_customer_email_org')
|
||
|
|
)
|
||
|
|
op.create_table('groups',
|
||
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(), nullable=False),
|
||
|
|
sa.Column('description', sa.String(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_table('knowledge',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('source', sa.String(), nullable=False),
|
||
|
|
sa.Column('source_type', sa.Enum('DATABASE', 'FILE', 'API', 'WEBSITE', 'CUSTOM', name='sourcetype'), nullable=False),
|
||
|
|
sa.Column('schema', sa.String(), nullable=True),
|
||
|
|
sa.Column('table_name', sa.String(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_knowledge_id'), 'knowledge', ['id'], unique=False)
|
||
|
|
op.create_table('roles',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(), nullable=False),
|
||
|
|
sa.Column('description', sa.String(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('is_default', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('name')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_roles_id'), 'roles', ['id'], unique=False)
|
||
|
|
op.create_table('agent_customizations',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('photo_url', sa.String(), nullable=True),
|
||
|
|
sa.Column('chat_background_color', sa.String(), nullable=True),
|
||
|
|
sa.Column('chat_bubble_color', sa.String(), nullable=True),
|
||
|
|
sa.Column('chat_text_color', sa.String(), nullable=True),
|
||
|
|
sa.Column('icon_url', sa.String(), nullable=True),
|
||
|
|
sa.Column('icon_color', sa.String(), nullable=True),
|
||
|
|
sa.Column('accent_color', sa.String(), nullable=True),
|
||
|
|
sa.Column('font_family', sa.String(), nullable=True),
|
||
|
|
sa.Column('custom_css', sa.Text(), nullable=True),
|
||
|
|
sa.Column('customization_metadata', sa.JSON(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='CASCADE'),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_agent_customizations_id'), 'agent_customizations', ['id'], unique=False)
|
||
|
|
op.create_table('agent_usergroup',
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('group_id', sa.UUID(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='CASCADE'),
|
||
|
|
sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ondelete='CASCADE')
|
||
|
|
)
|
||
|
|
op.create_table('knowledge_to_agents',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('knowledge_id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='CASCADE'),
|
||
|
|
sa.ForeignKeyConstraint(['knowledge_id'], ['knowledge.id'], ondelete='CASCADE'),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_knowledge_to_agents_id'), 'knowledge_to_agents', ['id'], unique=False)
|
||
|
|
op.create_table('role_permissions',
|
||
|
|
sa.Column('role_id', sa.Integer(), nullable=True),
|
||
|
|
sa.Column('permission_id', sa.Integer(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['permission_id'], ['permissions.id'], ),
|
||
|
|
sa.ForeignKeyConstraint(['role_id'], ['roles.id'], )
|
||
|
|
)
|
||
|
|
op.create_table('users',
|
||
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('email', sa.String(), nullable=True),
|
||
|
|
sa.Column('full_name', sa.String(), nullable=True),
|
||
|
|
sa.Column('hashed_password', sa.String(), nullable=True),
|
||
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('profile_pic', sa.String(), nullable=True),
|
||
|
|
sa.Column('is_online', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('last_seen', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('role_id', sa.Integer(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||
|
|
sa.Column('fcm_token_web', sa.String(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
||
|
|
op.create_table('widgets',
|
||
|
|
sa.Column('id', sa.String(), nullable=False),
|
||
|
|
sa.Column('name', sa.String(), nullable=False),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_table('knowledge_queue',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('user_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('source_type', sa.String(), nullable=False),
|
||
|
|
sa.Column('source', sa.String(), nullable=False),
|
||
|
|
sa.Column('status', sa.String(), nullable=True),
|
||
|
|
sa.Column('error', sa.String(), nullable=True),
|
||
|
|
sa.Column('queue_metadata', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_knowledge_queue_id'), 'knowledge_queue', ['id'], unique=False)
|
||
|
|
op.create_table('notifications',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('user_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('type', sa.Enum('KNOWLEDGE_PROCESSED', 'KNOWLEDGE_FAILED', 'SYSTEM', 'CHAT', name='notificationtype'), nullable=False),
|
||
|
|
sa.Column('title', sa.String(), nullable=False),
|
||
|
|
sa.Column('message', sa.String(), nullable=False),
|
||
|
|
sa.Column('notification_metadata', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('is_read', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_notifications_id'), 'notifications', ['id'], unique=False)
|
||
|
|
op.create_table('session_to_agents',
|
||
|
|
sa.Column('session_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('user_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('customer_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('group_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('status', sa.Enum('OPEN', 'CLOSED', 'TRANSFERRED', name='sessionstatus'), nullable=False),
|
||
|
|
sa.Column('assigned_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('closed_at', sa.DateTime(timezone=True), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('transfer_reason', sa.String(), nullable=True),
|
||
|
|
sa.Column('transfer_description', sa.String(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ondelete='CASCADE'),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
|
||
|
|
sa.PrimaryKeyConstraint('session_id')
|
||
|
|
)
|
||
|
|
op.create_table('user_groups',
|
||
|
|
sa.Column('user_id', sa.UUID(), nullable=False),
|
||
|
|
sa.Column('group_id', sa.UUID(), nullable=False),
|
||
|
|
sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('user_id', 'group_id')
|
||
|
|
)
|
||
|
|
op.create_table('chat_history',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('organization_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('user_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('customer_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('agent_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('session_id', sa.UUID(), nullable=True),
|
||
|
|
sa.Column('message', sa.String(), nullable=False),
|
||
|
|
sa.Column('message_type', sa.String(), nullable=False),
|
||
|
|
sa.Column('attributes', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ondelete='SET NULL'),
|
||
|
|
sa.ForeignKeyConstraint(['session_id'], ['session_to_agents.session_id'], ),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_chat_history_id'), 'chat_history', ['id'], unique=False)
|
||
|
|
# ### end Alembic commands ###
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
||
|
|
op.drop_index(op.f('ix_chat_history_id'), table_name='chat_history')
|
||
|
|
op.drop_table('chat_history')
|
||
|
|
op.drop_table('user_groups')
|
||
|
|
op.drop_table('session_to_agents')
|
||
|
|
op.drop_index(op.f('ix_notifications_id'), table_name='notifications')
|
||
|
|
op.drop_table('notifications')
|
||
|
|
op.drop_index(op.f('ix_knowledge_queue_id'), table_name='knowledge_queue')
|
||
|
|
op.drop_table('knowledge_queue')
|
||
|
|
op.drop_table('widgets')
|
||
|
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
||
|
|
op.drop_table('users')
|
||
|
|
op.drop_table('role_permissions')
|
||
|
|
op.drop_index(op.f('ix_knowledge_to_agents_id'), table_name='knowledge_to_agents')
|
||
|
|
op.drop_table('knowledge_to_agents')
|
||
|
|
op.drop_table('agent_usergroup')
|
||
|
|
op.drop_index(op.f('ix_agent_customizations_id'), table_name='agent_customizations')
|
||
|
|
op.drop_table('agent_customizations')
|
||
|
|
op.drop_index(op.f('ix_roles_id'), table_name='roles')
|
||
|
|
op.drop_table('roles')
|
||
|
|
op.drop_index(op.f('ix_knowledge_id'), table_name='knowledge')
|
||
|
|
op.drop_table('knowledge')
|
||
|
|
op.drop_table('groups')
|
||
|
|
op.drop_table('customers')
|
||
|
|
op.drop_index(op.f('ix_ai_configs_id'), table_name='ai_configs')
|
||
|
|
op.drop_table('ai_configs')
|
||
|
|
op.drop_table('agents')
|
||
|
|
op.drop_index(op.f('ix_permissions_id'), table_name='permissions')
|
||
|
|
op.drop_table('permissions')
|
||
|
|
op.drop_index(op.f('ix_organizations_id'), table_name='organizations')
|
||
|
|
op.drop_table('organizations')
|
||
|
|
# ### end Alembic commands ###
|