Jor*_*uez 8 django postgresql psycopg2 django-testing
我将 PostgreSQL 9.3 和 Django 1.7.4 与 psycopg2 2.5.4 一起使用
DBA 要求我们为我们的应用程序创建一个架构,而不是使用公共架构。
我们定义了模式,我们必须添加
'OPTIONS': {
'options': '-c search_path=custom-schema-name'
},
Run Code Online (Sandbox Code Playgroud)
到设置。
在测试过程中,Django正在创建对应名称的测试数据库,但是我们不能设置自定义模式名称
我试图找到一种方法来设置自定义架构名称(我已经阅读了文档),但是我找不到在测试期间强制创建架构名称的方法。
我得到的错误是
django.db.utils.ProgrammingError: 没有选择要在其中创建的架构
当我看到创建的 database 时,它默认创建了架构 public 。
我部分解决了这个问题,并将架构名称 public 添加到搜索路径中
'OPTIONS': {
'options': '-c search_path=custom-schema-name,public'
},
Run Code Online (Sandbox Code Playgroud)
但我想用自定义模式名称创建测试数据库。
有人知道如何设置测试模式名称吗?
dah*_*ens 10
我最终编写了一个自定义测试运行器来解决这个问题(使用 django 1.9.x):
我的应用程序/测试/runner.py
from types import MethodType
from django.test.runner import DiscoverRunner
from django.db import connections
def prepare_database(self):
self.connect()
self.connection.cursor().execute("""
CREATE SCHEMA foobar_schema AUTHORIZATION your_user;
GRANT ALL ON SCHEMA foobar_schema TO your_user;
""")
class PostgresSchemaTestRunner(DiscoverRunner):
def setup_databases(self, **kwargs):
for connection_name in connections:
connection = connections[connection_name]
connection.prepare_database = MethodType(prepare_database, connection)
return super().setup_databases(**kwargs)
Run Code Online (Sandbox Code Playgroud)
设置.py
TEST_RUNNER = 'myapp.test.runner.PostgresSchemaTestRunner'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1673 次 |
最近记录: |