Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle south migrations #129

Merged
merged 2 commits into from Jul 27, 2014
Merged

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented Jul 10, 2014

Ref: #128

@pelme
Copy link
Member

pelme commented Jul 10, 2014

Looks good.

Just having SOUTH_TESTS_MIGRATE = False makes it behave as before, right?

@blueyed
Copy link
Contributor Author

blueyed commented Jul 10, 2014

Unfortunately it's not fine.

With --create-db it fails (--reuse-db is fine, given that the DB is OK).

The initial problem is with (e.g.) socialaccount_socialapp, or any other app using migrations:

request = <SubRequest '_django_db_setup' for <Function 'test_no_change_permission'>>, _django_test_environment = None
_django_cursor_wrapper = <pytest_django.plugin.CursorManager object at 0x3d04a10>

    @pytest.fixture(scope='session')
    def _django_db_setup(request,
                         _django_test_environment,
                         _django_cursor_wrapper):
        """Session-wide database setup, internal to pytest-django"""
        skip_if_no_django()

        from .compat import setup_databases, teardown_databases

        # xdist
        if hasattr(request.config, 'slaveinput'):
            db_suffix = request.config.slaveinput['slaveid']
        else:
            db_suffix = None

        monkey_patch_creation_for_db_suffix(db_suffix)

        with _django_cursor_wrapper:
            # Monkey patch Django's setup code to support database re-use
            if request.config.getvalue('reuse_db'):
                if not request.config.getvalue('create_db'):
                    monkey_patch_creation_for_db_reuse()

            # Create the database
>           db_cfg = setup_databases(verbosity=0, interactive=False)

…/pytest_django/pytest_django/fixtures.py:47:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
…/django-stable/django/test/runner.py:279: in setup_databases
    verbosity, autoclobber=not interactive)
…/django-stable/django/db/backends/creation.py:349: in create_test_db
    database=self.connection.alias)
…/django-stable/django/core/management/__init__.py:159: in call_command
    return klass.execute(*args, **defaults)
…/django-stable/django/core/management/base.py:285: in execute
    output = self.handle(*args, **options)
…/django-stable/django/core/management/base.py:415: in handle
    return self.handle_noargs(**options)
…/django-stable/django/core/management/commands/flush.py:78: in handle_noargs
    six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])
…/django-stable/django/core/management/commands/flush.py:69: in handle_noargs
    cursor.execute(sql)
…/django-stable/django/db/backends/util.py:53: in execute
    return self.cursor.execute(sql, params)
…/django-stable/django/db/utils.py:99: in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)

…

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.backends.util.CursorWrapper object at 0x4890390>, sql = 'SELECT setval(pg_get_serial_sequence(\'"socialaccount_socialapp"\',\'id\'), 1, false);'
params = None

    def execute(self, sql, params=None):
        self.db.validate_no_broken_transaction()
        self.db.set_dirty()
        with self.db.wrap_database_errors:
            if params is None:
>               return self.cursor.execute(sql)
E               CommandError: Database tmm_tests couldn't be flushed. Possible reasons:
E                 * The database isn't running or isn't configured correctly.
E                 * At least one of the expected database tables doesn't exist.
E                 * The SQL was invalid.
E               Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
E               The full error: relation "socialaccount_socialapp" does not exist

…/django-stable/django/db/backends/util.py:51: CommandError
------------------------------------------------------------------------- Captured stderr setup --------------------------------------------------------------------------
Got an error creating the test database: database "tmm_tests" already exists

The following tests then fail with:

Got an error recreating the test database: cannot drop the currently open database

       …
       except Exception as e:
            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 '%s'..."
                              % self.connection.alias)
                    cursor.execute(
                        "DROP DATABASE %s" % qn(test_database_name))
                    cursor.execute(
                        "CREATE DATABASE %s %s" % (qn(test_database_name),
                                                   suffix))
                except Exception as e:
                    sys.stderr.write(
                        "Got an error recreating the test database: %s\n" % e)
>                   sys.exit(2)
E                   SystemExit: 2

…/django-stable/django/db/backends/creation.py:411: SystemExit
--- … Captured stderr setup … ---
Got an error creating the test database: database "project_tests" already exists

Got an error recreating the test database: cannot drop the currently open database

@blueyed
Copy link
Contributor Author

blueyed commented Jul 10, 2014

I've pushed a new commit, which fixes this and also fixes handling of initial_data.

@blueyed blueyed changed the title Experimental: do not disable south migrations Handle south migrations Jul 26, 2014
This requires to monkey-patch `south.hacks.django_1_0.SkipFlushCommand`,
which has a bug that prevents any initial data to be installed.

South issue: http://south.aeracode.org/ticket/1395#comment:3

Fixes pytest-dev#22

Django code reference for 1.6, from
django-1.6.x/django/db/backends/creation.py(339)create_test_db():

    # Report syncdb messages at one level lower than that requested.
    # This ensures we don't get flooded with messages during testing
    # (unless you really ask to be flooded)
    call_command('syncdb',
        verbosity=max(verbosity - 1, 0),
        interactive=False,
        database=self.connection.alias,
        load_initial_data=False)

    # We need to then do a flush to ensure that any data installed by
    # custom SQL has been removed. The only test data should come from
    # test fixtures, or autogenerated from post_syncdb triggers.
    # This has the side effect of loading initial data (which was
    # intentionally skipped in the syncdb).
    call_command('flush',
        verbosity=max(verbosity - 1, 0),
        interactive=False,
        database=self.connection.alias)
@blueyed
Copy link
Contributor Author

blueyed commented Jul 26, 2014

@pelme
This should be good to merge, but please review it first.

I am skipping the South related tests for Python 3.4 and Django >= 1.7, because South appears to cause trouble with both.

pelme added a commit that referenced this pull request Jul 27, 2014
@pelme pelme merged commit be3057e into pytest-dev:master Jul 27, 2014
@pelme
Copy link
Member

pelme commented Jul 27, 2014

Thank you so much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants