2013-05-03 6 views
7

मैंने सफलतापूर्वक hstore एक्सटेंशन स्थापित किया है, और जब मैं syncdb करता हूं तो सब कुछ काम करता है। (मैं djorm-ext-hstore का उपयोग कर रहा हूं)django नाक परीक्षणों में hstore एक्सटेंशन को स्थापित करना

हालांकि, नाक परीक्षण चलाने के लिए एक नया अस्थायी डेटाबेस बनाता है, और इसमें hstore स्थापित नहीं है।

मुझे CREATE EXTENSION HSTORE; को डीबी सिंक करने से पहले परीक्षण डीबी पर CREATE EXTENSION HSTORE; चलाने की आवश्यकता है, लेकिन मुझे यह कैसे करना है इस बारे में कोई जानकारी नहीं मिल रही है।

कोई विचार?

+1

आप 'template1' पोस्टग्रेज़ डेटाबेस पर एक्सटेंशन बनाकर अपने आप को बहुत दर्द बचा सकते हैं। उसके बाद आपके द्वारा बनाए गए किसी भी डेटाबेस में एचएसटीओआर एक्सटेंशन होगा। – rantanplan

+0

rantaplan, जो जवाब होना चाहिए था! – 0atman

+0

यह ठीक है, मैं आमतौर पर एक-पंक्ति उत्तर देने से बचता हूं। खुशी मैंने मदद की। – rantanplan

उत्तर

3

मुझे लगता है कि आप django-nose का उपयोग कर रहे हैं।

from django.db import connections, DEFAULT_DB_ALIAS 
from django_nose import NoseTestSuiteRunner 

class MyTestSuiteRunner(NoseTestSuiteRunner): 
    def setup_databases(self): 
     result = super(MyTestSuiteRunner, self).setup_databases() 

     connection = connections[DEFAULT_DB_ALIAS] 
     cursor = connection.cursor() 
     cursor.execute('CREATE EXTENSION HSTORE') 

     return result 

फिर settings.py में आप निर्दिष्ट करना चाहिए अपने कस्टम परीक्षण धावक: इस के लिए है ठीक करने के लिए सबसे अच्छा तरीका है:

TEST_RUNNER = 'path.to.my.module.MyTestSuiteRunner' 
+0

धन्यवाद! इसे चलाने के बिना, मैं एक समस्या देख सकता हूं: यदि setup_databases() वास्तव में टेबल बनाता है। क्या हमें पहले एक्सटेंशन नहीं बनाना चाहिए? – 0atman

+0

आप शायद सही हैं, मैंने यह सुनिश्चित करने के लिए इस कोड को नहीं चलाया। –

+1

जब मैं इसे चलाता हूं तो मैं आपको बता दूंगा। – 0atman

24

यह एक गैर मुद्दा है इस मामले में आप अपने खुद के TestSuiteRunner बनाना चाहिए डिफ़ॉल्ट डेटाबेस पर hstore विस्तार लागू होते हैं, template1

psql -d template1 -c 'create extension hstore;'

संदर्भ: 0,123,

2

मेरे आखिरी उत्तर के बाद, Django ने pre_syncdb सिग्नल को हटा दिया और हटा दिया। मैंने हाल के संस्करणों को समायोजित करने के लिए उत्तर अपडेट किया है। मूल यांत्रिकी नए संस्करणों के लिए समान हैं क्योंकि दोनों विधियां संकेतों पर निर्भर करती हैं और SQL कोड जो केवल तभी निष्पादित करता है जब HSTORE एक्सटेंशन मौजूद न हो।

Django 1.8+

के बाद से Django डीबी माइग्रेशन शुरू की, pre_syncdb संकेतों marked deprecated in 1.7 और completely removed in 1.9 थे। हालांकि, उन्होंने pre_migrate नामक एक नया सिग्नल पेश किया जिसका उपयोग उसी तरह किया जा सकता है।

""" 
This is an example models.py which contains all model definition. 
""" 
from django.dispatch import receiver 
from django.db import connection, models 
from django.db.models.signals import pre_syncdb 
import sys 

# The sender kwarg is optional but will be called for every pre_syncdb signal 
# if omitted. Specifying it ensures this callback to be called once. 
@receiver(pre_migrate, sender=sys.modules[__name__]) 
def setup_postgres_hstore(sender, **kwargs): 
    """ 
    Always create PostgreSQL HSTORE extension if it doesn't already exist 
    on the database before syncing the database. 
    Requires PostgreSQL 9.1 or newer. 
    """ 
    cursor = connection.cursor() 
    cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore") 

# ...rest of your model definition goes here 
class Foo(models.Model): 
    # ...field definitions, etc. 

Django 1.6+ (मूल जवाब)

मेरे सुझाव pre_syncdb संकेत हुक का प्रयोग है।

अन्य उत्तर question पर मेरा उत्तर देखें।

""" 
This is an example models.py which contains all model definition. 
""" 
from django.dispatch import receiver 
from django.db import connection, models 
from django.db.models.signals import pre_syncdb 
import sys 

# The sender kwarg is optional but will be called for every pre_syncdb signal 
# if omitted. Specifying it ensures this callback to be called once. 
@receiver(pre_syncdb, sender=sys.modules[__name__]) 
def setup_postgres_hstore(sender, **kwargs): 
    """ 
    Always create PostgreSQL HSTORE extension if it doesn't already exist 
    on the database before syncing the database. 
    Requires PostgreSQL 9.1 or newer. 
    """ 
    cursor = connection.cursor() 
    cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore") 

# ...rest of your model definition goes here 
class Foo(models.Model): 
    # ...field definitions, etc. 

pre_syncdb संकेत से पहले मॉडल तालिका बनने निकाल दिया जाता है, यह सुनिश्चित करने के लिए आदर्श विस्तार स्थापित किया गया है जब परीक्षण डेटाबेस हर बार की स्थापना की जा रही है बना रही है। IF NOT EXISTS यह सुनिश्चित करता है कि एक्सटेंशन पहले से स्थापित होने पर PostgreSQL कमांड को अनदेखा करता है। यदि आप पहले से मौजूद एक्सटेंशन पर CREATE EXTENSION चलाते हैं तो आपको एक त्रुटि मिलेगी।

यह डिफ़ॉल्ट Django इकाई परीक्षण ढांचे के लिए काम करता है और सबसे अधिक संभावना Django नाक परीक्षण के लिए काम करेगा।

संकेतों पर और जानकारी: https://docs.djangoproject.com/en/1.6/ref/signals/#management-signals

4

इसके अलावा, आप एक प्रवास में एसक्यूएल आदेश चला सकते हैं (Django 1।8):

class Migration(migrations.Migration): 

    # ... 

    operations = [ 
     migrations.RunSQL('create extension hstore;'), 
     # ... 
5

Django 1.8 के साथ:

from django.contrib.postgres.operations import HStoreExtension 

class Migration(migrations.Migration): 
    ... 

    operations = [ 
     HStoreExtension(), 
     ... 
    ] 

https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#hstorefield

संपादित करें: बस ध्यान दें कि यह भी एक JSONField जो संभालती है (संयुक्त राष्ट्र) पहले से ही json और इनलाइन खोज के मार्शलिंग है। HStoreExtension इसके लिए आवश्यक नहीं है। Django> = 1.11 और पोस्टग्रेस> = 9.4 की आवश्यकता है।

संबंधित मुद्दे