根据后端跳过alembic修订

2024-04-26 03:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Alembic中有一个版本,它依赖于一个特定的后端,但是语义并不明确地依赖于它(只会使事情变得更快)。在

我希望我的代码不依赖于特定的后端(即,修订版在运行时不会出错)。我应该在def upgrade():def downgrade():上写入什么条件才能不对其他后端运行修订?在

我正在考虑的一个具体例子是:以下修订版仅在postgres中有效。但是,应用程序仍将在sqllite中运行:

def upgrade():
    op.execute('CREATE EXTENSION pg_trgm') # requires being superuser
    op.execute('CREATE INDEX ix_document_id ON document USING gin (id gin_trgm_ops)')

def downgrade():
    op.execute('DROP INDEX ix_document_id')
    op.execute('DROP EXTENSION pg_trgm')

同样,sqllite中的这个错误。在


Tags: idexecuteindexdefcreateextensiondocumentupgrade
1条回答
网友
1楼 · 发布于 2024-04-26 03:28:27

虽然在alembic中可能有更好的方法来创建条件操作,但最简单的方法之一就是基于当前方言保护迁移操作:

...

from alembic import context

...


def upgrade():
    if context.get_impl().bind.dialect.name == "postgresql":
        pass
    pass


def downgrade():
    if context.get_impl().bind.dialect.name == "postgresql":
        pass
    pass

相关问题 更多 >