#27751: Teardown of a PG test database fails if it is an autogenerated test_MYDB
database
-----------------------------------+------------------------------------
     Reporter:  Cynthia Kiser      |                    Owner:  nobody
         Type:  Bug                |                   Status:  new
    Component:  Testing framework  |                  Version:  1.9
     Severity:  Normal             |               Resolution:
     Keywords:  database, testing  |             Triage Stage:  Accepted
    Has patch:  0                  |      Needs documentation:  0
  Needs tests:  0                  |  Patch needs improvement:  0
Easy pickings:  0                  |                    UI/UX:  0
-----------------------------------+------------------------------------

Comment (by Marek Onuszko):

 I'm getting the same error.

 Debian Linux 8.0 (Jessie)
 Python 3.4.2 (python --version)
 Django 1.10.5 (installed in virtualenv via pip)
 psycopg2 2.7.1 (installed in virtualenv via pip)

 {{{
 dpkg -l | grep postgres
 ii  postgresql
 9.4+165+deb8u2                       all          object-relational SQL
 database (supported version)
 ii  postgresql-9.4
 9.4.10-0+deb8u1                      amd64        object-relational SQL
 database, version 9.4 server
 ii  postgresql-client-9.4
 9.4.10-0+deb8u1                      amd64        front-end programs for
 PostgreSQL 9.4
 ii  postgresql-client-common                                    165+deb8u2
 all          manager for multiple PostgreSQL client versions
 ii  postgresql-common                                           165+deb8u2
 all          PostgreSQL database-cluster manager
 ii  postgresql-server-dev-9.4
 9.4.10-0+deb8u1                      amd64        development files for
 PostgreSQL 9.4 server-side programming
 }}}

 Setting up database was like this:

 {{{
 CREATE ROLE kartyuser LOGIN PASSWORD 'somepassword';
 ALTER ROLE kartyuser CREATEDB;
 CREATE DATABASE kartydb;
 ALTER ROLE kartyuser SET client_encoding TO 'utf8';
 ALTER ROLE kartyuser SET default_transaction_isolation TO 'read
 committed';
 ALTER ROLE kartyuser SET timezone TO 'UTC';
 GRANT ALL PRIVILEGES ON DATABASE kartydb TO kartyuser;
 }}}

 pg_hba.conf:
 {{{
 # "local" is for Unix domain socket connections only
 local   all             all                                     peer
 host    kartydb         kartyuser       10.7.90.5/24            md5
 host    test_kartydb    kartyuser       10.7.90.5/24            md5
 # IPv4 local connections:
 host    all             all             127.0.0.1/32            md5
 # IPv6 local connections:
 host    all             all             ::1/128                 md5
 }}}

 settings.py:
 {{{
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'kartydb',
         'USER': 'kartyuser',
         'PASSWORD': 'somepassword',
         'HOST': '10.7.90.5',
         'PORT': '5432',
     }
 }

 }}}

 runtests.sh:

 {{{
 coverage run manage.py test karty.tests
 coverage report
 }}}

 Traceback:
 {{{
 /home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/postgresql/base.py:248: RuntimeWarning:
 Normally Django will use a connection to the 'postgres' database to avoid
 running initialization queries against the production database when it's
 not needed (for example, when running tests). Django was unable to create
 a connection to the 'postgres' database and will use the default database
 instead.
   RuntimeWarning
 Got an error creating the test database: database "test_kartydb" already
 exists

 Type 'yes' if you would like to try deleting the test database
 'test_kartydb', or 'no' to cancel: yes
 Destroying old test database for alias 'default'...
 ..........................
 ----------------------------------------------------------------------
 Ran 26 tests in 4.090s

 OK
 Destroying test database for alias 'default'...
 /home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/postgresql/base.py:248: RuntimeWarning:
 Normally Django will use a connection to the 'postgres' database to avoid
 running initialization queries against the production database when it's
 not needed (for example, when running tests). Django was unable to create
 a connection to the 'postgres' database and will use the default database
 instead.
   RuntimeWarning
 Traceback (most recent call last):
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 62, in execute
     return self.cursor.execute(sql)
 psycopg2.OperationalError: cannot drop the currently open database


 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "manage.py", line 22, in <module>
     execute_from_command_line(sys.argv)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 367, in
 execute_from_command_line
     utility.execute()
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 359, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/commands/test.py", line 29, in
 run_from_argv
     super(Command, self).run_from_argv(argv)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/base.py", line 294, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/base.py", line 345, in execute
     output = self.handle(*args, **options)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/core/management/commands/test.py", line 72, in handle
     failures = test_runner.run_tests(test_labels)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/test/runner.py", line 551, in run_tests
     self.teardown_databases(old_config)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/test/runner.py", line 526, in teardown_databases
     connection.creation.destroy_test_db(old_name, self.verbosity,
 self.keepdb)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/base/creation.py", line 264, in
 destroy_test_db
     self._destroy_test_db(test_database_name, verbosity)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/base/creation.py", line 283, in
 _destroy_test_db
     % self.connection.ops.quote_name(test_database_name))
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 64, in execute
     return self.cursor.execute(sql, params)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/utils.py", line 94, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/utils/six.py", line 685, in reraise
     raise value.with_traceback(tb)
   File "/home/monuszko/warsztat/ENV/karty/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 62, in execute
     return self.cursor.execute(sql)
 django.db.utils.OperationalError: cannot drop the currently open database

 Name                                              Stmts   Miss Branch
 BrPart  Cover
 
-----------------------------------------------------------------------------------
 karty/admin.py                                       10      0      0
 0   100%
 karty/apps.py                                         3      0      0
 0   100%
 karty/factories.py                                   35      0      4
 0   100%
 karty/management/commands/create_sample_data.py      18      0      4
 0   100%
 karty/management/commands/destroy_app_data.py         7      0      0
 0   100%
 karty/models.py                                      44      1      4
 0    98%
 karty/serializers.py                                 11      0      0
 0   100%
 karty/urls.py                                         4      0      0
 0   100%
 karty/views.py                                       53      2      8
 1    95%
 
-----------------------------------------------------------------------------------
 TOTAL                                               185      3     20
 1    98%
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27751#comment:11>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.eea50992857de81b4793228ed50d3911%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to