Django和postgresql测试架构(Django and postgresql testing schema)

编程入门 行业动态 更新时间:2024-10-28 03:18:59
Django和postgresql测试架构(Django and postgresql testing schema)

我正在使用PostgreSQL 9.3和Django 1.7.4与psycopg2 2.5.4

DBA要求我们为我们的应用程序创建一个模式,而不是使用public。

我们定义了模式,我们不得不添加

'OPTIONS': { 'options': '-c search_path=custom-schema-name' },

到设置。

在测试期间,Django正在创建具有相应名称的测试数据库,但我们无法设置自定义模式名称

我试图找到一种方法来设置自定义模式名称(我已阅读文档 ),但我找不到在测试期间强制创建模式名称的方法。

我得到的错误是

django.db.utils.ProgrammingError:没有选择要创建的模式

当我看到创建的数据库时,默认情况下会创建一个公共模式。

我部分解决了这个问题,并在架构名称public上添加了搜索路径

'OPTIONS': { 'options': '-c search_path=custom-schema-name,public' },

但我想用自定义模式名称创建测试数据库。

有人知道如何设置测试模式名称吗?

I'm using PostgreSQL 9.3 and Django 1.7.4 with psycopg2 2.5.4

The DBA, asked us to create a schema for our application instead of using public.

We defined the schema, and we had to add the

'OPTIONS': { 'options': '-c search_path=custom-schema-name' },

to the settings.

During testing, Django is creating the test database with the corresponding name, but we can't set the custom-schema name

I tried to find a way to set up the custom schema name (I've read the docs) but I can't find a way to force the creation of the schema name during testing.

The error that I obtain is

django.db.utils.ProgrammingError: no schema has been selected to create in

When I see the created database , it has the schema public created by default.

I partially solved this issue and added to the search path the schema name public

'OPTIONS': { 'options': '-c search_path=custom-schema-name,public' },

but I'd like to create the test database with a custom schema name.

Does anybody knows how to set the testing schema name ?

最满意答案

我最终编写了一个自定义测试运行器来解决这个问题(使用django 1.9.x):

MyApp的/测试/ 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)

settings.py

TEST_RUNNER = 'myapp.test.runner.PostgresSchemaTestRunner'

I ended up with writing a custom test runner to solve this problem (using django 1.9.x):

myapp/test/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)

settings.py

TEST_RUNNER = 'myapp.test.runner.PostgresSchemaTestRunner'

更多推荐

本文发布于:2023-08-02 14:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:架构   测试   postgresql   Django   testing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!