我需要为django项目的postgresql数据库安装citext扩展。对于项目本身来说,它通过迁移进行得很顺利,工作得很好,但是我的pytest配置了--no-migrations
选项,所以pytest在不运行迁移的情况下创建数据库。我如何让pytest在创建表之前安装citext
postgres扩展?目前我正在获取- django.db.utils.ProgrammingError: type "citext" does not exist
当pytest尝试创建表auth_users
时
sql = 'CREATE TABLE "auth_user" ("id" serial NOT NULL PRIMARY KEY, "password" varchar(128) NOT NULL, "last_login" timestamp ...T NULL, "is_active" boolean NOT NULL, "date_joined" timestamp with time zone NOT NULL, "email" citext NOT NULL UNIQUE)', params = None
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0x7fb313bb0100>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7fb30d9f8580>})
我尝试使用django_db_setup
fixture,但我没有弄清楚如何更改它,因为这样的东西
@pytest.fixture(scope="session")
def django_db_setup(
request,
django_test_environment,
django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
django_db_modify_db_settings,
):
"""Top level fixture to ensure test databases are available"""
from django.test.utils import setup_databases, teardown_databases
setup_databases_args = {}
if not django_db_use_migrations:
from pytest_django.fixtures import _disable_native_migrations
_disable_native_migrations()
if django_db_keepdb and not django_db_createdb:
setup_databases_args["keepdb"] = True
with django_db_blocker.unblock():
from django.db import connection
cursor = connection.cursor()
cursor.execute("CREATE EXTENSION IF NOT EXISTS citext;")
db_cfg = setup_databases(
verbosity=request.config.option.verbose,
interactive=False,
**setup_databases_args
)
def teardown_database():
with django_db_blocker.unblock():
try:
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
except Exception as exc:
request.node.warn(
pytest.PytestWarning(
"Error when trying to teardown test databases: %r" % exc
)
)
if not django_db_keepdb:
request.addfinalizer(teardown_database)
对我没有帮助
1条答案
按热度按时间e0bqpujr1#
恼人的是,在设置数据库和加载适当的postgresql扩展之间没有适当的挂钩。你可以通过复制/修改禁止迁移的pytest-django代码并运行你的代码 * 而不是 * 上游代码来解决这个问题。