python - 应该如何使用 Alembic 删除 SQLAlchemy-Searchable 触发器

标签 python postgresql sqlalchemy alembic

我正在尝试正确设置我的数据库迁移文件,但没有找到降级部分的明确语法。使用 Flask-SQLAlchemy、Postgres 和使用 Alembic 的 Flask-Migrate

我的(简化的)代码如下所示:

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils
from sqlalchemy_searchable import sync_trigger

# revision identifiers, used by Alembic....(cut for space)

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('project_note',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('text', sa.Text(), nullable=False),
    sa.Column('search_vector', sqlalchemy_utils.types.ts_vector.TSVectorType(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_project_note_search_vector', 'project_note', ['search_vector'], unique=False, postgresql_using='gin')
    # ### end Alembic commands ###
    # Manually added commands
    conn = op.get_bind()
    sync_trigger(conn, 'project_note', 'search_vector', ['text'])


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_project_note_search_vector', table_name='project_note')
    op.drop_index(op.f('ix_project_note_project_id'), table_name='project_note')
    op.drop_table('project_note')
    # ### end Alembic commands ###

在降级中我需要做些什么来清理触发器或者删除表是否为我做这件事?

我找到了 documentation及相关代码

from alembic import op
from sqlalchemy_searchable import sync_trigger


def upgrade():
    conn = op.get_bind()
    op.add_column('article', sa.Column('content', sa.Text))

    sync_trigger(conn, 'article', 'search_vector', ['name', 'content'])

# ... same for downgrade

部分 # ... same for downgrade 不清楚。这是否意味着与此完全相同的代码(使用删除而不是添加)?

def downgrade():
        op.drop_column('article', 'content')
        conn = op.get_bind()
        sync_trigger(conn, 'article', 'search_vector', ['name', 'content'])

我在开始一张新 table 时遇到了更多麻烦。如果我把

sync_trigger(conn, 'project_note', 'search_vector', ['text'])

在 Alembic 命令之前,触发器函数被留在后面。如果它是在 Alembic 生成的命令之后,那么您会收到一条错误消息,指出该表不存在。我还没有找到 sync_trigger 的 indexed_columns 参数的参数,它会在不创建任何新内容的情况下删除所有内容。空列表不起作用。

最佳答案

答案是肯定的,您应该以与 upgrade 完全相同的方式调用 sync_triggersync_trigger 的文档是这样写的:

1. Drops search trigger for given table (if it exists)
2. Drops search function for given table (if it exists)
3. Creates search function for given table
4. Creates search trigger for given table

注意前两点。

关于python - 应该如何使用 Alembic 删除 SQLAlchemy-Searchable 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48208959/

相关文章:

python - OpenCV 打开文件错误(Assertion failed)

python - 如何在QDateEdit弹出QCalendarWidget中添加Today按钮

php - 连接postgres和php,使用d3.js可视化

python - 如何在 Sqlalchemy 中设置用户定义类型

具有关系的 SQLAlchemy 克隆表行

python - Jupyter 卡在 pip install 中

python - 我可以在 python 输入缓冲区中插入可删除字符吗?

mysql - kubernetes 中的关系数据库复制有多个 master

javascript - 节点 Postgres Pub/Sub - 保留剩余的连接槽

python - 如果我需要自动递增 ID 但想要检查另一个字段上的重复项,如何定义模型?