Opened 8 years ago

Closed 8 years ago

#27061 closed New feature (fixed)

Support creating the TEST PostgreSQL database from a custom template

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using PostgreSQL 9.3.13, when I set the TEST database setting CHARSET to UTF8 and run tests with a personal project, I get the following error:

Got an error creating the test database: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT: Use the same encoding as in the template database, or use template0 as template.

I believe the way to do this in postgres is something like:

CREATE DATABASE <name> WITH TEMPLATE template0 ENCODING 'UTF8'

(See here for postgres documentation.)

You can see here in Django's sql_table_creation_suffix() that it doesn't allow passing a template name:

def sql_table_creation_suffix(self):
    test_settings = self.connection.settings_dict['TEST']
    assert test_settings['COLLATION'] is None, (
        "PostgreSQL does not support collation setting at database creation time."
    )
    if test_settings['CHARSET']:
        return "WITH ENCODING '%s'" % test_settings['CHARSET']
    return ''

Perhaps adding a postgres-specific TEMPLATE setting to the TEST database settings would be a general way to solve this?

Change History (9)

comment:1 by Claude Paroz, 8 years ago

This was already handled in #17207 and #18710 as documentation/wont' fix resolutions. I still think you should fix your main template encoding.
I'm letting the ticket open for another opinion.

comment:2 by Chris Jerdonek, 8 years ago

I still think you should fix your main template encoding.

There's no need for me to change the PostgreSQL installation or change the template encoding because I'm able to run the following CREATE statement in my environment setup:

CREATE DATABASE <name> WITH TEMPLATE template0 ENCODING 'UTF8'

But Django doesn't support this option.

If Django could clone a test database from an existing database, that would be another option. It already has syntax like the following in postgresql's _clone_test_db() to clone a test database from another test database (from here:

cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % (
    qn(target_database_name), qn(source_database_name)))

So it's almost there. It's just the bootstrapping part that's missing, which we would get for free if the Django code were more DRY.

Version 1, edited 8 years ago by Chris Jerdonek (previous) (next) (diff)

comment:3 by Chris Jerdonek, 8 years ago

For future reference, here is a link to the official PostgreSQL docs, where it explains how to use other encodings without changing any installation templates. For example--

The encoding and locale settings must match those of the template database, except when template0 is used as template. ... CREATE DATABASE music ENCODING 'LATIN1' TEMPLATE template0; In this example, the TEMPLATE template0 clause would only be required if template1's encoding is not ISO-8859-1.

comment:4 by Claude Paroz, 8 years ago

Component: UncategorizedTesting framework
Summary: unable to set CHARSET to UTF8 for TEST database setting with PostgreSQLSupport creating the TEST PostgreSQL database from a custom template
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

Hopefully you don't mind the summary change!

comment:5 by Chris Jerdonek, 8 years ago

Cool, thanks! And not at all! :)

comment:6 by Chris Jerdonek, 8 years ago

Owner: changed from nobody to Chris Jerdonek
Status: newassigned

comment:7 by Chris Jerdonek, 8 years ago

Has patch: set

I posted a pull request for this here.

comment:8 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In a3db4803:

Fixed #27061 -- Added a TESTTEMPLATE setting for PostgreSQL.

Note: See TracTickets for help on using tickets.
Back to Top