如何在全新的应用程序部署中将数据库与Flask-Migrate同步? [英] How to sync db with Flask-Migrate in a fresh application deployment?

查看:153
本文介绍了如何在全新的应用程序部署中将数据库与Flask-Migrate同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将应用程序部署到新服务器(即数据库为空)时,如何使用Flask-Migrate正确同步数据库?

When deploying an application to a fresh server (i.e. the database is empty) how do I sync the database properly with Flask-Migrate?

当我已经有一些架构时,我已经向项目添加了Flask-Migrate,因此我没有对所有create_table()进行初始"迁移.现在,当我在部署脚本中执行manage.py db upgrade时,我会得到relation "..." does not exist.

I've added Flask-Migrate to the project when I already had some schema in place so I don't have "initial" migrations with all create_table(). Now when I do manage.py db upgrade in my deployment script I get relation "..." does not exist.

是否有任何内置方法来检测空数据库并运行'create_all()'而不是迁移?这就是Alembic的食谱建议.由于我已经在使用Flask-Migrate,因此我正在寻找某种统一的方式来处理迁移.

Is there any built-in way to detect empty database and run 'create_all()' instead of migrations? This is what the Alembic's Cookbook suggests. Since I'm using Flask-Migrate already I'm looking for some unified way of dealing with migrations.

推荐答案

理想的解决方案是为数据库模式生成初始迁移,因为那是您开始使用Flask-Migrate和Alembic跟踪迁移的那天.

The ideal solution is that you generate an initial migration for your db schema as it was the day you started tracking migrations with Flask-Migrate and Alembic.

如果您当时记得要这样做,那么这样做很简单.只需创建一个单独的空数据库(不留您的实际数据库),将您的应用程序配置为使用该空数据库,然后生成一个迁移.此迁移将具有整个架构.一旦生成了迁移,就摆脱空的数据库,并将配置恢复回实际的数据库.

Doing this is simple if you remember to do it at the time. Just create a separate empty database (leave your real db alone), configure your app to use the empty database, and then generate a migration. This migration will have the entire schema. Once you have that migration generated, get rid of the empty database and restore your configuration back to the real db.

如果您已经有其他迁移,那么它将变得更加复杂.您将必须返回到要生成初始迁移的代码版本,然后按照上述过程进行生成.最后,您需要将初始迁移作为第一个迁移插入到迁移列表中.查看一些迁移脚本,以了解每次迁移如何引用上一个迁移脚本.进行一些小的修改就可以使您正常运行.

If you already have additional migrations then it gets a little bit more complicated. You will have to go back to the version of your code where you want to generate the initial migration, then follow the above procedure to generate it. Finally, you will need to insert the initial migration in the migration list as the first one. Look at a few of the migration scripts to figure out how each migration references the previous one. A couple of small edits should get you up and running.

如果这看起来工作太多,那么另一种选择是使用db.create_all()生成直到最新迁移的数据库,然后使用./manage.py db stamp head告诉Alembic它应该考虑数据库已更新.

If this seems like too much work, then the other alternative is that you use db.create_all() to generate the database up to the latest migration, and then ./manage.py db stamp head to tell Alembic that it should consider the database updated.

这篇关于如何在全新的应用程序部署中将数据库与Flask-Migrate同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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