feat: Initial commit with full deployment architecture and scripts
This commit is contained in:
commit
abb3efe114
96 files changed
+16789
No files matched your search
@@ -0,0 +1 @@
|
||||
Single-database configuration for Flask.
|
||||
@@ -0,0 +1,50 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
@@ -0,0 +1,113 @@
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine():
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine()
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engine
|
||||
|
||||
|
||||
def get_engine_url():
|
||||
try:
|
||||
return get_engine().url.render_as_string(hide_password=False).replace(
|
||||
'%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine().url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata():
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[None]
|
||||
return target_db.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url, target_metadata=get_metadata(), literal_binds=True
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
connectable = get_engine()
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=get_metadata(),
|
||||
**conf_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,34 @@
|
||||
"""add phone_number to user
|
||||
|
||||
Revision ID: 08f34c125a73
|
||||
Revises: 5b797d05f412
|
||||
Create Date: 2026-02-23 17:10:37.613505
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '08f34c125a73'
|
||||
down_revision = '5b797d05f412'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('phone_number', sa.String(length=20), nullable=True))
|
||||
batch_op.create_unique_constraint(None, ['phone_number'])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_constraint(None, type_='unique')
|
||||
batch_op.drop_column('phone_number')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,32 @@
|
||||
"""add_tokens_used_to_chatlog
|
||||
|
||||
Revision ID: 439e501ac52b
|
||||
Revises: 7797942bfa85
|
||||
Create Date: 2026-02-24 08:51:08.086113
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '439e501ac52b'
|
||||
down_revision = '7797942bfa85'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('tokens_used', sa.Integer(), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.drop_column('tokens_used')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Add default_agent_id to User
|
||||
|
||||
Revision ID: 48f1ca8fa73a
|
||||
Revises: 08f34c125a73
|
||||
Create Date: 2026-02-23 20:39:30.511482
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '48f1ca8fa73a'
|
||||
down_revision = '08f34c125a73'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('default_agent_id', sa.Integer(), nullable=True))
|
||||
batch_op.create_foreign_key(None, 'agents', ['default_agent_id'], ['id'])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_constraint(None, type_='foreignkey')
|
||||
batch_op.drop_column('default_agent_id')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Initial migration
|
||||
|
||||
Revision ID: 510db8a4f806
|
||||
Revises:
|
||||
Create Date: 2026-02-23 11:16:09.218508
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '510db8a4f806'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('agents', schema=None) as batch_op:
|
||||
batch_op.alter_column('type',
|
||||
existing_type=sa.VARCHAR(length=20),
|
||||
nullable=False,
|
||||
existing_server_default=sa.text("'api'::character varying"))
|
||||
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.alter_column('status',
|
||||
existing_type=sa.VARCHAR(length=20),
|
||||
nullable=False,
|
||||
existing_server_default=sa.text("'active'::character varying"))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.alter_column('status',
|
||||
existing_type=sa.VARCHAR(length=20),
|
||||
nullable=True,
|
||||
existing_server_default=sa.text("'active'::character varying"))
|
||||
|
||||
with op.batch_alter_table('agents', schema=None) as batch_op:
|
||||
batch_op.alter_column('type',
|
||||
existing_type=sa.VARCHAR(length=20),
|
||||
nullable=True,
|
||||
existing_server_default=sa.text("'api'::character varying"))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,36 @@
|
||||
"""Add real_name and employee_id
|
||||
|
||||
Revision ID: 5b797d05f412
|
||||
Revises: 80382a78c530
|
||||
Create Date: 2026-02-23 12:43:47.707814
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '5b797d05f412'
|
||||
down_revision = '80382a78c530'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('real_name', sa.String(length=50), nullable=True))
|
||||
batch_op.add_column(sa.Column('employee_id', sa.String(length=50), nullable=True))
|
||||
batch_op.create_unique_constraint(None, ['employee_id'])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_constraint(None, type_='unique')
|
||||
batch_op.drop_column('employee_id')
|
||||
batch_op.drop_column('real_name')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Add Department table
|
||||
|
||||
Revision ID: 60b4aee3ad2d
|
||||
Revises: 48f1ca8fa73a
|
||||
Create Date: 2026-02-23 20:56:21.095082
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '60b4aee3ad2d'
|
||||
down_revision = '48f1ca8fa73a'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('departments',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('name')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('departments')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,32 @@
|
||||
"""add_conversation_id_to_chatlog
|
||||
|
||||
Revision ID: 7797942bfa85
|
||||
Revises: b1246a8b37fa
|
||||
Create Date: 2026-02-24 08:50:46.391292
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '7797942bfa85'
|
||||
down_revision = 'b1246a8b37fa'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('conversation_id', sa.String(length=100), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.drop_column('conversation_id')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,97 @@
|
||||
"""add_zl_language_and_user_lang
|
||||
|
||||
Revision ID: 80382a78c530
|
||||
Revises: 510db8a4f806
|
||||
Create Date: 2026-02-23 12:06:43.705586
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '80382a78c530'
|
||||
down_revision = '510db8a4f806'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('zl_languages',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('code', sa.String(length=10), nullable=False),
|
||||
sa.Column('name', sa.String(length=50), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('code')
|
||||
)
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('latency', sa.Float(), nullable=False, server_default='0.0'))
|
||||
batch_op.alter_column('answer',
|
||||
existing_type=sa.TEXT(),
|
||||
nullable=False)
|
||||
batch_op.drop_index(batch_op.f('ix_chat_logs_agent_id'))
|
||||
batch_op.drop_index(batch_op.f('ix_chat_logs_conversation_id'))
|
||||
batch_op.drop_index(batch_op.f('ix_chat_logs_created_at'))
|
||||
batch_op.drop_index(batch_op.f('ix_chat_logs_user_id'))
|
||||
batch_op.drop_column('tokens_used')
|
||||
batch_op.drop_column('conversation_id')
|
||||
batch_op.drop_column('latency_ms')
|
||||
|
||||
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()))
|
||||
batch_op.drop_column('tags')
|
||||
|
||||
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
|
||||
batch_op.alter_column('reason',
|
||||
existing_type=sa.TEXT(),
|
||||
nullable=False)
|
||||
batch_op.drop_index(batch_op.f('ix_permission_requests_agent_id'))
|
||||
batch_op.drop_index(batch_op.f('ix_permission_requests_user_id'))
|
||||
|
||||
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_quick_questions_agent_id'))
|
||||
batch_op.drop_index(batch_op.f('ix_quick_questions_user_id'))
|
||||
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('language', sa.String(length=10), nullable=False, server_default='zh-CN'))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_column('language')
|
||||
|
||||
with op.batch_alter_table('quick_questions', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_quick_questions_user_id'), ['user_id'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_quick_questions_agent_id'), ['agent_id'], unique=False)
|
||||
|
||||
with op.batch_alter_table('permission_requests', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_permission_requests_user_id'), ['user_id'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_permission_requests_agent_id'), ['agent_id'], unique=False)
|
||||
batch_op.alter_column('reason',
|
||||
existing_type=sa.TEXT(),
|
||||
nullable=True)
|
||||
|
||||
with op.batch_alter_table('feedbacks', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('tags', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True))
|
||||
batch_op.drop_column('created_at')
|
||||
|
||||
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('latency_ms', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
batch_op.add_column(sa.Column('conversation_id', sa.VARCHAR(length=100), autoincrement=False, nullable=False))
|
||||
batch_op.add_column(sa.Column('tokens_used', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
batch_op.create_index(batch_op.f('ix_chat_logs_user_id'), ['user_id'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_chat_logs_created_at'), ['created_at'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_chat_logs_conversation_id'), ['conversation_id'], unique=False)
|
||||
batch_op.create_index(batch_op.f('ix_chat_logs_agent_id'), ['agent_id'], unique=False)
|
||||
batch_op.alter_column('answer',
|
||||
existing_type=sa.TEXT(),
|
||||
nullable=True)
|
||||
batch_op.drop_column('latency')
|
||||
|
||||
op.drop_table('zl_languages')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,205 @@
|
||||
"""sync schema with global questions
|
||||
|
||||
Revision ID: a3dcfbe69b00
|
||||
Revises: f0f24c20e91a
|
||||
Create Date: 2026-02-23 22:28:18.892924
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.engine.reflection import Inspector
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'a3dcfbe69b00'
|
||||
down_revision = 'f0f24c20e91a'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
conn = op.get_bind()
|
||||
inspector = Inspector.from_engine(conn)
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if 'agents' not in tables:
|
||||
op.create_table('agents',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('icon_url', sa.String(length=255), nullable=True),
|
||||
sa.Column('dify_app_id', sa.String(length=100), nullable=False),
|
||||
sa.Column('dify_api_key', sa.String(length=100), nullable=False),
|
||||
sa.Column('type', sa.String(length=20), nullable=False),
|
||||
sa.Column('api_base_url', sa.String(length=255), nullable=True),
|
||||
sa.Column('extra_config', sa.JSON(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('dify_app_id')
|
||||
)
|
||||
|
||||
if 'departments' not in tables:
|
||||
op.create_table('departments',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('code', sa.String(length=50), nullable=True),
|
||||
sa.Column('company', sa.String(length=100), nullable=True),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('code'),
|
||||
sa.UniqueConstraint('name')
|
||||
)
|
||||
else:
|
||||
columns = [c['name'] for c in inspector.get_columns('departments')]
|
||||
if 'code' not in columns:
|
||||
op.add_column('departments', sa.Column('code', sa.String(length=50), nullable=True))
|
||||
op.create_unique_constraint(None, 'departments', ['code'])
|
||||
if 'company' not in columns:
|
||||
op.add_column('departments', sa.Column('company', sa.String(length=100), nullable=True))
|
||||
|
||||
if 'zl_languages' not in tables:
|
||||
op.create_table('zl_languages',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('code', sa.String(length=10), nullable=False),
|
||||
sa.Column('name', sa.String(length=50), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('code')
|
||||
)
|
||||
|
||||
if 'users' not in tables:
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=50), nullable=False),
|
||||
sa.Column('email', sa.String(length=100), nullable=False),
|
||||
sa.Column('department', sa.String(length=100), nullable=True),
|
||||
sa.Column('role', sa.String(length=20), nullable=False),
|
||||
sa.Column('status', sa.String(length=20), nullable=False),
|
||||
sa.Column('password_hash', sa.String(length=255), nullable=True),
|
||||
sa.Column('language', sa.String(length=10), nullable=False),
|
||||
sa.Column('real_name', sa.String(length=50), nullable=True),
|
||||
sa.Column('employee_id', sa.String(length=50), nullable=True),
|
||||
sa.Column('phone_number', sa.String(length=20), nullable=True),
|
||||
sa.Column('default_agent_id', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['default_agent_id'], ['agents.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('email'),
|
||||
sa.UniqueConstraint('employee_id'),
|
||||
sa.UniqueConstraint('phone_number')
|
||||
)
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_users_username'), ['username'], unique=True)
|
||||
else:
|
||||
columns = [c['name'] for c in inspector.get_columns('users')]
|
||||
if 'default_agent_id' not in columns:
|
||||
op.add_column('users', sa.Column('default_agent_id', sa.Integer(), nullable=True))
|
||||
op.create_foreign_key(None, 'users', 'agents', ['default_agent_id'], ['id'])
|
||||
if 'real_name' not in columns:
|
||||
op.add_column('users', sa.Column('real_name', sa.String(length=50), nullable=True))
|
||||
if 'employee_id' not in columns:
|
||||
op.add_column('users', sa.Column('employee_id', sa.String(length=50), nullable=True))
|
||||
try:
|
||||
op.create_unique_constraint(None, 'users', ['employee_id'])
|
||||
except: pass
|
||||
if 'phone_number' not in columns:
|
||||
op.add_column('users', sa.Column('phone_number', sa.String(length=20), nullable=True))
|
||||
try:
|
||||
op.create_unique_constraint(None, 'users', ['phone_number'])
|
||||
except: pass
|
||||
|
||||
if 'chat_logs' not in tables:
|
||||
op.create_table('chat_logs',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('query', sa.Text(), nullable=False),
|
||||
sa.Column('answer', sa.Text(), nullable=False),
|
||||
sa.Column('latency', sa.Float(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
if 'permission_requests' not in tables:
|
||||
op.create_table('permission_requests',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('reason', sa.Text(), nullable=False),
|
||||
sa.Column('status', sa.String(length=20), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('reviewed_at', sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
if 'quick_questions' not in tables:
|
||||
op.create_table('quick_questions',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('title', sa.String(length=100), nullable=False),
|
||||
sa.Column('question', sa.Text(), nullable=False),
|
||||
sa.Column('sort_order', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
if 'user_agent_permissions' not in tables:
|
||||
op.create_table('user_agent_permissions',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id', 'agent_id')
|
||||
)
|
||||
|
||||
if 'user_default_agents' not in tables:
|
||||
op.create_table('user_default_agents',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('sort_order', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
if 'user_favorite_agents' not in tables:
|
||||
op.create_table('user_favorite_agents',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id', 'agent_id')
|
||||
)
|
||||
|
||||
if 'user_homepage_configs' not in tables:
|
||||
op.create_table('user_homepage_configs',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('layout', sa.JSON(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id')
|
||||
)
|
||||
|
||||
if 'feedbacks' not in tables:
|
||||
op.create_table('feedbacks',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('chat_log_id', sa.Integer(), nullable=False),
|
||||
sa.Column('score', sa.Integer(), nullable=False),
|
||||
sa.Column('comment', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_log_id'], ['chat_logs.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('chat_log_id')
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
pass
|
||||
@@ -0,0 +1,42 @@
|
||||
"""make dify credentials nullable
|
||||
|
||||
Revision ID: b1246a8b37fa
|
||||
Revises: a3dcfbe69b00
|
||||
Create Date: 2026-02-23 22:43:26.470122
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'b1246a8b37fa'
|
||||
down_revision = 'a3dcfbe69b00'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('agents', schema=None) as batch_op:
|
||||
batch_op.alter_column('dify_app_id',
|
||||
existing_type=sa.VARCHAR(length=100),
|
||||
nullable=True)
|
||||
batch_op.alter_column('dify_api_key',
|
||||
existing_type=sa.VARCHAR(length=100),
|
||||
nullable=True)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('agents', schema=None) as batch_op:
|
||||
batch_op.alter_column('dify_api_key',
|
||||
existing_type=sa.VARCHAR(length=100),
|
||||
nullable=False)
|
||||
batch_op.alter_column('dify_app_id',
|
||||
existing_type=sa.VARCHAR(length=100),
|
||||
nullable=False)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Add UserHomepageConfig, UserFavoriteAgent, UserDefaultAgent and Update Department
|
||||
|
||||
Revision ID: f0f24c20e91a
|
||||
Revises: 60b4aee3ad2d
|
||||
Create Date: 2026-02-23 21:13:19.814958
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'f0f24c20e91a'
|
||||
down_revision = '60b4aee3ad2d'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('user_default_agents',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('sort_order', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('user_favorite_agents',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('agent_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id', 'agent_id')
|
||||
)
|
||||
op.create_table('user_homepage_configs',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('layout', sa.JSON(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id')
|
||||
)
|
||||
with op.batch_alter_table('departments', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('code', sa.String(length=50), nullable=True))
|
||||
batch_op.add_column(sa.Column('company', sa.String(length=100), nullable=True))
|
||||
batch_op.create_unique_constraint(None, ['code'])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('departments', schema=None) as batch_op:
|
||||
batch_op.drop_constraint(None, type_='unique')
|
||||
batch_op.drop_column('company')
|
||||
batch_op.drop_column('code')
|
||||
|
||||
op.drop_table('user_homepage_configs')
|
||||
op.drop_table('user_favorite_agents')
|
||||
op.drop_table('user_default_agents')
|
||||
# ### end Alembic commands ###
|
||||
Reference in new issue
Block a user