检测Django项目的向后不兼容迁移

django-migration-linter的Python项目详细描述


检测Django项目的向后不兼容迁移。确保迁移不会破坏任何内容,这将节省您的时间。

https://travis-ci.org/3YOURMIND/django-migration-linter.svg?branch=masterhttps://img.shields.io/pypi/v/django-migration-linter.svghttps://img.shields.io/github/license/3yourmind/django-migration-linter.svghttps://img.shields.io/badge/PR-welcome-green.svghttps://img.shields.io/badge/3YOURMIND-Hiring-brightgreen.svghttps://img.shields.io/github/stars/3YOURMIND/django-migration-linter.svg?style=social&label=Stars

安装

pip install django-migration-linter

用法

将迁移linter添加到您的INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    "django_migration_linter",
    ...,
]

python manage.py lintmigrations [GIT_COMMIT_ID] [--ignore-name-contains IGNORE_NAME_CONTAINS] [--include-apps INCLUDE_APPS [INCLUDE_APPS ...] | --exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]][--exclude-migration-tests MIGRATION_TEST_CODE [MIGRATION_TEST_CODE ...]][--project-root-path DJANGO_PROJECT_FOLDER]

ParameterDescription
^{tt4}$If specified, only migrations since this commit will be taken into account. If not specified, all migrations will be linted.
^{tt5}$Ignore migrations containing this name.
^{tt6}$Ignore migrations with exactly one of these names.
^{tt7}$Check only migrations that are in the specified django apps.
^{tt8}$Ignore migrations that are in the specified django apps.
^{tt9}$Specify backward incompatible migration tests to be ignored using the code (e.g. ALTER_COLUMN).
^{tt10}$Print more information during execution.
^{tt11}$Specify the database for which to generate the SQL. Defaults to default.
^{tt12}$specify a directory that should be used to store cache-files in.
^{tt13}$Don’t use a cache.
^{tt14}$Only lint migrations that are applied to the selected database. Other migrations are ignored.
^{tt15}$Only lint migrations that are not yet applied to the selected database. Other migrations are ignored.
^{tt16}$An absolute or relative path to the django project.

示例

3你的大脑正在运行每一个通过ci的构建的linter。 这样可以确保迁移允许A/B测试、蓝/绿部署,并且不会破坏您的开发环境。 作为每一个合理的工具,非零错误代码意味着至少找到一个无效的迁移。

向后不兼容迁移

linter分析您的迁移,并检查sql中的:

  • 添加了NOT NULL列,这些列没有默认值
  • 删除列
  • 删除表格
  • 重命名列
  • 重命名表
  • 正在更改列(可以向后兼容并可能被忽略)
  • 添加唯一约束

这些是最重要和最频繁的向后不兼容迁移。 如果您能具体说明,我们很高兴再补充一些。

忽略迁移

也可以通过将此添加到迁移中来忽略迁移:

import django_migration_linter as linter
# ...

    operations = [
        linter.IgnoreMigration(),
        # ...
    ]
# ...

忽略迁移测试

通过在执行期间添加此选项,还可以忽略向后不兼容的迁移测试:

python manage.py lintmigrations --exclude-migration-tests ALTER_COLUMN

迁移测试代码可以在文件django_migration_linter/sql_analyser.py中找到。

缓存

默认情况下,linter使用缓存来防止多次对同一迁移进行linting操作。 Linux上缓存的默认位置是 /home/<username>/.cache/django-migration-linter/<version>/<ldjango-project>_<database_name>.pickle

由于linter使用文件内容的散列,因此修改迁移文件将在该迁移上重新运行linter。 如果要在没有缓存的情况下运行linter,请使用标记--no-cache。 如果要使缓存无效,请删除缓存文件夹。 缓存文件夹也可以通过--cache-path选项手动定义。

测试

运行测试的最简单方法是调用tox

您需要安装测试需求,可以在setup.py文件中找到。 开始的一个好方法是通过执行pip install -e .[test]来安装linter的开发版本。

要完全测试linter,需要同时运行mysql和postgresql数据库。 您可以调整tests/test_project/settings.py文件以获得正确的数据库设置,或者让数据库和用户与默认travis用户对应。

贡献

首先,如果你想为这个项目做贡献的话,非常感谢。 请将您的工作建立在master分支上,并在pull请求中以该分支为目标。

发布包

关于通常如何向pypi发布linter的一个小说明:

  • python setup.py check --restructuredtext
  • python3 setup.py sdist bdist_wheel --universal
  • twine upload dist/django_migration_linter-X.Y.Z-py2.py3-none-any.whldist/django-migration-linter-X.Y.Z.tar.gz

博客文章

Keeping Django database migrations backward compatible

许可证

django migration linterApache 2.0 License下释放。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何定制springdata存储库方法名称?   html有没有办法将java应用程序或JApplet嵌入到网站中?   Jackson ObjectMapper将java从字符串序列化为JSON作为namevalue   java刷新JTable?   用Java程序分发用JavaDB制作的数据库   java Android如何启动新活动   当集合大小超过500.000时,java的处理速度会显著降低   在java的分层目录中的多个目录中查找相同的文件   java如何将ArrayList数据插入数据库   java如何修改此代码,使其时间复杂度为o(logn)或o(n),而不是o(n^2)   java面板(SimplePanel)中仅显示图像的前1/3(大约),   jakarta ee Java Bean和企业Java Bean之间的区别?   创建AWS Cloudfront自签名URL(java sdk)时出现amazon web服务错误   基于Jersey和Jackson查询参数的java动态属性过滤