Fixed #9858 -- Added ability to use PostGIS template with GIS test runner via `POSTGIS_TEMPLATE` setting. Thanks to Aryeh Leib Taurog for feature suggestion and initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9803 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2009-02-01 18:22:57 +00:00
parent d982edff4b
commit 2b8e768b22
1 changed files with 35 additions and 24 deletions

View File

@ -39,12 +39,18 @@ def create_lang(db_name, verbosity=1):
def _create_with_cursor(db_name, verbosity=1, autoclobber=False): def _create_with_cursor(db_name, verbosity=1, autoclobber=False):
"Creates database with psycopg2 cursor." "Creates database with psycopg2 cursor."
qn = connection.ops.quote_name
# Constructing the necessary SQL to create the database (the DATABASE_USER # Constructing the necessary SQL to create the database.
# must possess the privileges to create a database) create_sql = 'CREATE DATABASE %s' % qn(db_name)
create_sql = 'CREATE DATABASE %s' % connection.ops.quote_name(db_name)
# If there's a template database for PostGIS set, then use it.
if hasattr(settings, 'POSTGIS_TEMPLATE'):
create_sql += ' TEMPLATE %s' % qn(settings.POSTGIS_TEMPLATE)
# The DATABASE_USER must possess the privileges to create a spatial database.
if settings.DATABASE_USER: if settings.DATABASE_USER:
create_sql += ' OWNER %s' % settings.DATABASE_USER create_sql += ' OWNER %s' % qn(settings.DATABASE_USER)
cursor = connection.cursor() cursor = connection.cursor()
connection.creation.set_autocommit() connection.creation.set_autocommit()
@ -52,7 +58,6 @@ def _create_with_cursor(db_name, verbosity=1, autoclobber=False):
try: try:
# Trying to create the database first. # Trying to create the database first.
cursor.execute(create_sql) cursor.execute(create_sql)
#print create_sql
except Exception, e: except Exception, e:
# Drop and recreate, if necessary. # Drop and recreate, if necessary.
if not autoclobber: if not autoclobber:
@ -71,10 +76,15 @@ def _create_with_shell(db_name, verbosity=1, autoclobber=False):
If no spatial database already exists, then using a cursor will not work. If no spatial database already exists, then using a cursor will not work.
Thus, a `createdb` command will be issued through the shell to bootstrap Thus, a `createdb` command will be issued through the shell to bootstrap
creation of the spatial database. creation of the spatial database.
"""
TODO: Actually allow this method to be used without a spatial database
in place first.
"""
# Getting the command-line options for the shell command # Getting the command-line options for the shell command
options = get_cmd_options(False) options = get_cmd_options(False)
if hasattr(settings, 'POSTGIS_TEMPLATE'):
options += '-T %s ' % settings.POSTGIS_TEMPlATE
create_cmd = 'createdb -O %s %s%s' % (settings.DATABASE_USER, options, db_name) create_cmd = 'createdb -O %s %s%s' % (settings.DATABASE_USER, options, db_name)
if verbosity >= 1: print create_cmd if verbosity >= 1: print create_cmd
@ -115,6 +125,8 @@ def create_spatial_db(test=False, verbosity=1, autoclobber=False, interactive=Fa
db_name = get_spatial_db() db_name = get_spatial_db()
_create_with_shell(db_name, verbosity=verbosity, autoclobber=autoclobber) _create_with_shell(db_name, verbosity=verbosity, autoclobber=autoclobber)
# If a template database is used, then don't need to do any of the following.
if not hasattr(settings, 'POSTGIS_TEMPLATE'):
# Creating the db language, does not need to be done on NT platforms # Creating the db language, does not need to be done on NT platforms
# since the PostGIS installer enables this capability. # since the PostGIS installer enables this capability.
if os.name != 'nt': if os.name != 'nt':
@ -176,7 +188,6 @@ def load_postgis_sql(db_name, verbosity=1):
This routine loads up the PostGIS SQL files lwpostgis.sql and This routine loads up the PostGIS SQL files lwpostgis.sql and
spatial_ref_sys.sql. spatial_ref_sys.sql.
""" """
# Getting the path to the PostGIS SQL # Getting the path to the PostGIS SQL
try: try:
# POSTGIS_SQL_PATH may be placed in settings to tell GeoDjango where the # POSTGIS_SQL_PATH may be placed in settings to tell GeoDjango where the