Not Creating test DBs with PG backend

88 views
Skip to first unread message

Curtis Maloney

unread,
Aug 28, 2018, 2:23:52 AM8/28/18
to django-d...@googlegroups.com
Greetings,

one problem that turns up more and more frequently, it seems, is that
someone is trying to run tests but can't because of the following scenario:

1. The user they connect to the DB using a role that does NOT have
permission to create a DB

2. Django's PG adapter, when keepdb is not set, will attempt to create
the DB

3. If the create fails, and NOT with the error of "duplicate DB name",
KABOOM.

The error they get is that the user does not have permission to create
DBs, since permissions are checked before testing if the name exists.

Instead, I'm trying to leverage the existing _database_exists check used
by keepdb.

My initial attempt checked if the DB exists and, if it does, issues a
"DROP OWNED BY CURRENT_USER;" instead of calling _execute_create_test_db.

Of course, this failed the tests, and rightly so. This obliterates the
protection of connecting to the wrong DB... and, in fact, makes that
mistake far FAR more damaging.

So I'm looking for input on what might be the best direction moving forward:

1. wrangle a new option to say "use existing" ... to allow using an
existing DB if it exists.

2. prompt the user to confirm they want to use the existing DB... and
perhaps prompt a second time to confirm they want to DROP OWNED BY...

3. some other option?

The first option, at least, will possibly require more invasive changes,
since higher levels of the code make assumptions about
_execute_create_test_db and when it will succeed / raise exception.

--
Curtis

Tim Graham

unread,
Aug 28, 2018, 6:33:41 AM8/28/18
to Django developers (Contributions to Django itself)
Why is a new option needed? Why can't --keepdb be used for this use case?

Curtis Maloney

unread,
Aug 28, 2018, 8:32:56 PM8/28/18
to django-d...@googlegroups.com
On 08/28/2018 08:33 PM, Tim Graham wrote:
> Why is a new option needed? Why can't --keepdb be used for this use case?

Because that would be different behavior.

If you can convince me that using --keepdb is a solution, then I will
use your argument to say that --keepdb should be on always.

What I'm looking to offer users is to create and clean up objects in the
test DB, separate from create/drop test DB.

However, I want to do this in a way that doesn't weaken the existing
protection for "Oops, we're trying to use a test DB that already
exists... did you make a mistake? We don't want to trounce real data."

--
Curtis

Tim Graham

unread,
Aug 29, 2018, 2:11:58 PM8/29/18
to Django developers (Contributions to Django itself)
Thanks for clarifying. I left an idea (reuse Oracle-specifc CREATE_DB setting, if it has the same meaning) on the ticket. Maybe we continue the discussion there for now.

Reply all
Reply to author
Forward
0 new messages