如何重置Migrations

2017-08-24

how to reset django migrations (图片: https://www.pexels.com/photo/sky-flying-animals-birds-1209/)

Django迁移系统已经开发并优化了大量迁移工作。通常你不要介意在代码库中保留大量的模型迁移。即使有时会导致一些不期望的影响,例如在运行测试时花费很多时间。但是在这样的情况下,您可以轻松地禁用迁移(尽管目前没有内置的选项)。

无论如何,如果要执行清理,我将在本教程中介绍几个选项。


情景1:

该项目仍处于开发环境中,您要进行全面的清理。你不介意把整个数据库丢掉。

1.删​​除项目中的所有迁移文件

通过每个项目的应用程序迁移文件夹,并删除__init__.py文件以外的所有内容。

或者如果您使用的是类Unix操作系统,则可以运行以下脚本(在项目目录中):

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

2.删除当前数据库,或者删除(如果是 db.sqlite3)。

3.创建初始迁移并生成数据库模式:

python manage.py makemigrations
python manage.py migrate

那么继续


情景2:

您要清除所有迁移历史记录,但是要保留现有的数据库。

1.确保您的模型符合当前的数据库模式

最简单的方法是尝试创建新的迁移:

python manage.py makemigrations

如果有任何待处理的迁移,请先应用它们。

如果您看到消息:

No changes detected

准备好继续。

2.清除每个应用程序的迁移历史记录

现在,您需要通过应用清除迁移历史记录应用。

首先运行showmigrations命令,以便跟踪发生了什么:

$ python manage.py showmigrations

结果:

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 [X] 0001_initial
 [X] 0002_remove_mymodel_i
 [X] 0003_mymodel_bio
sessions
 [X] 0001_initial

清除迁移历史记录(请注意,core是我的应用程序的名称):

$ python manage.py migrate --fake core zero

结果将是这样的:

Operations to perform:
  Unapply all migrations: core
Running migrations:
  Rendering model states... DONE
  Unapplying core.0003_mymodel_bio... FAKED
  Unapplying core.0002_remove_mymodel_i... FAKED
  Unapplying core.0001_initial... FAKED

现在再次运行showmigrations命令:

$ python manage.py showmigrations

结果:

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 [ ] 0001_initial
 [ ] 0002_remove_mymodel_i
 [ ] 0003_mymodel_bio
sessions
 [X] 0001_initial

您必须为要重置迁移历史记录的所有应用程序执行此操作。

3.删除实际的迁移文件。

浏览每个项目的应用程序迁移文件夹,并删除文件中的所有内容__init__.py。

或者如果您使用的是类Unix操作系统,则可以运行以下脚本(在项目目录中):

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

PS:上面的例子将删除项目中的所有迁移文件。

再次运行showmigrations

$ python manage.py showmigrations

结果:

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 (no migrations)
sessions
 [X] 0001_initial

4.创建初始迁移

$ python manage.py makemigrations

结果:

Migrations for 'core':
  0001_initial.py:
    - Create model MyModel

5. Fake the initial migration

在这种情况下,您将无法应用初始迁移,因为数据库表已存在。我们想要做的是伪造这种 migration:

$ python manage.py migrate --fake-initial

结果:

Operations to perform:
  Apply all migrations: admin, core, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying core.0001_initial... FAKED

再次运行showmigrations

dmin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 [X] 0001_initial
sessions
 [X] 0001_initial

这样我们都完成了 :)

https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html