<p>这是一个非常宽泛的问题,真正实现你的想法将取决于你,但这是可能的。</p>
<p>您可以从您的Python代码中调用Alembic而不使用命令,因为它也是用Python实现的!你只需要重新创建命令在幕后所做的事情。</p>
<p>不可否认,这些文档的状态不是很好,因为它们仍然是库的相对早期版本,但是稍微挖掘一下,您会发现以下内容:</p>
<ol>
<li>创建<a href="http://alembic.zzzcomputing.com/en/latest/api/config.html" rel="noreferrer">Config</a></li>
<li>使用配置创建<a href="http://alembic.zzzcomputing.com/en/latest/api/script.html" rel="noreferrer">ScriptDirectory</a></li>
<li>使用Config和script目录创建<a href="http://alembic.zzzcomputing.com/en/latest/api/runtime.html#the-environment-context" rel="noreferrer">EnvironmentContext</a></li>
<li>使用EnvironmentContext创建<a href="http://alembic.zzzcomputing.com/en/latest/api/runtime.html#the-migration-context" rel="noreferrer">MigrationContext</a></li>
<li>大多数命令使用Config和MigrationContext中的一些方法组合</li>
</ol>
<p>我编写了一个扩展来提供对Flask SQLAlchemy数据库的编程Alembic访问。这个实现与Flask和Flask SQLAlchemy绑定在一起,但它应该是一个很好的起点。<a href="https://bitbucket.org/davidism/flask-alembic" rel="noreferrer">See Flask-Alembic here.</a></p>
<p>关于如何创建新数据库的最后一点,可以使用Alembic创建表,也可以使用<code>metadata.create_all()</code>然后<code>alembic stamp head</code>(或等效的python代码)。我建议始终使用迁移路径创建表,并忽略原始的<code>metadata.create_all()</code>。</p>
<p>我对cx_freeze没有任何经验,但只要迁移包含在发行版中,并且代码中指向该目录的路径正确,就应该没问题。</p>