How to force Django to create a test database with specific sorting?

I have a Postgres installation that is used to use LATIN1 as the default encoding. However, for my production database I use UTF8 (it is hosted on Heroku, so I have no choice).

I created my local dev database to set this parameter correctly:

sudo createdb -Upostgres $PROJECT_NAME --template=template0 \ --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 

however, I cannot run the django tests now, since the test database only uses the default Postgres cluster settings (LATIN1), which leads to test failures (I have invalid characters in some templates - ...character 0xe28099 of encoding "UTF8" has no equivalent in "LATIN1" )

The "nuclear" option is to reinstall Postgres with the correct settings (en_US.UTF8), however, since I run it in the VM, every time I deploy the virtual machine, I really don't want to do this, If there was a way to mass Django to create the database correctly, first of all, which would be preferable.

[UPDATE 1: TEST_ENCODING]

Following @sneawo's suggestion, I set the database TEST_ENCODING attribute and now I get the following error:

 Creating test database for alias 'default'... Got an error creating the test database: encoding UTF8 does not match locale en_US DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1. 

[UPDATE 2: nuclear option]

I talked about this above, and this is not for everyone, but since I run it in a virtual machine, it is very easy for me to recreate the postgres cluster with the correct sort in my sentence for strollers script (shell script):

 sudo service postgresql stop sudo pg_dropcluster 9.1 --stop main sudo pg_createcluster --start -e UTF-8 9.1 main sudo cp -f $CONF_DIR/pg_hba.conf /etc/postgresql/9.1/main/pg_hba.conf sudo cp -f $CONF_DIR/postgresql.conf /etc/postgresql/9.1/main/postgresql.conf sudo service postgresql restart 

I have standard versions of "allow all" pg_hba.conf and postgresql.conf that I copy from the host computer to overwrite the default settings created when the cluster was created. This is a bit of a crack sledgehammer solution, but it works and it is fast.

+4
source share
1 answer

Add this code below settings.py

 try: from local_settings import * except ImportError: pass 

Add this code to local_settings.py

 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test_db', 'USER': 'root', 'PASSWORD': '', 'HOST': '', 'PORT': '3306', 'OPTIONS': { 'charset': 'latin1', 'use_unicode': True, }, }, } 

Add local_settings.py to git ignore.

0
source

All Articles