如果您不是从alembic/flask-migrate开始的,那么如何添加迁移现有数据库? [英] How do you add migrate an existing database with alembic/flask-migrate if you did not start off with it?

查看:206
本文介绍了如果您不是从alembic/flask-migrate开始的,那么如何添加迁移现有数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是已经发生和正在发生的一系列事件

This is the chain of events that has and is happening

  • 第0天:我开发并部署了我的应用程序
  • 第一天:我创建了新数据库
  • 第三天:我意识到我想在现有表中添加新行.我找到了flask-migrate,并想用它来迁移数据库.

目前我在第3天

有很多关于如何从第0天开始运行Flask-migrate的文档.您只需调用flask db initflask db migrateflask db upgrade.

There are plenty of documentations on how to get Flask-migrate running if you start from Day 0. You just call flask db init, flask db migrate and flask db upgrade.

但是,就我而言,情况有所不同.我运行了命令,迁移的第一个版本是 empty .然后,我修改了数据库架构并生成了新的迁移.现在,我的最新迁移只有1行迁移,这会将新行添加到表中.

However, for my case, it's a bit different. I ran the commands, and my first version of migration is empty. Then I modified my database schema and generated a new migration. Now my latest migration only has 1 line of migration which is adding the new row to the table.

我意识到我的迁移都没有创建数据库的实际架构,这应该是在第1天开始flask-migrate时所看到的第一个迁移.

I realized that none of my migrations have the actual schema to create the database, which is supposed to be the first migration you see if you start flask-migrate on day 1.

如果要从头克隆我的存储库:

flask db migrate将产生alembic.util.exc.CommandError: Target database is not up to date..

flask db upgrade将导致sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "offer" does not exist.

我该怎么做才能解决此问题?

What can I do to fix this?

推荐答案

您有两个选择.

1)如果您只想跟踪将来的数据库迁移

1) If you only want to track database migrations going forward

  • 运行flask db init,以创建迁移存储库.
  • 将新列添加到数据库模型中.
  • 运行flask db migrate,以生成迁移.迁移脚本中将仅包含新列.
  • 运行flask db upgrade将新迁移应用于您的数据库.
  • Run flask db init, to create the migration repository.
  • Add the new column to your database model.
  • Run flask db migrate, to generate a migration. The migration script will only have the new column in it.
  • Run flask db upgrade to apply the new migration to your database.

此时,您的数据库应具有新列,您可以继续工作.每当您需要进行其他更改时,请重复上述步骤.

At this point your database should have the new column, and you can continue working. Repeat the above steps any time you need to make additional changes.

请注意,使用这种方法,您不能从头开始重新创建整个数据库.您必须有一种方法可以将数据库初始化为第1天使用的架构,然后可以将迁移历史记录应用于该数据库,以将其升级到当前架构.

Note that with this approach, you cannot recreate the entire database from scratch. You have to have a way to initialize the database to the schema you had on Day 1, and then you can apply the migration history to that to upgrade it to your current schema.

2)如果要跟踪整个迁移历史记录,包括将Flask-Migrate添加到应用程序当天的架构.

2) If you want to keep track of the entire migration history, including the schema on the day you are adding Flask-Migrate to your application.

这有点棘手,但是可以做到.

This is a little bit tricky, but it can be done.

  • flask db init开头,以创建迁移存储库.
  • 接下来,您需要诱使Flask-Migrate认为您的数据库为空.您可以通过重命名实际的数据库并创建一个没有表名称的新数据库来做到这一点.在那种状态下,运行flask db migrate.这将生成一个迁移,其中包含数据库的整个架构.
  • 进行了初始迁移后,将数据库还原到正确的状态.
  • 运行flask db stamp head将数据库标记为已更新.
  • 将新列添加到您的数据库模型中.
  • 再次运行flask db migrate,以生成第二个迁移.迁移脚本中将仅包含新列.
  • 运行flask db upgrade将新迁移应用于您的数据库.
  • Start with flask db init, to create the migration repository.
  • Next, you need to trick Flask-Migrate into thinking your database is empty. You can do this by renaming your actual db, and creating a new db with the same name that has no tables in it. In that state, run flask db migrate. This will generate a migration that contains the entire schema of your database.
  • Once you have that initial migration, restore your database to the correct state.
  • Run flask db stamp head to mark the database as updated.
  • Add the new column to your database model.
  • Run flask db migrate again, to generate a second migration. The migration script will only have the new column in it.
  • Run flask db upgrade to apply the new migration to your database.

希望这会有所帮助!

这篇关于如果您不是从alembic/flask-migrate开始的,那么如何添加迁移现有数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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