dify/api/migrations/versions/2025_10_31_1505-f26e7cdbb0fe_.py
longbingljw fcc71124b1
feat:mysql adaptation for meta db (#50)
* phase1:uuid stringdefault timestamp date

* phase2:json adapt

* phase3:BinaryData,ARRAY,Text,

* fix sa.String()

* fix defalut valud

* models adapt

* models adapt supplement

* fix pg unique dialect

* supplement

* migration fix

* migration fix

* date sql adapt

* model adapt fix

* config adapt

* migration fix

* fix

* fix anotation in .env.example

* config feat

* config fix

* .env.example adapt fix
2025-11-07 15:35:31 +08:00

203 lines
9.3 KiB
Python

"""empty message
Revision ID: f26e7cdbb0fe
Revises: 03f8dcbc611e
Create Date: 2025-10-31 15:05:38.637798
"""
from alembic import op
import models as models
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql, mysql
def _is_pg(conn):
return conn.dialect.name == "postgresql"
# revision identifiers, used by Alembic.
revision = 'f26e7cdbb0fe'
down_revision = '03f8dcbc611e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
if _is_pg(conn):
with op.batch_alter_table('data_source_oauth_bindings', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('source_info_idx'), postgresql_using='gin')
batch_op.alter_column('source_info',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=False)
with op.batch_alter_table('datasets', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('retrieval_model_idx'), postgresql_using='gin')
batch_op.alter_column('retrieval_model',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=True)
batch_op.alter_column('icon_info',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=True)
with op.batch_alter_table('datasource_oauth_params', schema=None) as batch_op:
batch_op.alter_column('system_credentials',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=False)
with op.batch_alter_table('datasource_oauth_tenant_params', schema=None) as batch_op:
batch_op.alter_column('client_params',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=False)
with op.batch_alter_table('datasource_providers', schema=None) as batch_op:
batch_op.alter_column('provider',
existing_type=sa.VARCHAR(length=255),
type_=sa.String(length=128),
existing_nullable=False)
batch_op.alter_column('encrypted_credentials',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=False)
with op.batch_alter_table('documents', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('document_metadata_idx'), postgresql_using='gin')
batch_op.alter_column('doc_metadata',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=True)
with op.batch_alter_table('external_knowledge_bindings', schema=None) as batch_op:
batch_op.alter_column('external_knowledge_id',
existing_type=sa.TEXT(),
type_=sa.String(length=512),
existing_nullable=False)
with op.batch_alter_table('providers', schema=None) as batch_op:
batch_op.drop_column('credential_status')
with op.batch_alter_table('tenant_plugin_auto_upgrade_strategies', schema=None) as batch_op:
op.execute("""
ALTER TABLE tenant_plugin_auto_upgrade_strategies
ALTER COLUMN exclude_plugins TYPE JSON
USING array_to_json(exclude_plugins)
""")
op.execute("""
ALTER TABLE tenant_plugin_auto_upgrade_strategies
ALTER COLUMN include_plugins TYPE JSON
USING array_to_json(include_plugins)
""")
with op.batch_alter_table('tool_oauth_tenant_clients', schema=None) as batch_op:
batch_op.alter_column('plugin_id',
existing_type=sa.VARCHAR(length=512),
type_=sa.String(length=255),
existing_nullable=False)
else:
with op.batch_alter_table('providers', schema=None) as batch_op:
batch_op.drop_column('credential_status')
with op.batch_alter_table('workflows', schema=None) as batch_op:
batch_op.alter_column('updated_at',
existing_type=sa.TIMESTAMP(),
type_=sa.DateTime(),
existing_nullable=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
if _is_pg(conn):
with op.batch_alter_table('tool_oauth_tenant_clients', schema=None) as batch_op:
batch_op.alter_column('plugin_id',
existing_type=sa.String(length=255),
type_=sa.VARCHAR(length=512),
existing_nullable=False)
with op.batch_alter_table('tenant_plugin_auto_upgrade_strategies', schema=None) as batch_op:
op.execute("""
ALTER TABLE tenant_plugin_auto_upgrade_strategies
ALTER COLUMN exclude_plugins TYPE JSON
USING array_to_json(exclude_plugins)
""")
op.execute("""
ALTER TABLE tenant_plugin_auto_upgrade_strategies
ALTER COLUMN include_plugins TYPE JSON
USING array_to_json(include_plugins)
""")
with op.batch_alter_table('providers', schema=None) as batch_op:
batch_op.add_column(sa.Column('credential_status', sa.VARCHAR(length=20), server_default=sa.text("'active'::character varying"), autoincrement=False, nullable=True))
with op.batch_alter_table('external_knowledge_bindings', schema=None) as batch_op:
batch_op.alter_column('external_knowledge_id',
existing_type=sa.String(length=512),
type_=sa.TEXT(),
existing_nullable=False)
with op.batch_alter_table('documents', schema=None) as batch_op:
batch_op.alter_column('doc_metadata',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True)
batch_op.create_index(batch_op.f('document_metadata_idx'), ['doc_metadata'], unique=False, postgresql_using='gin')
with op.batch_alter_table('datasource_providers', schema=None) as batch_op:
batch_op.alter_column('encrypted_credentials',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=False)
batch_op.alter_column('provider',
existing_type=sa.String(length=128),
type_=sa.VARCHAR(length=255),
existing_nullable=False)
with op.batch_alter_table('datasource_oauth_tenant_params', schema=None) as batch_op:
batch_op.alter_column('client_params',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=False)
with op.batch_alter_table('datasource_oauth_params', schema=None) as batch_op:
batch_op.alter_column('system_credentials',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=False)
with op.batch_alter_table('datasets', schema=None) as batch_op:
batch_op.alter_column('icon_info',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True)
batch_op.alter_column('retrieval_model',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True)
batch_op.create_index(batch_op.f('retrieval_model_idx'), ['retrieval_model'], unique=False, postgresql_using='gin')
with op.batch_alter_table('data_source_oauth_bindings', schema=None) as batch_op:
batch_op.alter_column('source_info',
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=False)
batch_op.create_index(batch_op.f('source_info_idx'), ['source_info'], unique=False, postgresql_using='gin')
else:
with op.batch_alter_table('workflows', schema=None) as batch_op:
batch_op.alter_column('updated_at',
existing_type=sa.DateTime(),
type_=sa.TIMESTAMP(),
existing_nullable=False)
with op.batch_alter_table('providers', schema=None) as batch_op:
batch_op.add_column(sa.Column('credential_status', mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=20), server_default=sa.text("'active'"), nullable=True))
# ### end Alembic commands ###