﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29040	test database creation log output doesn't use consistent stream	Chris Jerdonek	nobody	"While troubleshooting a test issue, I ran into confusing output that I tracked down to [https://github.com/django/django/blob/7fbb1bd00d8a3e9a834de83d36ebcbff15c18938/django/db/backends/base/creation.py#L173-L186 `base/creation.py`] (code shown below) logging its log output to two different streams. This caused messages to display different from their actual order.

Specifically, I was seeing the following message:

{{{
Got an error creating the test database: ...
}}}

//after// this message:

{{{
Destroying old test database for alias 'default'
}}}

when the actual order is the reverse:

{{{#!python
sys.stderr.write(
    ""Got an error creating the test database: %s\n"" % e)
if not autoclobber:
    confirm = input(
        ""Type 'yes' if you would like to try deleting the test ""
        ""database '%s', or 'no' to cancel: "" % test_database_name)
if autoclobber or confirm == 'yes':
    try:
        if verbosity >= 1:
            print(""Destroying old test database for alias %s..."" % (
                self._get_database_display_str(verbosity, test_database_name),
            ))
        cursor.execute('DROP DATABASE %(dbname)s' % test_db_params)
        self._execute_create_test_db(cursor, test_db_params, keepdb)
    except Exception as e:
        sys.stderr.write(
            ""Got an error recreating the test database: %s\n"" % e)
        sys.exit(2)
}}}

I think the correct solution is for this module to be logging all output to `stderr` (e.g. like Python's default `logging` behavior) -- reserving `stdout` for structured / API output. But even just outputting all messages to the //same// stream would be a big improvement.

I also think it would be a good idea to define a function like `log()` instead of having `print()` and `sys.stderr.write()` occur throughout. That would allow logging code to be controlled more centrally (aka DRY).
"	Cleanup/optimization	closed	Testing framework	dev	Normal	fixed	stdout,stderr,database,creation		Accepted	1	0	0	0	0	0
