Python - 使用 Setuptools 打包 Alembic 迁移 [英] Python - Packaging Alembic Migrations with Setuptools

查看:39
本文介绍了Python - 使用 Setuptools 打包 Alembic 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Setuptools setup.py 文件中打包 Alembic 迁移文件的正确方法是什么?一切都以 alembic/ 的形式存在于我的仓库根目录中.

What is the right way to package Alembic migration files in a Setuptools setup.py file? Everything is in my repo root as alembic/.

这是一个 Python 应用程序,而不是一个库.

This is a Python application, not a library.

我想要的安装流程是有人可以pip install我的应用程序的轮子.然后,他们将能够通过运行诸如 之类的东西来初始化应用程序数据库.alembic 升级 --sqlalchemy.url=.然后升级需要 pip install -U,之后他们可以再次运行 Alembic 命令.

My desired installation flow is that someone can pip install the wheel that is my application. They would then be able to initialize the application database by running something like <app> alembic upgrade --sqlalchemy.url=<db_url>. Upgrades would then require a pip install -U, after which they can run the Alembic command again.

这是非正统的吗?

如果没有,我将如何实现这一目标?当然是 console_scripts entry_points.但除此之外?

If not, how would I accomplish this? Certainly a console_scripts entry_points. But beyond that?

推荐答案

我不确定这是不是正确的方法,但我是这样做的:

I am not sure this is the right way but I did it this way:

首先,您可以使用 -x 选项向 alembic 添加各种自定义选项,您可以在 这个很好的答案.这允许您在运行时指定 db_url 并使其覆盖 config.ini 中的值.

First, you can add sort of custom options to alembic using the -x option and you can find details explained in this great answer. This allows you to specify the db_url at runtime and make it override the value in the config.ini.

然后,我通过将 alembic.ini 文件和 alembic 目录从我的项目根目录移动到我的顶级 python 包,成功地打包了 alembic 和我的迁移:

Then I managed to package alembic and my migrations by moving the alembic.ini file and the alembic directory from my project root to my top-level python package:

<project root>
├── src
│   └── <top-level package dir>
│       ├── alembic
│       │   ├── env.py
│       │   ├── README
│       │   ├── script.py.mako
│       │   └── versions
│       │       ├── 58c8dcd5fbdc_revision_1.py
│       │       └── ec385b47da23_revision_2.py
│       ├── alembic.ini
│       ├── __init__.py
│       └── <other files and dirs>
└── <other files and dirs>

这允许在我的 setup.py 中使用 setuptools package_data 指令:

This allows to use the setuptools package_data directive inside my setup.py:

setup(
    name=<package_name>,
    package_dir={'': 'src'},
    packages=find_packages(where='src'),
    package_data={
        '<top-level package dir>': ['alembic.ini', 'alembic/*', 'alembic/**/*'],
    },
    [...]
)  

此时,alembic 配置和修订版已正确打包,但必须调整 alembic.ini 设置以反映新目录树.可以使用 %(here)s 参数来完成,该参数包含包含 alembic.ini 文件的目录的绝对路径:

A this point, the alembic config and revisions are correctly packaged but the alembic.ini settings have to be tweaked to reflect the new directory tree. It can be done using the %(here)s param which contains the absolute path of the directory containing the alembic.ini file:

# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = %(here)s/alembic

[...]

# version location specification; this defaults
# to alembic/versions.  When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
version_locations = %(here)s/alembic/versions

[...]

最后,您必须使用 -c 选项调用 alembic,该选项允许提供配置文件的路径:

Finally, you have to call alembic with the -c option which allows to provide the path of the config file:

alembic -c <path to alembic.ini> ...

这篇关于Python - 使用 Setuptools 打包 Alembic 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆